Skip to content

Commit

Permalink
Migrate tests to node test and Add key gen in node
Browse files Browse the repository at this point in the history
I'm adding key generation to pure Javascript, port of `keygen-html`.

I'm also migrating the mocha tests to the Node tests runner.

I solved a typescript typing problem

add gitea CI/CD

Gitlab CI/CD and Github Actions update test
  • Loading branch information
Sirherobrine23 committed Jan 14, 2024
1 parent f23fa9e commit 23afbe6
Show file tree
Hide file tree
Showing 19 changed files with 497 additions and 126 deletions.
89 changes: 89 additions & 0 deletions .gitea/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Test
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
linux_test:
runs-on: ubuntu-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: |
echo "Host arch: $(uname -m)"
export DEBIAN_FRONTEND=noninteractive
sudo apt update
packages=( "binutils-multiarch" );
if [[ $(uname -m) == "x86_64" ]];then
packages+=( "gcc-*aarch64-linux-gnu" "gcc-*aarch64-linux-gnu-base" "g++-*aarch64-linux-gnu" "libc6-arm64-cross" )
else
packages+=( "libc6-amd64-cross" "g++-*x86-64-linux-gnu" "g++-*x86-64-linux-gnux32" "gcc-*x86-64-linux-gnu" "gcc-*x86-64-linux-gnux32" )
if [[ $(uname -m) != "aarch64" ]];then
packages+=( "gcc-*aarch64-linux-gnu" "gcc-*aarch64-linux-gnu-base" "g++-*aarch64-linux-gnu" "libc6-arm64-cross" )
fi
fi
sudo apt install -y ${packages[@]}
npm install --no-save --ignore-scripts
- name: Build addon
run: npm run prebuildify -- -v

- name: Test
run: sudo -E "$(command -v node)" --require ts-node/register --loader ts-node/esm ./src/index_test.ts

- name: Upload generate interface
uses: actions/upload-artifact@v3
with:
name: addrs_${{ runner.os }}_${{ matrix.node_version }}
path: "*.addrs.json"

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

pack_package:
needs: linux_test
runs-on: ubuntu-latest
name: Pack npm package
env:
PACKAGE_VERSION: ${{ github.ref }}
steps:
- uses: actions/checkout@v4
name: Code checkout

- uses: actions/setup-node@v4
name: Setup node.js
with:
node-version: 20.x
registry-url: https://registry.npmjs.org/

- name: Download all artefacts
uses: actions/download-artifact@v3
with:
path: ./prebuilds

- run: npm install --no-save --ignore-scripts
- run: npm pack

- name: Upload npm package
uses: actions/upload-artifact@v3
with:
name: Package_Pack
path: "*.tgz"
21 changes: 15 additions & 6 deletions .github/workflows/testProject.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node_version: [ 16.x, 17.x, 18.x, 19.x, 20.x ]
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
steps:
- uses: actions/checkout@v4
name: Checkout
Expand All @@ -29,8 +29,11 @@ jobs:
sudo apt install -y binutils-multiarch gcc-*aarch64-linux-gnu gcc-*aarch64-linux-gnu-base g++-*aarch64-linux-gnu libc6-arm64-cross
npm install --no-save --ignore-scripts
- name: Build addon
run: npm run prebuildify -- -v

- name: Test
run: npm run prebuildify -- -v && sudo -E ./node_modules/.bin/mocha ./src
run: sudo -E node --require ts-node/register --loader ts-node/esm ./src/index_test.ts

- name: Upload generate interface
uses: actions/upload-artifact@v3
Expand All @@ -49,7 +52,7 @@ jobs:
runs-on: macos-latest
strategy:
matrix:
node_version: [ 16.x, 17.x, 18.x, 19.x, 20.x ]
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
steps:
- uses: actions/checkout@v4
name: Checkout
Expand All @@ -74,8 +77,11 @@ jobs:
- name: Install dependencies
run: npm install --no-save --ignore-scripts

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

- name: Test
run: npm run prebuildify -- -v && sudo -E ./node_modules/.bin/mocha ./src
run: sudo -E node --require ts-node/register --loader ts-node/esm ./src/index_test.ts

