Skip to content

Commit

Permalink
Merge userpsace and wg_binding in wginterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirherobrine23 committed Aug 28, 2023
1 parent 11594ff commit e8e6113
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 168 deletions.
21 changes: 2 additions & 19 deletions addons/tools/wginterface-dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,11 @@
#include <net/if.h>
#endif


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

Napi::Value listDevicesSync(const Napi::CallbackInfo& info) {
const Napi::Env env = info.Env();
Napi::Error::New(env, "Use userpace implementation, kernel only on linux!").ThrowAsJavaScriptException();
return env.Undefined();
}

Napi::Value setupInterfaceSync(const Napi::CallbackInfo& info) {
const Napi::Env env = info.Env();
Napi::Error::New(env, "Use userpace implementation, kernel only on linux!").ThrowAsJavaScriptException();
return env.Undefined();
}

Napi::Value parseWgDeviceSync(const Napi::CallbackInfo& info) {
const Napi::Env env = info.Env();
Napi::Error::New(env, "Use userpace implementation, kernel only on linux!").ThrowAsJavaScriptException();
return env.Undefined();
}
void listDevices::Execute() {}

void setConfig::Execute() {
SetError("Use userpace implementation, kernel only on linux!");
Expand Down
23 changes: 21 additions & 2 deletions addons/tools/wginterface-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@
#include <sys/types.h>
#include "wginterface.hh"
#include "linux/set_ip.cpp"

extern "C" {
#include "linux/wireguard.h"
}

int maxName() {
#ifndef SETCONFIG
#define SETCONFIG
#endif
#ifndef GETCONFIG
#define GETCONFIG
#endif
#ifndef LISTDEV
#define LISTDEV
#endif

unsigned long maxName() {
return IFNAMSIZ;
}

Expand All @@ -47,6 +56,16 @@ Napi::Value listDevicesSync(const Napi::CallbackInfo& info) {
return devicesArray;
}

void listDevices::Execute() {
char *device_name, *devicesList = wg_list_device_names();
if (!devicesList) SetError("Unable to get device names");
else {
size_t len;
for ((device_name) = (devicesList), (len) = 0; ((len) = strlen(device_name)); (device_name) += (len) + 1) deviceNames.push_back(device_name);
free(devicesList);
}
}

int setInterface(std::string wgName) {
size_t len = 0;
char *device_name, *devicesList = wg_list_device_names();
Expand Down
28 changes: 25 additions & 3 deletions addons/tools/wginterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Napi::Value setConfigAsync(const Napi::CallbackInfo &info) {
}
return env.Undefined();
}

Napi::Value getConfigAsync(const Napi::CallbackInfo &info) {
const Napi::Env env = info.Env();
const auto wgName = info[0];
Expand All @@ -53,6 +54,23 @@ Napi::Value getConfigAsync(const Napi::CallbackInfo &info) {
return env.Undefined();
}

Napi::Value listDevicesAsync(const Napi::CallbackInfo &info) {
const Napi::Env env = info.Env();
const auto callback = info[0];
if (!(callback.IsFunction())) {
Napi::Error::New(env, "Require callback").ThrowAsJavaScriptException();
return env.Undefined();
}

try {
const auto devicesFind = new listDevices(callback.As<Napi::Function>());
devicesFind->Queue();
} catch (const Napi::Error &err) {
err.ThrowAsJavaScriptException();
}
return env.Undefined();
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
const Napi::Object constants = Napi::Object::New(env);
constants.Set("MAX_NAME_LENGTH", maxName());
Expand All @@ -61,11 +79,15 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set("constants", constants);

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

// Sync function lock loop
exports.Set("listDevicesSync", Napi::Function::New(env, listDevicesSync));
#endif
#ifdef LISTDEV
exports.Set("listDevicesAsync", Napi::Function::New(env, listDevicesAsync));
#endif
return exports;
}
NODE_API_MODULE(addon, Init);
27 changes: 23 additions & 4 deletions addons/tools/wginterface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <vector>
#include <map>

int maxName();
unsigned long maxName();

/*
Esta função consegela o loop event
Expand All @@ -13,6 +13,28 @@ Pegar todas as interfaces do Wireguard e retorna em forma de String sendo tratad
*/
Napi::Value listDevicesSync(const Napi::CallbackInfo& info);

class listDevices : public Napi::AsyncWorker {
private:
std::vector<std::string> deviceNames;
public:
~listDevices() {}
listDevices(const Napi::Function &callback) : AsyncWorker(callback) {}
void OnOK() override {
Napi::HandleScope scope(Env());
const Napi::Env env = Env();
const auto deviceArray = Napi::Array::New(env);
if (deviceNames.size() > 0) {
for (auto it = deviceNames.begin(); it != deviceNames.end(); ++it) deviceArray.Set(deviceArray.Length(), it->append(""));
}
Callback().Call({ Env().Undefined(), deviceArray });
};
void OnError(const Napi::Error& e) override {
Napi::HandleScope scope(Env());
Callback().Call({ e.Value() });
}
void Execute() override;
};

class Peer {
public:
bool removeMe;
Expand Down Expand Up @@ -163,9 +185,6 @@ class getConfig : public Napi::AsyncWorker {
// Interface address'es
std::vector<std::string> Address;

// Replace peers
bool replacePeers;

/*
Wireguard peers
Map: <publicKey, Peer>
Expand Down
18 changes: 15 additions & 3 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
}],
["OS=='mac'", {
"xcode_settings": {
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
"CLANG_CXX_LIBRARY": "libc++",
"MACOSX_DEPLOYMENT_TARGET": "10.7",
"GCC_ENABLE_CPP_EXCEPTIONS": "YES"
},
}],
],
Expand Down Expand Up @@ -61,11 +59,25 @@
],
"conditions": [
["OS=='linux'", {
"defines": [
"LISTDEV",
"GETCONFIG",
"SETCONFIG"
],
"sources": [
"addons/tools/linux/wireguard.c",
"addons/tools/wginterface-linux.cpp"
]
}],
["OS=='mac'", {
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"cflags_cc": [ "-fexceptions" ],
"cflags": [ "-fexceptions" ],
"xcode_settings": {
"GCC_ENABLE_CPP_EXCEPTIONS": "YES"
},
}],
["OS!='linux'", {
"sources": [
"addons/tools/wginterface-dummy.cpp"
Expand Down
4 changes: 2 additions & 2 deletions libs/prebuildifyLoad.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = main;
*/
function main(name, pathLocation) {
if (!pathLocation) pathLocation = process.cwd();
else pathLocation = path.resolve(pathLocation);
else pathLocation = path.resolve(process.cwd(), pathLocation);
const folders = [
path.join(pathLocation, "build", "Release"),
path.join(pathLocation, "build", "Debug"),
Expand All @@ -23,7 +23,7 @@ function main(name, pathLocation) {
if (typeof name === "number") return require(path.join(folder, files.at(name)));
else if (!name) name = files.at(0);
if (typeof name === "string") {
const bname = name;
const bname = name.concat("");
if ((name = files.find(s => s.startsWith(name)))) return require(path.join(folder, name));
name = bname;
}
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * as userspace from "./userspace";
export * as utils from "./utils/index";
export * from "./mergeSets";
export * from "./wginterface";
55 changes: 0 additions & 55 deletions src/mergeSets.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { wireguardInterface } from "../userspace";
import { wireguardInterface } from "../wginterface";

/**
* Create wg-quick config file
Expand Down
35 changes: 0 additions & 35 deletions src/wg_binding.ts

This file was deleted.

14 changes: 8 additions & 6 deletions src/interfaces.test.ts → src/wginterface.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { randomInt } from "crypto";
import { writeFileSync } from "fs";
import { userInfo } from "os";
import * as Bridge from "../src/mergeSets";
import * as utils from "../src/utils/index";
import * as Bridge from "./wginterface";
import * as utils from "./utils/index";

if (process.platform !== "win32" && (userInfo()).gid === 0) {
// Make base config
Expand Down Expand Up @@ -33,16 +33,18 @@ if (process.platform !== "win32" && (userInfo()).gid === 0) {
describe(`Wireguard interface (${interfaceName})`, () => {
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");
await Bridge.setConfig(interfaceName, deviceConfig);
if (!((await Bridge.listDevices()).some(s => s.name === interfaceName))) throw new Error("Invalid list devices");
});

it("Get config", async () => {
const raw = await Bridge.parseWgDevice(interfaceName);
const raw = await Bridge.getConfig(interfaceName);
let keyNot: string;
writeFileSync(`${__dirname}/../${interfaceName}.addrs.json`, JSON.stringify({
raw,
deviceConfig,
raw
}, null, 2));
if (Object.keys(deviceConfig.peers).some(s => !(deviceConfig.peers[s].removeMe) && !(raw.peers[(keyNot = s)]))) throw new Error(("Invalid return config, key not exists: ").concat(keyNot));
});

it("After list", async () => await Bridge.listDevices());
Expand Down
Loading

0 comments on commit e8e6113

Please sign in to comment.