Skip to content

Commit

Permalink
Update set description and test CI
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Sampaio Queiroga <[email protected]>
  • Loading branch information
Sirherobrine23 committed Feb 20, 2024
1 parent a21535b commit b8b39f4
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 69 deletions.
34 changes: 34 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM ubuntu:latest
ARG DEBIAN_FRONTEND="noninteractive"
ARG GOVERSION="1.22.0"
ARG ZIGVERSION="0.11.0"

RUN apt update && apt list --upgradable -a && apt upgrade -y
RUN apt update && apt install -y xz-utils software-properties-common cmake make build-essential git curl wget jq sudo procps zsh tar screen ca-certificates procps lsb-release gnupg gnupg2 gpg apt-transport-https python3-pip apt-file attr bash-completion bc bison clang command-not-found dialog dos2unix ed flex gawk gperf htop libresolv-wrapper lld llvm lsof man neofetch neovim rhash tree tshark unbound unzip xxhash openssh-server openssh-client
STOPSIGNAL SIGSTOP
CMD [ "zsh" ]
WORKDIR /root

# Nodejs
RUN curl https://deb.nodesource.com/setup_lts.x | bash && apt install nodejs -y

# Go (golang)
RUN wget -qO- "https://go.dev/dl/go${GOVERSION}.linux-$(dpkg --print-architecture).tar.gz" | tar -C /usr/local -xzv && ln -s /usr/local/go/bin/go /usr/bin/go && ln -s /usr/local/go/bin/gofmt /usr/bin/gofmt

# Install Github CLI (gh)
RUN (wget -q "$(wget -qO- https://api.github.com/repos/cli/cli/releases/latest | grep 'browser_download_url' | grep '.deb' | cut -d \" -f 4 | grep $(dpkg --print-architecture))" -O /tmp/gh.deb && dpkg -i /tmp/gh.deb && rm /tmp/gh.deb) || echo "Fail Install gh"

# Zig
RUN wget -qO- "https://ziglang.org/download/${ZIGVERSION}/zig-linux-$(uname -m)-${ZIGVERSION}.tar.xz" | tar -xvJ -C /usr/local/ && \
mv -v /usr/local/zig* /usr/local/zig && \
ln -s /usr/local/zig/zig /usr/bin/zig

ARG USERNAME="devcontainer"
ARG USER_UID="1000"
ARG USER_GID=$USER_UID

# Add non root user and Install oh my zsh
RUN addgroup --gid "${USER_GID}" "${USERNAME}"; useradd --uid "${USER_UID}" --gid "${USER_GID}" --groups sudo -m --shell /usr/bib/zsh "${USERNAME}" -p test
USER $USERNAME
WORKDIR /home/$USERNAME
RUN git clone https://github.com/Sirherobrine23/dotfiles.git dotfiles && cd dotfiles && ./install; cd /home/$USERNAME; rm -rf dotfiles
13 changes: 13 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "rebory linux devcontainer",
"updateRemoteUserUID": false,
"containerUser": "develop",
"remoteUser": "develop",
"build": {
"dockerfile": "Dockerfile",
"args": {
"USERNAME": "develop",
"USER_UID": "1000"
}
}
}
28 changes: 14 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: "Setup zig"
uses: korandoru/setup-zig@v1
with:
zig-version: "master"
zig-version: "0.11.0"

- uses: actions/setup-node@v4
name: Setup Node.js
Expand All @@ -28,8 +28,7 @@ jobs:
- name: Install dependencies
run: |
export DEBIAN_FRONTEND=noninteractive
sudo apt update
sudo apt install -y "binutils-multiarch" "build-essential"
sudo apt update && sudo apt install -y "build-essential"
npm install --no-save --ignore-scripts
- name: Build addon
Expand All @@ -44,19 +43,15 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
target_arch: [ "x86_64", "aarch64" ]
target_os:
- "linux"
# - "macos"
node_version: [ 16.x, 17.x, 18.x, 19.x, 20.x, 21.x ]
steps:
- uses: actions/checkout@v4
name: Checkout

