Skip to content

Commit

Permalink
Add delete interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirherobrine23 committed Aug 29, 2023
1 parent e8e6113 commit 6694822
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testProject.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
run: |
tsc --build --clean
tsc --build
sudo -E mocha ./
sudo -E mocha ./src
- name: Upload generate interface
uses: actions/upload-artifact@v3
Expand Down
11 changes: 3 additions & 8 deletions addons/tools/wginterface-dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ unsigned long maxName() {
}

void listDevices::Execute() {}

void setConfig::Execute() {
SetError("Use userpace implementation, kernel only on linux!");
}

void getConfig::Execute() {
SetError("Use userpace implementation, kernel only on linux!");
}
void deleteInterface::Execute() {}
void setConfig::Execute() {}
void getConfig::Execute() {}
47 changes: 24 additions & 23 deletions addons/tools/wginterface-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,15 @@ extern "C" {
#include "linux/wireguard.h"
}

#ifndef SETCONFIG
#define SETCONFIG
#endif
#ifndef GETCONFIG
#define GETCONFIG
#endif
#ifndef LISTDEV
#define LISTDEV
#endif
#define SETCONFIG 1
#define GETCONFIG 1
#define LISTDEV 1
#define DELIFACE 1

unsigned long maxName() {
return IFNAMSIZ;
}

Napi::Value listDevicesSync(const Napi::CallbackInfo& info) {
const Napi::Env env = info.Env();
size_t len;
char *device_name, *devicesList = wg_list_device_names();
if (!devicesList) {
Napi::Error::New(env, "Unable to get device names").ThrowAsJavaScriptException();
return env.Undefined();
}
const Napi::Array devicesArray = Napi::Array::New(env);
for ((device_name) = (devicesList), (len) = 0; ((len) = strlen(device_name)); (device_name) += (len) + 1) devicesArray.Set(devicesArray.Length(), Napi::String::New(env, device_name));
free(devicesList);
return devicesArray;
}

void listDevices::Execute() {
char *device_name, *devicesList = wg_list_device_names();
if (!devicesList) SetError("Unable to get device names");
Expand Down Expand Up @@ -85,6 +66,26 @@ int setInterface(std::string wgName) {
return len;
}

void deleteInterface::Execute() {
size_t len = 0;
char *device_name, *devicesList = wg_list_device_names();
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);
}
break;
}
}
free(devicesList);
}
}