- name: Upload generate interface
uses: actions/upload-artifact@v3
Expand All @@ -94,7 +100,7 @@ jobs:
runs-on: windows-latest
strategy:
matrix:
node_version: [ 16.x, 17.x, 18.x, 19.x, 20.x ]
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
steps:
- uses: actions/checkout@v4
name: Checkout
Expand All @@ -107,8 +113,11 @@ jobs:
- name: Install dependencies
run: npm install --no-save --ignore-scripts

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

- name: Test
run: npm run prebuildify -- -v && ./node_modules/.bin/mocha ./src
run: node --require ts-node/register --loader ts-node/esm ./src/index_test.ts

- name: Upload generate interface
uses: actions/upload-artifact@v3
Expand Down
27 changes: 21 additions & 6 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
image: node:lts
test:
only:
- main
stage: test
image: node:latest
script:
- npm i -g typescript ts-node
- npm install --no-save && npm i -g mocha
- npm run build
- mocha 'tests/**/*.ts'
- |
echo "Host arch: $(uname -m)"
export DEBIAN_FRONTEND=noninteractive
sudo apt update
packages=( "binutils-multiarch" );
if [[ $(uname -m) == "x86_64" ]];then
packages+=( "gcc-*aarch64-linux-gnu" "gcc-*aarch64-linux-gnu-base" "g++-*aarch64-linux-gnu" "libc6-arm64-cross" )
else
packages+=( "libc6-amd64-cross" "g++-*x86-64-linux-gnu" "g++-*x86-64-linux-gnux32" "gcc-*x86-64-linux-gnu" "gcc-*x86-64-linux-gnux32" )
if [[ $(uname -m) != "aarch64" ]];then
packages+=( "gcc-*aarch64-linux-gnu" "gcc-*aarch64-linux-gnu-base" "g++-*aarch64-linux-gnu" "libc6-arm64-cross" )
fi
fi
sudo apt install -y ${packages[@]}
- npm install --no-save --ignore-scripts
- npm run prebuildify -- -v
- |
sudo=""
if command -v sudo; then sudo="sudo" fi
$sudo node --require ts-node/register --loader ts-node/esm ./src/index_test.ts
8 changes: 0 additions & 8 deletions .mocharc.yml

This file was deleted.

1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules/
/*.tgz

# Typescript
src/**/*_test.*
src/**/*.ts
!src/**/*.d.ts

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ other tools are wrappers over `wg`, `wireguard-tools.js` is not like that, it is

