Skip to content

Commit

Permalink
Fix ip manipulation and update test files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirherobrine23 committed Aug 22, 2023
1 parent 18881cf commit b2e1c20
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 121 deletions.
7 changes: 6 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ updates:
- directory: /
package-ecosystem: "github-actions"
schedule:
interval: daily
interval: daily

- directory: /
package-ecosystem: npm
schedule:
interval: monthly
1 change: 1 addition & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ full-trace: true
recursive: true
parallel: false
timeout: 0
extension: [ "test.ts" ]
node-option:
- "loader=ts-node/register"
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wireguard-tools.js",
"version": "1.7.0",
"version": "1.7.1",
"description": "Control your wireguard interface from node.js, not a wireguard-tools wrapper!",
"main": "src/index.js",
"types": "src/index.d.ts",
Expand Down Expand Up @@ -34,15 +34,15 @@
},
"scripts": {
"install": "node libs/build.mjs",
"test": "mocha tests/**/*.ts",
"test": "mocha ./",
"prebuildify": "node libs/build.mjs build --auto",
"prepack": "tsc --build --clean && tsc --build && npm run prebuildify",
"postpack": "tsc --build --clean && node-gyp clean"
},
"devDependencies": {
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@types/node": "^20.5.0",
"@types/node": "^20.5.2",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
Expand Down
50 changes: 50 additions & 0 deletions src/interfaces.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { writeFileSync } from "fs";
import * as Bridge from "../src/mergeSets";
import * as utils from "../src/utils/index";
import { randomInt } from "crypto";
import { userInfo } from "os";

if (process.platform !== "win32" && (userInfo()).uid === 0) {
// Make base config
const interfaceName = String(((process.env.WG_INETRFACE||"").length > 0) ? process.env.WG_INETRFACE : (process.platform === "darwin" ? "utun" : "shtest").concat(String(randomInt(20, 1023))));
const deviceConfig: Bridge.wireguardInterface = {
portListen: randomInt(1024, 65535),
privateKey: utils.genPrivateKey(),
replacePeers: true,
Address: [],
peers: {}
};

// Create Random Peer
for (let i = 0; i < 20; i++) {
const peerKey = utils.keygen(true);
const ips = [utils.nodeCidr4.randomIp(Math.random() > 0.5 ? "192.168.15.1/24" : "10.0.0.1/8"), utils.nodeCidr4.randomIp(Math.random() > 0.5 ? "10.0.0.1/8" : "192.168.15.1/24")];
deviceConfig.peers[peerKey.public] = {
allowedIPs: ips.concat(ips.map(s => utils.nodeCidr4.toV6(s))),
removeMe: Math.random() > 0.7,
};
if (i%2 === 0) deviceConfig.peers[peerKey.public].presharedKey = peerKey.preshared;
ips.map(utils.nodeCidr4.fistIp).forEach(ip => {if (deviceConfig.Address) {if (!deviceConfig.Address.find(ips => ip === ips)) deviceConfig.Address.push(ip);}});
}

// Add IPv6 addresses
if (deviceConfig.Address) deviceConfig.Address.push(...deviceConfig.Address.map(s => utils.nodeCidr4.toV6(s)));

describe("Wireguard interface", () => {
it("Fist list", () => Bridge.listDevices());
it("Maneger", async () => {
await Bridge.addDevice(interfaceName, deviceConfig);
if (!((await Bridge.listDevices()).includes(interfaceName))) throw new Error("Invalid list devices");
});

it("Get config", async () => {
const raw = await Bridge.parseWgDevice(interfaceName);
writeFileSync(`${__dirname}/../${interfaceName}.addrs.json`, JSON.stringify({
deviceConfig,
raw
}, null, 2));
});

it("After list", async () => await Bridge.listDevices());
});
}
15 changes: 0 additions & 15 deletions tests/utils.ts → src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,6 @@ describe("Key Gen", () => {
});
});

// Shuffle IP and Convert to IPv6
describe("Random IP and IPv4 in to IPv6", () => {
it("Shuffle IP", () => {
const Ip = utils.nodeCidr4.randomIp("10.0.0.0/24");
if (/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/.test(Ip)) return;
throw new Error("Shuffle IP failed");
});
it("IPv6 Convert", () => {
const Ip = utils.nodeCidr4.randomIp("192.168.1.1/24");
const IPv6 = utils.nodeCidr6.FourToSix(Ip);
if (!/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/.test(IPv6)) return;
throw new Error("IPv6 conversion failed");
});
});

describe("Parse and Create config", function() {
it("Parse server config", () => {
utils.config.parseConfig(wg0);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
export * as config from "./config";

// IPs utils
export * as nodeCidr4 from "../lib/nodeCidr4";
export * as nodeCidr6 from "../lib/nodeCidr6";
export * as ip_manipulation from "./ipm";
export * as nodeCidr4 from "./ipm";

// Export types fist before export functions
export * from "./keygen";
29 changes: 29 additions & 0 deletions src/utils/ipm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { equal, match } from "assert";
import {
cidrCommonCidr, previousCidr, nextCidr,
randomIp, previousIp, nextIp, count,
toV6, fromV6,
} from "./ipm";

describe("IP Manipulation", function() {
// CIDR
it("cidrCommonCidr", () => equal(cidrCommonCidr(["192.168.1.1/24", "192.168.1.2/24", "192.168.1.3/24"]), "192.168.1.0/24"));
it("previousCidr", () => equal(previousCidr("192.168.1.1/24"), "192.168.0.1/24"));
it("nextCidr", () => equal(nextCidr("192.168.1.1/24"), "192.168.2.1/24"));

// IP
it("randomIp", () => match(randomIp("192.168.0.0/24"), /^192\.168\.0\.[0-9]+$/));
it("previousIp", () => equal(previousIp("192.168.0.0"), "192.167.255.255"));
it("nextIp", () => equal(nextIp("192.168.0.0"), "192.168.0.1"));
it("Count", () => equal(count("192.168.0.0/24"), 256));

// Convert
it("To IPv6", () => equal(toV6("192.178.66.255"), "0000:0000:0000:0000:0000:ffff:c0b2:42ff"));
it("From IPv6", () => equal(fromV6("0000:0000:0000:0000:0000:ffff:c0b2:42ff"), "192.178.66.255"));

it("From IPv6, compressedv6 false", () => equal(fromV6("0000:0000:0000:0000:0000:ffff:c0b2:42ff"), "192.178.66.255"));
it("To IPv6, compressedv6 false", () => equal(toV6("192.178.66.255", false), "0000:0000:0000:0000:0000:ffff:c0b2:42ff"));

it("To IPv6, compressedv6 true", () => equal(toV6("192.178.66.255", true), "::ffff:c0b2:42ff"));
it("From IPv6, compressedv6 true", () => equal(fromV6("::ffff:c0b2:42ff"), "192.178.66.255"));
});
Loading

0 comments on commit b2e1c20

Please sign in to comment.