-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ip manipulation and update test files
- Loading branch information
1 parent
18881cf
commit 1719adf
Showing
11 changed files
with
236 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
}); |
Oops, something went wrong.