- name: "Setup zig"
uses: korandoru/setup-zig@v1
with:
zig-version: "master"
zig-version: "0.11.0"

- uses: actions/setup-node@v4
name: Setup Node.js
Expand All @@ -66,16 +61,21 @@ jobs:
- name: Install dependencies
run: |
export DEBIAN_FRONTEND=noninteractive
sudo apt update
sudo apt install -y "binutils-multiarch" "build-essential"
sudo apt update && sudo apt install -y "build-essential"
npm install --no-save --ignore-scripts
- name: Build addon
run: npm run dev -- --target ${{ matrix.target_arch }}-${{ matrix.target_os }}
- name: Build addon linux
run: npm run dev -- --target x86_64-linux --target aarch64-linux

- name: Build addon macos
run: npm run dev -- --target x86_64-macos --target aarch64-macos

- name: Build addon windows
run: npm run dev -- --target x86_64-windows --target aarch64-windows

- name: Upload prebuilds interface
uses: actions/upload-artifact@v3
with:
retention-days: 7
name: prebuilds_${{ matrix.target_arch }}-${{ matrix.target_os }}_${{ matrix.node_version }}
name: build-${{ matrix.node_version }}
path: "build/*"
104 changes: 78 additions & 26 deletions addons/tools/wginterface-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,79 @@ extern "C" {
#include "linux/wireguard.h"
}

#define SETCONFIG 1
#define GETCONFIG 1
#define LISTDEV 1
#define DELIFACE 1
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */

std::string getKernelMesage(int errStatus) {
std::string message = std::string("Error code: ").append(std::to_string(errStatus));
if (errStatus == -EPERM) message = std::string("Operation not permitted, code: ").append(std::to_string(errStatus));
else if (errStatus == -ENOENT) message = std::string("No such file or directory, code: ").append(std::to_string(errStatus));
else if (errStatus == -ESRCH) message = std::string("No such process, code: ").append(std::to_string(errStatus));
else if (errStatus == -EINTR) message = std::string("Interrupted system call, code: ").append(std::to_string(errStatus));
else if (errStatus == -EIO) message = std::string("I/O error, code: ").append(std::to_string(errStatus));
else if (errStatus == -ENXIO) message = std::string("No such device or address, code: ").append(std::to_string(errStatus));
else if (errStatus == -E2BIG) message = std::string("Argument list too long, code: ").append(std::to_string(errStatus));
else if (errStatus == -ENOEXEC) message = std::string("Exec format error, code: ").append(std::to_string(errStatus));
else if (errStatus == -EBADF) message = std::string("Bad file number, code: ").append(std::to_string(errStatus));
else if (errStatus == -ECHILD) message = std::string("No child processes, code: ").append(std::to_string(errStatus));
else if (errStatus == -EAGAIN) message = std::string("Try again, code: ").append(std::to_string(errStatus));
else if (errStatus == -ENOMEM) message = std::string("Out of memory, code: ").append(std::to_string(errStatus));
else if (errStatus == -EACCES) message = std::string("Permission denied, code: ").append(std::to_string(errStatus));
else if (errStatus == -EFAULT) message = std::string("Bad address, code: ").append(std::to_string(errStatus));
else if (errStatus == -ENOTBLK) message = std::string("Block device required, code: ").append(std::to_string(errStatus));
else if (errStatus == -EBUSY) message = std::string("Device or resource busy, code: ").append(std::to_string(errStatus));
else if (errStatus == -EEXIST) message = std::string("File exists, code: ").append(std::to_string(errStatus));
else if (errStatus == -EXDEV) message = std::string("Cross-device link, code: ").append(std::to_string(errStatus));
else if (errStatus == -ENODEV) message = std::string("No such device, code: ").append(std::to_string(errStatus));
else if (errStatus == -ENOTDIR) message = std::string("Not a directory, code: ").append(std::to_string(errStatus));
else if (errStatus == -EISDIR) message = std::string("Is a directory, code: ").append(std::to_string(errStatus));
else if (errStatus == -EINVAL) message = std::string("Invalid argument, code: ").append(std::to_string(errStatus));
else if (errStatus == -ENFILE) message = std::string("File table overflow, code: ").append(std::to_string(errStatus));
else if (errStatus == -EMFILE) message = std::string("Too many open files, code: ").append(std::to_string(errStatus));
else if (errStatus == -ENOTTY) message = std::string("Not a typewriter, code: ").append(std::to_string(errStatus));
else if (errStatus == -ETXTBSY) message = std::string("Text file busy, code: ").append(std::to_string(errStatus));
else if (errStatus == -EFBIG) message = std::string("File too large, code: ").append(std::to_string(errStatus));
else if (errStatus == -ENOSPC) message = std::string("No space left on device, code: ").append(std::to_string(errStatus));
else if (errStatus == -ESPIPE) message = std::string("Illegal seek, code: ").append(std::to_string(errStatus));
else if (errStatus == -EROFS) message = std::string("Read-only file system, code: ").append(std::to_string(errStatus));
else if (errStatus == -EMLINK) message = std::string("Too many links, code: ").append(std::to_string(errStatus));
else if (errStatus == -EPIPE) message = std::string("Broken pipe, code: ").append(std::to_string(errStatus));
else if (errStatus == -EDOM) message = std::string("Math argument out of domain of func, code: ").append(std::to_string(errStatus));
else if (errStatus == -ERANGE) message = std::string("Math result not representable, code: ").append(std::to_string(errStatus));
return message;
}

