Big code refactoring #10

Merged
Sirherobrine23 merged 15 commits from code_refactoring into main 2024-03-15 04:08:27 +00:00
4 changed files with 74 additions and 3 deletions
Showing only changes of commit 06db113ff2 - Show all commits

@ -17,9 +17,10 @@ extern "C" {
#include <netinet/in.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
// #include <malloc.h>
#include <string.h>
#include <iostream>
#include <sys/ioctl.h>
#include <net/if.h>
std::string getWireguardVersion() {
return "Kernel";
@ -76,7 +77,7 @@ void WireguardConfig::getWireguardConfig() {
if ((status = wg_get_device(&devConfig, this->name.c_str())) < 0) throw std::string("It was not possible to get the Wireguard interface settings, code: ").append(std::to_string(status));
if (devConfig->flags & WGDEVICE_HAS_PRIVATE_KEY) this->privateKey = wgKeys::toString(devConfig->private_key);
if (devConfig->flags & WGDEVICE_HAS_PUBLIC_KEY) this->publicKey = wgKeys::toString(devConfig->public_key);
if (devConfig->flags & WGDEVICE_HAS_LISTEN_PORT) this->portListen = devConfig->listen_port;
if (!!devConfig->listen_port && devConfig->listen_port > 0) this->portListen = devConfig->listen_port;
this->interfaceAddress.GetInInterface(this->name);
for ((peer) = (devConfig)->first_peer; (peer); (peer) = (peer)->next_peer) {
@ -395,10 +396,25 @@ void IpManeger::SetInInterface(std::string interfaceName) {
lcl.bitlen = (req.ifa.ifa_family == AF_INET) ? 32 : 128;
if (inet_pton(req.ifa.ifa_family, ip.Address.c_str(), &lcl.data) <= 0) throw std::string("Invalid IP address: ").append(ip.Address);
if (req.ifa.ifa_family == AF_UNSPEC) req.ifa.ifa_family = lcl.family;
addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
if ((err = rtnl_talk(rth, &req.n, 0, 0, NULL)) < 0) throw std::string("Cannot set interface IP, code: ").append(std::to_string(err));
}
// Get INET socket
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) throw std::string("Error creating socket to set interface flags");
struct ifreq ifr;
strcpy(ifr.ifr_name, interfaceName.c_str());
// Set interface flags
ifr.ifr_flags = IFF_POINTOPOINT | IFF_NOARP | IFF_UP | IFF_RUNNING;
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
close(sockfd);
throw std::string("Error setting interface flags");
}
close(sockfd);
}

@ -0,0 +1,15 @@
#include "wginterface.hh"
#include "genKey/wgkeys.hh"
void WireguardDevices::getInterfaces() {}
void WireguardDevices::deleteInterface(std::string wgName) {
throw std::string("Use userspace module");
}
void WireguardConfig::getWireguardConfig() {
throw std::string("Use userspace module");
}
void WireguardConfig::setWireguardConfig() {
throw std::string("Use userspace module");
}

30
addon/win/wgip.cpp Normal file

@ -0,0 +1,30 @@
#include "wginterface.hh"
#include <windows.h>
#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>
#include <vector>
#pragma comment(lib, "wbemuuid.lib")
void SetIPAddress(IWbemServices* pSvc, IWbemClassObject* pclsObj, const wchar_t* propertyName, const wchar_t* ip) {
VARIANT vtProp;
HRESULT hres = pclsObj->Get(propertyName, 0, &vtProp, 0, 0);
if (SUCCEEDED(hres)) {
vtProp.vt = VT_BSTR;
vtProp.bstrVal = SysAllocString(ip);
hres = pclsObj->Put(propertyName, 0, &vtProp, 0);
VariantClear(&vtProp);
if (FAILED(hres)) {
std::cerr << "Failed to set " << propertyName << ". Error code: " << hres << std::endl;
}
}
}
void IpManeger::SetInInterface(std::string interfaceName) {
}
void IpManeger::GetInInterface(std::string interfaceName) {
throw std::string("Use userspace module");
}

@ -24,6 +24,16 @@ target:
- "-w"
- "-fpermissive"
- "-fPIC"
windows:
sources:
- "!addon/dummy/wginterface.cpp"
- "addon/win/wginterface.cpp"
- "addon/win/wgip.cpp"
libraries:
- wbemuuid.lib
defines:
- "_HAS_EXCEPTIONS=1"
- "ONSTARTADDON"
macos:
flags:
- "!-fno-exceptions"