Skip to content

Commit

Permalink
Update builder and migrate to ESM (#3)
Browse files Browse the repository at this point in the history
We are saying goodbye to node-gyp and commonjs, I am currently migrating several tools to ESM, and wireguard-tools.js was one of them.

I'm saying goodbye to node-gyp, it ended up becoming a difficult tool to use with wireguard-tools.js, wg.js uses several other programming languages, much more than C and C++, and rebory it brings new features that node-gyp doesn't have.

Reviewed-on: https://sirherobrine23.org/Wireguard/Wireguard-tools.js/pulls/3
  • Loading branch information
Sirherobrine23 committed Feb 17, 2024
1 parent 38dca1b commit 499cf79
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 525 deletions.
100 changes: 0 additions & 100 deletions .gitea/workflows/test.yml

This file was deleted.

92 changes: 15 additions & 77 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ jobs:
strategy:
matrix:
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
target_arch: [ "x86_64", "aarch64" ]
target_os:
- "linux"
# - "windows"
# - "macos"
steps:
- uses: actions/checkout@v4
name: Checkout

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

- uses: actions/setup-node@v4
name: Setup Node.js
with:
Expand All @@ -26,96 +36,24 @@ jobs:
run: |
export DEBIAN_FRONTEND=noninteractive
sudo apt update
sudo apt install -y binutils-multiarch gcc-*aarch64-linux-gnu gcc-*aarch64-linux-gnu-base g++-*aarch64-linux-gnu libc6-arm64-cross
sudo apt install -y "binutils-multiarch" "build-essential"
npm install --no-save --ignore-scripts
- name: Build addon
run: npm run prebuildify -- -v

- name: Test
run: sudo -E node --require ts-node/register --loader ts-node/esm ./src/index_test.ts
run: npm run dev -- --target_zig=${{ matrix.target_arch }}-${{ matrix.target_os }}

- name: Upload prebuilds interface
uses: actions/upload-artifact@v3
with:
retention-days: 7
name: prebuilds_${{ runner.os }}
name: prebuilds_${{ matrix.target_arch }}-${{ matrix.target_os }}_${{ matrix.node_version }}
path: "prebuilds/**"

macos_test:
runs-on: macos-latest
strategy:
matrix:
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
steps:
- uses: actions/checkout@v4
name: Checkout

- uses: actions/setup-node@v4
name: Setup Node.js
with:
node-version: ${{ matrix.node_version }}

- name: Setup Go environment
uses: actions/[email protected]

- name: Setup wireguard-go
run: |
cd ..
git clone https://git.zx2c4.com/wireguard-go
cd wireguard-go
echo "WG_INETRFACE=utun15" >> $GITHUB_ENV
go build -v -o "wireguard-go"
sudo ./wireguard-go utun15
- name: Install dependencies
run: npm install --no-save --ignore-scripts

- name: Build addon
run: npm run prebuildify -- -v

- name: Test
run: sudo -E node --require ts-node/register --loader ts-node/esm ./src/index_test.ts

- name: Upload prebuilds interface
uses: actions/upload-artifact@v3
with:
retention-days: 7
name: prebuilds_${{ runner.os }}
path: "prebuilds/**"

win_test:
runs-on: windows-latest
strategy:
matrix:
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
steps:
- uses: actions/checkout@v4
name: Checkout

- uses: actions/setup-node@v4
name: Setup Node.js
with:
node-version: ${{ matrix.node_version }}

- name: Install dependencies
run: npm install --no-save --ignore-scripts

- name: Build addon
run: npm run prebuildify -- -v

- name: Test
run: node --require ts-node/register --loader ts-node/esm ./src/index_test.ts

- name: Upload prebuilds interface
uses: actions/upload-artifact@v3
with:
retention-days: 7
name: prebuilds_${{ runner.os }}
path: "prebuilds/**"

pack_package:
needs: [ linux_test, macos_test, win_test ]
needs: linux_test
runs-on: ubuntu-latest
name: Pack npm package
env:
Expand All @@ -142,4 +80,4 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: Package_Pack
path: "*.tgz"
path: "*.tgz"
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"source.organizeImports": "explicit"
},
"files.exclude": {
"**/node_modules/": true,
"**/build/": true
"**/node_modules/": true
},
"terminal.integrated.env.windows": {
"PATH": "${workspaceFolder}/node_modules/.bin;${env:PATH}"
Expand Down
3 changes: 1 addition & 2 deletions addons/tools/wginterface-dummy.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <napi.h>
#include <wginterface.hh>
#include <net/if.h>

unsigned long maxName() {
return IFNAMSIZ;
return 16;
}

std::string versionDrive() {
Expand Down
29 changes: 15 additions & 14 deletions addons/tools/wginterface-win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <setupapi.h>
#include <cfgmgr32.h>
#include <devguid.h>
#include <ndisguid.h>
#include "wginterface.hh"
#include <wgkeys.hh>

Expand Down Expand Up @@ -61,22 +60,22 @@ std::string getErrorString(DWORD errorMessageID) {
return std::string("Error code: ").append(std::to_string(errorMessageID)).append(", Message: ").append(message);
}

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

HMODULE WireGuardDll = LoadLibraryExW(dllPath, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!WireGuardDll) return ((std::string)"Failed to initialize WireGuardNT, ").append(getErrorString(GetLastError()));;
if (!WireGuardDll) return std::string("Failed to initialize WireGuardNT, ").append(getErrorString(GetLastError()));;
#define X(Name) ((*(FARPROC *)&Name = GetProcAddress(WireGuardDll, #Name)) == NULL)
if (X(WireGuardCreateAdapter) || X(WireGuardOpenAdapter) || X(WireGuardCloseAdapter) || X(WireGuardGetAdapterLUID) || X(WireGuardGetRunningDriverVersion) || X(WireGuardDeleteDriver) || X(WireGuardSetLogger) || X(WireGuardSetAdapterLogging) || X(WireGuardGetAdapterState) || X(WireGuardSetAdapterState) || X(WireGuardGetConfiguration) || X(WireGuardSetConfiguration))
#undef X
{
DWORD LastError = GetLastError();
FreeLibrary(WireGuardDll);
SetLastError(LastError);
return ((std::string)"Failed to set Functions from WireGuardNT DLL, ").append(getErrorString(GetLastError()));;
return std::string("Failed to set Functions from WireGuardNT DLL, ").append(getErrorString(GetLastError()));;
}

return "";
Expand All @@ -89,10 +88,10 @@ std::string versionDrive() {
auto statusErr = GetLastError();
WireGuardCloseAdapter(Adapter);
if (statusErr == ERROR_FILE_NOT_FOUND) return "Driver not loaded";
return ((std::string)"Cannot get version drive, ").append(getErrorString(GetLastError()));
return std::string("Cannot get version drive, ").append(getErrorString(GetLastError()));
}
WireGuardCloseAdapter(Adapter);
return ((std::string)"WireGuardNT v").append(std::to_string((Version >> 16) & 0xff)).append(".").append(std::to_string((Version >> 0) & 0xff));
return std::string("WireGuardNT v").append(std::to_string((Version >> 16) & 0xff)).append(".").append(std::to_string((Version >> 0) & 0xff));
}

void listDevices::Execute() {
Expand Down Expand Up @@ -197,7 +196,7 @@ void getConfig::Execute() {
free(wg_iface);
if (GetLastError() != ERROR_MORE_DATA) return SetError((std::string("Failed get interface config, code: ")).append(std::to_string(GetLastError())));
wg_iface = (WIREGUARD_INTERFACE *)malloc(buf_len);
if (!wg_iface) return SetError(((std::string)"Failed get interface config, ").append(std::to_string(-errno)));
if (!wg_iface) return SetError(std::string("Failed get interface config, ").append(std::to_string(-errno)));
}

if (wg_iface->Flags & WIREGUARD_INTERFACE_FLAG::WIREGUARD_INTERFACE_HAS_PRIVATE_KEY) privateKey = wgKeys::toString(wg_iface->PrivateKey);
Expand Down Expand Up @@ -268,7 +267,8 @@ void setConfig::Execute() {
wgKeys::stringToKey(wg_peer->PublicKey, peerPublicKey);
} catch (std::string &err) {
SetError(err);
goto outEnd;
free(wg_iface);
return;
}
wg_peer->Flags = WIREGUARD_PEER_FLAG::WIREGUARD_PEER_HAS_PUBLIC_KEY;
wg_peer->AllowedIPsCount = 0;
Expand All @@ -284,7 +284,8 @@ void setConfig::Execute() {
wg_peer->Flags = (WIREGUARD_PEER_FLAG)(wg_peer->Flags|WIREGUARD_PEER_FLAG::WIREGUARD_PEER_HAS_PRESHARED_KEY);
} catch (std::string &err) {
SetError(err);
goto outEnd;
free(wg_iface);
return;
}
}

Expand All @@ -297,7 +298,8 @@ void setConfig::Execute() {
wg_peer->Flags = (WIREGUARD_PEER_FLAG)(wg_peer->Flags|WIREGUARD_PEER_FLAG::WIREGUARD_PEER_HAS_ENDPOINT);
} catch (std::string &err) {
SetError(std::string("Cannot parse endpoint, ").append(err));
goto outEnd;
free(wg_iface);
return;
}
}

