Парсер wireguard конфига в формат MikroTik
Добавлено: 07 июн 2024, 18:03
обязательно должен быть предварительно созданный адрес-лист в микротике allow-local-all с созданными локальными адресами
wg-0.2.1-reliz.cpp
Откомпилировать:
Откомпилировать для винды:
Запуск под Linux:
Запуск под Win:
wg-0.2.1-reliz.cpp
Код: Выделить всё
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <vector>
struct WireGuardConfig {
std::string publicKey;
std::string privateKey;
std::string presharedKey;
std::string address;
std::string dns;
std::string mtu;
std::string endpoint;
std::string endport;
};
int main(int argc, char* argv[]) {
if (argc < 2 ) exit (1);
std::map<std::string, std::string> config;
std::ifstream configFile(argv[1]);
if (!configFile.is_open()) {
std::cerr << "Error opening config file" << std::endl;
return 1;
}
std::vector<WireGuardConfig> configs;
WireGuardConfig currentConfig;
std::string line;
while (std::getline(configFile, line)) {
if (line.empty() || line[0] == '#') {
continue; // skip empty lines and lines starting with #
}
std::string key, value;
size_t pos = line.find('=');
if (pos != std::string::npos)
{
key = line.substr(0, pos);
value = line.substr(pos + 1);
// Remove leading and trailing whitespaces
key.erase(0, key.find_first_not_of(" \t\r"));
key.erase(key.find_last_not_of(" \t\r") + 1);
value.erase(0, value.find_first_not_of(" \t\r"));
value.erase(value.find_last_not_of(" \t\r") + 1);
config[key] = value;
if (key == "PublicKey") {
currentConfig.publicKey = value;
} else if (key == "PrivateKey") {
currentConfig.privateKey = value;
} else if (key == "Address") {
currentConfig.address = value;
} else if (key == "DNS") {
currentConfig.dns = value;
} else if (key == "MTU") {
currentConfig.mtu= value;
} else if (key == "PresharedKey") {
currentConfig.presharedKey = value;
} else if (key == "Endpoint") {
currentConfig.endpoint = value.substr(0,value.find(":"));
currentConfig.endport = value.substr(value.find(":")+1);
} else if (key == "}") {
configs.push_back(currentConfig);
currentConfig = WireGuardConfig();
}
}
}
configFile.close();
std::string wireguard_port;
std::string wireguard_local_address;
std::cout << "Enter port WireGuard: ";
std::getline(std::cin, wireguard_port);
std::cout << "Enter local address WireGuard: ";
std::getline(std::cin, wireguard_local_address);
std::cout << std::endl;
std::cout << std::endl;
//Создание интерфейса
std::cout << "# Interface wireguard" << std::endl;
std::cout << "/interface wireguard add";
std::cout << " listen-port=" << wireguard_port;
if(std::size(currentConfig.mtu) )
std::cout << " mtu=" << currentConfig.mtu;
std::cout << " name=wg-" << currentConfig.endpoint;
std::cout << " private-key=\""<< currentConfig.privateKey << "\"";
std::cout << std::endl;
//Создание пира
std::cout << "# WireGuard peer" << std::endl;
std::cout << "/interface wireguard peers add allowed-address=0.0.0.0/0";
std::cout << " endpoint-address=" << currentConfig.endpoint;
std::cout << " endpoint-port=" << currentConfig.endport;
std::cout << " interface=wg-" << currentConfig.endpoint;
std::cout << " persistent-keepalive=25s";
std::cout << " public-key=\"" << currentConfig.publicKey << "\"";
if(std::size(currentConfig.presharedKey) )
std::cout <<" preshared-key\""<< currentConfig.presharedKey << "\"";
std::cout << " comment=wg-" << currentConfig.endpoint;
std::cout << std::endl;
//Создание локального адреса интерфейса
std::cout << "# Local ip address WireGuard" << std::endl;
std::cout << "/ip address add";
std::cout << " address=" << currentConfig.address;
std::cout << " interface=wg-" << currentConfig.endpoint;
std::cout << std::endl;
//Создание NAT правила
std::cout << "# Firewall nat WireGuard" << std::endl;
std::cout << "/ip firewall nat add action=masquerade chain=srcnat";
std::cout << " out-interface=wg-" << currentConfig.endpoint;
std::cout << " comment=wg-" << currentConfig.endpoint;
std::cout << std::endl;
//Создание таблицы маршрута
std::cout << "# Routing table WireGuard" << std::endl;
std::cout << "/routing table add disabled=no fib";
std::cout << " name=vpn-" << currentConfig.endpoint;
std::cout << std::endl;
//Создание маршрута
std::cout << "# Ip route WireGuard" << std::endl;
std::cout << "/ip route add disabled=no distance=1 dst-address=0.0.0.0/0";
std::cout << " gateway=wg-"<< currentConfig.endpoint;
std::cout << " pref-src=\"\"";
std::cout << " routing-table=vpn-"<< currentConfig.endpoint;
std::cout << " suppress-hw-offload=no";
std::cout << " comment=vpn-" << currentConfig.endpoint;
std::cout << std::endl;
//Создание мангала
std::cout << "# Firewall mangle WireGuard" << std::endl;
std::cout << "/ip firewall mangle add action=mark-routing chain=prerouting disabled=no";
std::cout << " src-address=" << wireguard_local_address;
std::cout << " dst-address-list=!allow-local-all protocol=tcp dst-port=443";
std::cout << " new-routing-mark=vpn-" << currentConfig.endpoint;
std::cout << " passthrough=yes";
std::cout << " comment=vpn-" << currentConfig.endpoint;
std::cout << std::endl;
std::cout << "# Enter to exit" << std::endl;
std::getline(std::cin, wireguard_local_address);
std::cout << std::endl;
return 0;
}
Код: Выделить всё
g++ -std=c++17 -o wg.a wg-0.2.1-reliz.cpp
Код: Выделить всё
i686-w64-mingw32-g++ -o wg.exe wg-0.2.1-reliz.cpp
Код: Выделить всё
./wg.a wireguard.conf
Код: Выделить всё
wg.exe wireguard.conf