- Userspace [(wireguard-go)](https://git.zx2c4.com/wireguard-go/about/) support.
- Maneger wireguard interface (linux and windows create if not exist's).
- Generate `pre-shared`, `private` and `public` keys.
- Generate `preshared`, `private` and `public` keys.
- [wg-quick](https://man7.org/linux/man-pages/man8/wg-quick.8.html) file support.
- More info and example check [`docs`](docs/README.md) folder.
- More info and example check [`wiki`](https://sirherobrine23.org/Wireguard/Wireguard-tools.js/wiki).

> **Note**
>
> we have pre-copiled files for:
> - `Windows`: x64, arm64
> - `MacOS`: x64/intel, arm64
> - `Linux`: x64/amd64, arm64
> - `MacOS`: x64/intel, arm64/Apple silicon
> - `Linux`: x64/amd64, arm64/aarch64
>
> else arch and system require copiler supported by `node-gyp` installed to compile Node addon.
>
Expand Down
18 changes: 11 additions & 7 deletions addons/tools/wginterface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class getConfig : public Napi::AsyncWorker {
std::string publicKey;

// Wireguard port listen
uint32_t portListen;
unsigned int portListen;

// FirewallMark specifies a device's firewall mark
// else set to 0, the firewall mark will be cleared.
Expand Down Expand Up @@ -305,24 +305,28 @@ class getConfig : public Napi::AsyncWorker {
const auto PeersObject = Napi::Object::New(env);
for (auto &peer : peersVector) {
const auto PeerObject = Napi::Object::New(env);
const std::string peerPubKey = peer.first;
auto peerConfig = peer.second;

if (peerConfig.presharedKey.length() == B64_WG_KEY_LENGTH) PeerObject.Set("presharedKey", peerConfig.presharedKey);
if (peerConfig.keepInterval > 0 && peerConfig.keepInterval <= 65535) PeerObject.Set("keepInterval", peerConfig.keepInterval);
if (peerConfig.endpoint.length() > 0) PeerObject.Set("endpoint", peerConfig.endpoint);
if (peerConfig.last_handshake >= 0) PeerObject.Set("lastHandshake", Napi::Date::New(env, peerConfig.last_handshake));
// if (peerConfig.last_handshake >= 0) PeerObject.Set("lastHandshake", peerConfig.last_handshake);
if (peerConfig.rxBytes >= 0) PeerObject.Set("rxBytes", peerConfig.rxBytes);
if (peerConfig.txBytes >= 0) PeerObject.Set("txBytes", peerConfig.txBytes);
if (peerConfig.rxBytes >= 0) PeerObject.Set("rxBytes", Napi::BigInt::New(env, (uint64_t)peerConfig.rxBytes));
if (peerConfig.txBytes >= 0) PeerObject.Set("txBytes", Napi::BigInt::New(env, (uint64_t)peerConfig.txBytes));
if (peerConfig.last_handshake >= 0) {
PeerObject.Set("lastHandshake", Napi::Date::New(env, peerConfig.last_handshake));
PeerObject.Set("lastHandshakeBigint", peerConfig.last_handshake); // Debug to windows
}
if (peerConfig.allowedIPs.size() > 0) {
const auto allowedIPs = Napi::Array::New(env);
for (auto &ip : peerConfig.allowedIPs) allowedIPs.Set(allowedIPs.Length(), ip);
PeerObject.Set("allowedIPs", allowedIPs);
}

PeersObject.Set(peerPubKey, PeerObject);
// const std::string peerPubKey = peer.first;
PeersObject.Set(peer.first, PeerObject);
}

// Set peers to object
config.Set("peers", PeersObject);

// Resolve config json
Expand Down
17 changes: 14 additions & 3 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import __wg from "./src/index.js";
export const { key, wgQuick, wginterface, constants, deleteInterface, getConfig, listDevices, setConfig } = __wg;
export default __wg["default"]||__wg;
import * as __wg from "./src/index.js";
export const {
constants,
key,
key_experimental,
wgQuick,
wginterface,
getConfig,
setConfig,
listDevices,
deleteInterface,
} = __wg;

export default __wg.default;
21 changes: 10 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wireguard-tools.js",
"version": "1.8.1",
"description": "Control your wireguard interface from node.js, not a wireguard-tools wrapper!",
"description": "Control your wireguard interface from node.js, not a wireguard-tools/wg wrapper!",
"private": false,
"type": "commonjs",
"main": "./src/index.js",
Expand All @@ -14,12 +14,12 @@
"types": "./src/index.d.ts"
}
},
"homepage": "https://github.com/Sirherobrine23/Wireguard-tools.js#readme",
"homepage": "https://sirherobrine23.org/Wireguard/Wireguard-tools.js#readme",
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)",
"license": "GPL-3.0-or-later",
"repository": {
"type": "git",
"url": "git+https://github.com/Sirherobrine23/Wireguard-tools.js.git"
"url": "git+https://sirherobrine23.org/Wireguard/Wireguard-tools.js.git"
},
"keywords": [
"wireguard",
Expand All @@ -29,7 +29,7 @@
"wireguard-utils"
],
"bugs": {
"url": "https://github.com/Sirherobrine23/Wireguard-tools.js/issues/new"
"url": "https://sirherobrine23.org/Wireguard/Wireguard-tools.js/issues/new"
},
"sponsor": {
"url": "https://github.com/sponsors/Sirherobrine23"
Expand All @@ -42,20 +42,19 @@
},
"scripts": {
"install": "node libs/build.mjs",
"test": "node libs/build.mjs build --clean && mocha ./testPackage.test.cjs",
"test": "node libs/build.mjs build --clean && node --require ts-node/register --loader ts-node/esm --test src/**/*_test.ts",
"dev": "node libs/build.mjs build",
"prebuildify": "node libs/build.mjs build --auto",
"prepack": "tsc --build --clean && tsc --build && node libs/build.mjs",
"postpack": "tsc --build --clean"
},
"devDependencies": {
"@types/node": "^20.8.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
"@types/node": "^20.11.0",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"dependencies": {
"node-addon-api": "^7.0.0",
"node-gyp": "^10.0.0"
"node-gyp": "^10.0.1"
}
}
}
Loading

0 comments on commit 23afbe6

Please sign in to comment.