unsigned long maxName() {
return IFNAMSIZ;
Expand Down Expand Up @@ -78,13 +147,7 @@ void deleteInterface::Execute() {
if (!!devicesList) {
for ((device_name) = (devicesList), (len) = 0; ((len) = strlen(device_name)); (device_name) += (len) + 1) {
if (device_name == wgName.c_str()) {
if ((len = wg_add_device(wgName.c_str())) < 0) {
std::string err = "Error code: ";
err = err.append(std::to_string(len));
if (len == -ENOMEM) err = "Out of memory";
else if (len == -errno) err = ((std::string)"Cannot add device, code: ").append(std::to_string(len));
SetError(err);
}
if ((len = wg_add_device(wgName.c_str())) < 0) SetError(getKernelMesage(len));
break;
}
}
Expand All @@ -94,12 +157,8 @@ void deleteInterface::Execute() {

void setConfig::Execute() {
int res = setInterface(wgName);
if (res < 0) {
std::string err = "Error code: ";
err = err.append(std::to_string(res));
if (res == -ENOMEM) err = "Out of memory";
else if (res == -errno) err = ((std::string)"Cannot add device, code: ").append(std::to_string(res));
SetError(err);
if (res < 0 && res != EEXIST) {
SetError(getKernelMesage(res));
return;
}

Expand Down Expand Up @@ -270,12 +329,7 @@ void setConfig::Execute() {

// Set interface config
if ((res = wg_set_device(deviceStruct)) < 0) {
std::string err = "Set wireguard config Error code: ";
err = err.append(std::to_string(res));
if (res == -ENODEV) err = "No such device";
else if (res == -EINVAL) err = "Invalid argument";
else if (res == -ENOSPC) err = "No space left on device";
SetError(err);
SetError(getKernelMesage(res));
}

if (res >= 0) {
Expand Down Expand Up @@ -313,9 +367,7 @@ std::string keyTo64(const uint8_t *key) {
void getConfig::Execute() {
int res; wg_device *device;
if ((res = wg_get_device(&device, strdup(wgName.c_str()))) < 0) {
std::string err = "Cannot get wireguard device, Error code ";
err = err.append(std::to_string(res));
SetError(err);
SetError(getKernelMesage(res));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion addons/tools/wginterface-win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ std::string getErrorString(DWORD errorMessageID) {

std::string startAddon(const Napi::Env env, Napi::Object exports) {
if (!IsRunAsAdmin()) return "Run nodejs with administrator privilegies";
auto DLLPATH = exports.Get("WIREGUARD_DLL_PATH");
auto DLLPATH = exports.Get("WIN32DLLPATH");
if (!(DLLPATH.IsString())) return "Require WIREGUARD_DLL_PATH in addon load!";
LPCWSTR dllPath = toLpcwstr(DLLPATH.ToString());

Expand Down
19 changes: 15 additions & 4 deletions addons/tools/wginterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,26 @@ Napi::Object Init(Napi::Env initEnv, Napi::Object exports) {

// Wireguard constants set
const Napi::Object constants = Napi::Object::New(initEnv);
constants.Set("WG_B64_LENGTH", B64_WG_KEY_LENGTH);
constants.Set("WG_LENGTH", WG_KEY_LENGTH);
constants.Set("MAX_NAME_LENGTH", maxName());
// Set wireguard version if present
constants.Set("driveVersion", versionDrive());

// Constants
// Wireguard max name length
constants.Set("nameLength", maxName());

constants.Set("base64Length", B64_WG_KEY_LENGTH);
constants.Set("keyLength", WG_KEY_LENGTH);

// Set addon constants
exports.Set("constants", constants);

// Function's
#ifdef USERSPACE_GO
exports.Set("createTun", Napi::Function::New(initEnv, [&](const Napi::CallbackInfo &info) -> Napi::Value { return info.Env().Undefined(); }));
exports.Set("deleteTun", Napi::Function::New(initEnv, [&](const Napi::CallbackInfo &info) -> Napi::Value { return info.Env().Undefined(); }));
exports.Set("checkTun", Napi::Function::New(initEnv, [&](const Napi::CallbackInfo &info) -> Napi::Value { return info.Env().Undefined(); }));
exports.Set("getTun", Napi::Function::New(initEnv, [&](const Napi::CallbackInfo &info) -> Napi::Value { return info.Env().Undefined(); }));
#endif

#ifdef SETCONFIG
exports.Set("setConfig", Napi::Function::New(initEnv, [&](const Napi::CallbackInfo &info) -> Napi::Value {
const Napi::Env env = info.Env();
Expand Down
12 changes: 6 additions & 6 deletions addons/tools/wginterface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,17 @@ class setConfig : public Napi::AsyncWorker {
if (sfw.IsNumber() && (sfw.ToNumber().Uint32Value() >= 0)) fwmark = sfw.ToNumber().Uint32Value();
else fwmark = -1;

const auto saddr = config.Get("Address");
if (saddr.IsArray()) {
const Napi::Array addrs = saddr.As<Napi::Array>();
const auto setAddress = config.Get("address");
if (setAddress.IsArray()) {
const Napi::Array addrs = setAddress.As<Napi::Array>();
for (unsigned int i = 0; i < addrs.Length(); i++) {
if (addrs.Get(i).IsString()) Address.push_back(addrs.Get(i).ToString().Utf8Value());
}
}

// Replace peers
const auto srpee = config.Get("replacePeers");
if (srpee.IsBoolean()) replacePeers = srpee.ToBoolean().Value();
const auto setReplace = config.Get("replacePeers");
if (setReplace.IsBoolean()) replacePeers = setReplace.ToBoolean().Value();

// Peers
const auto speers = config.Get("peers");
Expand Down Expand Up @@ -298,7 +298,7 @@ class getConfig : public Napi::AsyncWorker {
if (Address.size() > 0) {
const auto Addrs = Napi::Array::New(env);
for (auto &addr : Address) Addrs.Set(Addrs.Length(), addr);
config.Set("Address", Addrs);
config.Set("address", Addrs);
}

// Peer object
Expand Down
6 changes: 6 additions & 0 deletions binding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ sources:
- "addons/tools/wginterface.cpp"
- "addons/tools/wginterface-dummy.cpp"
target:
macos:
defines:
- USERSPACE_GO
linux:
sources:
- "addons/tools/linux/wireguard.c"
Expand All @@ -47,6 +50,9 @@ target:
flagsCC:
- "-fPIC"
windows:
flags:
- "-undefined"
- "dynamic_lookup"
target:
x86_64:
release: true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
},
"dependencies": {
"node-addon-api": "^7.1.0",
"rebory": "^0.1.11-2"
"rebory": "^0.1.12"
}
}
Loading

0 comments on commit b8b39f4

Please sign in to comment.