void setConfig::Execute() {
int res = setInterface(wgName);
if (res < 0) {
Expand Down
32 changes: 31 additions & 1 deletion addons/tools/wginterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ Napi::Value setConfigAsync(const Napi::CallbackInfo &info) {
return env.Undefined();
}

Napi::Value deleteInterfaceAsync(const Napi::CallbackInfo &info) {
const Napi::Env env = info.Env();
const auto wgName = info[0];
const auto callback = info[1];
if (!(wgName.IsString())) {
Napi::Error::New(env, "Require wireguard interface name").ThrowAsJavaScriptException();
return env.Undefined();
} else if (wgName.ToString().Utf8Value().length() >= maxName()) {
Napi::Error::New(env, "interface name is so long").ThrowAsJavaScriptException();
return env.Undefined();
} else if (!(callback.IsFunction())) {
Napi::Error::New(env, "Require callback").ThrowAsJavaScriptException();
return env.Undefined();
}

try {
const auto delInterWorker = new deleteInterface(callback.As<Napi::Function>(), wgName.ToString().Utf8Value());
delInterWorker->Queue();
} catch (const Napi::Error &err) {
err.ThrowAsJavaScriptException();
}
return env.Undefined();
}

Napi::Value getConfigAsync(const Napi::CallbackInfo &info) {
const Napi::Env env = info.Env();
const auto wgName = info[0];
Expand Down Expand Up @@ -78,13 +102,19 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
// Constants
exports.Set("constants", constants);

// async function
// Function's
#ifdef SETCONFIG
exports.Set("setConfigAsync", Napi::Function::New(env, setConfigAsync));
#endif

#ifdef DELIFACE
exports.Set("deleteInterfaceAsync", Napi::Function::New(env, deleteInterfaceAsync));
#endif

#ifdef GETCONFIG
exports.Set("getConfigAsync", Napi::Function::New(env, getConfigAsync));
#endif

#ifdef LISTDEV
exports.Set("listDevicesAsync", Napi::Function::New(env, listDevicesAsync));
#endif
Expand Down
22 changes: 17 additions & 5 deletions addons/tools/wginterface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@

unsigned long maxName();

/*
Esta função consegela o loop event
class deleteInterface : public Napi::AsyncWorker {
private:
std::string wgName;
public:
deleteInterface(const Napi::Function &callback, std::string name): AsyncWorker(callback), wgName(name) {}
~deleteInterface() {}
void OnOK() override {
Napi::HandleScope scope(Env());
Callback().Call({ Env().Undefined() });
};
void OnError(const Napi::Error &e) override {
Napi::HandleScope scope(Env());
Callback().Call({ e.Value() });
}

Pegar todas as interfaces do Wireguard e retorna em forma de String sendo tratado pelo Node.js quando retornado.
*/
Napi::Value listDevicesSync(const Napi::CallbackInfo& info);
// Set platform Execute script
void Execute() override;
};

class listDevices : public Napi::AsyncWorker {
private:
Expand Down
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"defines": [
"LISTDEV",
"GETCONFIG",
"SETCONFIG"
"SETCONFIG",
"DELIFACE"
],
"sources": [
"addons/tools/linux/wireguard.c",
Expand Down
12 changes: 5 additions & 7 deletions examples/addPeer/index.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { listDevices, parseWgDevice, addDevice, utils } from "wireguard-tools.js";
import { listDevices, parseWgDevice, addDevice, wgConfig, utils } from "wireguard-tools.js";
const deviceName = (await listDevices()).at(-1);
if (!deviceName) {
console.info("Create wireguard interface");
process.exit(-1);
}

// Get current config
const currentConfig = await parseWgDevice(deviceName);
const currentConfig = new wgConfig(await parseWgDevice(deviceName));

// Create Peer
const peerKey = await utils.keygenAsync(true);
currentConfig.peers[peerKey.public] = { presharedKey: peerKey.preshared, allowedIPs: [ utils.ipManipulation.randomIp("10.22.66.1/24", Object.keys(currentConfig.peers).map(k => currentConfig.peers[k].allowedIPs).flat(2).filter(Boolean)) ] };
currentConfig.peers[peerKey.public].allowedIPs.push(utils.ipManipulation.toV6(currentConfig.peers[peerKey.public].allowedIPs.at(0)));
console.log("Add %O peer", peerKey.public);
const peer = await currentConfig.newPeer(true);
console.log("Add %O peer", peer.publicKey);

// Set to interface
await addDevice(deviceName, currentConfig);
console.dir(currentConfig, { colors: true, depth: null });
console.dir(currentConfig, { depth: null });
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"scripts": {
"install": "node libs/build.mjs",
"test": "mocha ./",
"test": "mocha ./src",
"dev": "tsc --build --clean && tsc --build && node libs/build.mjs build",
"prebuildify": "node libs/build.mjs build --auto",
"prepack": "tsc --build --clean && tsc --build && npm run prebuildify",
Expand Down
1 change: 1 addition & 0 deletions src/wginterface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ if (process.platform !== "win32" && (userInfo()).gid === 0) {
});

it("After list", async () => await Bridge.listDevices());
it("Delete", async () => await Bridge.deleteInterface(interfaceName));
});
}
12 changes: 11 additions & 1 deletion src/wginterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class wgConfig extends Map<string, peerConfig> {
allowedIPs: [IPv4, IPv6],
presharedKey: withPreshared ? peerKey.preshared : undefined
});
return this.get(peerKey.public);
return Object.assign({}, { publicKey: peerKey.public }, this.get(peerKey.public));
}

toJSON(): wireguardInterface {
Expand Down Expand Up @@ -178,6 +178,16 @@ export async function getConfig(deviceName: string): Promise<wireguardInterface>
return config.toJSON();
}

/**
* Delete wg interface if possible
* @param deviceName - Wireguard interface name.
* @returns
*/
export async function deleteInterface(deviceName: string): Promise<void> {
if (typeof wg_binding.deleteInterfaceAsync === "function") return promisify(wg_binding.deleteInterfaceAsync)(deviceName);
return fs.rm(path.join(defaultPath, deviceName).concat(".sock"), { force: true });
}

/**
* Set Wireguard interface config.
* @param deviceName - Wireguard interface name.
Expand Down

0 comments on commit 6694822

Please sign in to comment.