Expand Down Expand Up @@ -325,7 +327,7 @@ void setConfig::Execute() {

WIREGUARD_ADAPTER_HANDLE Adapter = WireGuardOpenAdapter(toLpcwstr(wgName));
if (!Adapter) Adapter = WireGuardCreateAdapter(toLpcwstr(wgName), L"Wireguard-tools.js", NULL);
if (!Adapter) SetError(((std::string)"Failed to create adapter, ").append(getErrorString(GetLastError())));
if (!Adapter) SetError(std::string("Failed to create adapter, ").append(getErrorString(GetLastError())));
else if (!WireGuardSetConfiguration(Adapter, reinterpret_cast<WIREGUARD_INTERFACE*>(wg_iface), buf_len)) {
auto status = GetLastError();
SetError(std::string("Failed to set interface config, ").append(getErrorString(status)));
Expand Down Expand Up @@ -357,6 +359,5 @@ void setConfig::Execute() {
}
}
}
outEnd:
free(wg_iface);
}
2 changes: 1 addition & 1 deletion addons/tools/wginterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Napi::Object Init(Napi::Env initEnv, Napi::Object exports) {
/// Call Addon
#ifdef ONSTARTADDON
auto status = startAddon(initEnv);
auto status = startAddon(initEnv, exports);
if (status.length() >= 1) {
Napi::Error::New(initEnv, status).ThrowAsJavaScriptException();
return exports;
Expand Down
2 changes: 1 addition & 1 deletion addons/tools/wginterface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ unsigned long maxName();
std::string versionDrive();

// On start module call this function
std::string startAddon(const Napi::Env env);
std::string startAddon(const Napi::Env env, Napi::Object exports);

class deleteInterface : public Napi::AsyncWorker {
private:
Expand Down
Loading

0 comments on commit 499cf79

Please sign in to comment.