Skip to content

Commit

Permalink
Clean code for userspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirherobrine23 committed Jul 11, 2024
1 parent f9a4e00 commit da28427
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 39 deletions.
15 changes: 5 additions & 10 deletions .zed/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,25 @@
"formatter": "auto",
"format_on_save": "off",
"tab_size": 2,
"file_scan_exclusions": [
"/*-lock",
"/node_modules",
"/build"
],
"languages": {
"TypeScript": {
"hard_tabs": false
"format_on_save": true,
"hard_tabs": false,
"format_on_save": "on",
"code_actions_on_format": {
"source.organizeImports": true,
"source.removeUnusedImports": true
}
},
"JavaScript": {
"hard_tabs": false
"format_on_save": true,
"hard_tabs": false,
"format_on_save": "on",
"code_actions_on_format": {
"source.organizeImports": true,
"source.removeUnusedImports": true
}
},
"Go": {
"format_on_save": true
"format_on_save": "on"
},
"C++": {
"hard_tabs": false
Expand Down
8 changes: 5 additions & 3 deletions addon/genKey/wgkeys.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "wgkeys.hh"
#include <cstdio>
#include <errno.h>
#include <fcntl.h>
#include <iostream>
Expand Down Expand Up @@ -330,15 +331,16 @@ std::string wgKeys::toHex(const std::string &keyBase64) {
wg_key key;
wgKeys::stringToKey(key, keyBase64);
char hex[65];
for (int i = 0; i < 32; ++i) sprintf(hex + i * 2, "%02x", key[i]);
// for (int i = 0; i < 32; ++i) sprintf(hex + i * 2, "%02x", key[i]);
for (int i = 0; i < 32; ++i) snprintf(hex + i * 2, 3, "%02x", key[i]);
hex[64] = '\0';
return std::string(hex);
}

std::string wgKeys::HextoBase64(const std::string &s_hex) {
wg_key key;
for(unsigned i = 0, uchr ; i < s_hex.length() ; i += 2) {
sscanf( s_hex.c_str()+ i, "%2x", &uchr); // conversion
for(unsigned i = 0, uchr; i < s_hex.length(); i += 2) {
sscanf(s_hex.c_str() + i, "%2x", &uchr); // conversion
key[i/2] = uchr; // save as char
}
return wgKeys::toString(key);
Expand Down
7 changes: 3 additions & 4 deletions addon/userspace/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import (

func main() {}

// Default log level to print in terminal
const levelLog = device.LogLevelSilent

func getCharErr(err error) *C.char {
if err == nil {
return C.CString("")
Expand Down Expand Up @@ -63,10 +60,12 @@ func stopWg(wgName *C.char) (bool, *C.char) {
tunName := C.GoString(wgName)
if dev, ok := Devices[tunName]; ok {
dev.Close()
delete(Devices, tunName)
if tun, ok := Tuns[tunName]; ok {
if err := tun.Close(); err != nil {
return false, getCharErr(err)
}
delete(Tuns, tunName)
}
return true, nil
}
Expand All @@ -76,7 +75,7 @@ func stopWg(wgName *C.char) (bool, *C.char) {
// Set config in tun, if not exist create and add to Tuns
//
//export setWg
func setWg(wgName, wgConfig *C.char) *C.char {
func setWg(levelLog int, wgName, wgConfig *C.char) *C.char {
tunName, configString := C.GoString(wgName), C.GoString(wgConfig)
_, okTuns := Tuns[tunName]
_, okDev := Devices[tunName]
Expand Down
4 changes: 2 additions & 2 deletions addon/userspace/wginterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void IpManeger::GetInInterface(std::string interfaceName) {}
std::string driveLoad(std::map<std::string, std::string> load) { return ""; }

std::string getWireguardVersion() {
return std::string(wgVersion());
return std::string("wireguard-go ").append(wgVersion());
}

void WireguardDevices::getInterfaces() {
Expand Down Expand Up @@ -68,7 +68,7 @@ void WireguardConfig::setWireguardConfig() {
}
}

std::string err = setWg((char*)this->name.c_str(), (char*)userspaceConfig.append("\n").c_str());
std::string err = setWg(0, (char*)this->name.c_str(), (char*)userspaceConfig.append("\n").c_str());
if (!err.empty()) throw err;
}

Expand Down
19 changes: 0 additions & 19 deletions addon/wginterface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -215,23 +215,4 @@ class WireguardConfig {
void getWireguardConfig();
};

namespace WireguardUserspace {
// Get Wireguard-go version
std::string getWireguardVersion();

// Close all wireguard tunels
void closeAllWireguardTunnels();

// Create tunel and return path to tunel
std::string createWireguardTunnel(std::string wgName);

// Delete tunel by name and return true if success
void deleteWireguardTunnel(std::string wgName);

// List all tunels
std::vector<std::string> listTunnels();

// Check if tunel exist
bool checkIfExistTunnel(std::string wgName);
};
#endif
3 changes: 2 additions & 1 deletion compile_flags.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-std=c++17
-Iaddon/
-Iaddon/genKey
-Inode_modules/node-addon-api
-I/usr/local/include/node
-std=c++17
-I/opt/local/include/node

0 comments on commit da28427

Please sign in to comment.