mirror of
https://github.com/Mahdi-zarei/nekoray.git
synced 2025-12-19 05:30:06 +08:00
implement wireguard file config parser
This commit is contained in:
parent
3d5f5a0a00
commit
930ed61f7e
@ -72,5 +72,98 @@ namespace Configs {
|
||||
QString ToShareLink() override;
|
||||
|
||||
bool IsEndpoint() override {return true;}
|
||||
|
||||
private:
|
||||
bool parseWgConfig(const QString &config)
|
||||
{
|
||||
if (!config.contains("[Interface]") || !config.contains("[Peer]")) return false;
|
||||
auto lines = config.split("\n");
|
||||
for (const auto& line : lines)
|
||||
{
|
||||
if (line.trimmed().isEmpty()) continue;
|
||||
if (line.contains("[Interface]") || line.contains("[Peer]")) continue;
|
||||
if (!line.contains("=")) return false;
|
||||
auto eqIdx = line.indexOf("=");
|
||||
if (line.startsWith("PrivateKey"))
|
||||
{
|
||||
privateKey = line.mid(eqIdx + 1).trimmed();
|
||||
}
|
||||
if (line.startsWith("Address"))
|
||||
{
|
||||
auto addresses = line.mid(eqIdx + 1).trimmed().split(",");
|
||||
for (const auto& address : addresses) localAddress.append(address.trimmed());
|
||||
}
|
||||
if (line.startsWith("MTU"))
|
||||
{
|
||||
MTU = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
if (line.startsWith("PublicKey"))
|
||||
{
|
||||
publicKey = line.mid(eqIdx + 1).trimmed();
|
||||
}
|
||||
if (line.startsWith("PresharedKey"))
|
||||
{
|
||||
preSharedKey = line.mid(eqIdx + 1).trimmed();
|
||||
}
|
||||
if (line.startsWith("PersistentKeepalive"))
|
||||
{
|
||||
persistentKeepalive = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
if (line.startsWith("Endpoint"))
|
||||
{
|
||||
auto addrPort = line.mid(eqIdx + 1).trimmed();
|
||||
if (!addrPort.contains(":")) return false;
|
||||
serverAddress = addrPort.split(":")[0].trimmed();
|
||||
serverPort = addrPort.split(":")[1].trimmed().toInt();
|
||||
}
|
||||
if (line.startsWith("S1"))
|
||||
{
|
||||
enable_amnezia = true;
|
||||
init_packet_junk_size = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
if (line.startsWith("S2"))
|
||||
{
|
||||
enable_amnezia = true;
|
||||
response_packet_junk_size = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
if (line.startsWith("Jc"))
|
||||
{
|
||||
enable_amnezia = true;
|
||||
junk_packet_count = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
if (line.startsWith("Jmin"))
|
||||
{
|
||||
enable_amnezia = true;
|
||||
junk_packet_min_size = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
if (line.startsWith("Jmax"))
|
||||
{
|
||||
enable_amnezia = true;
|
||||
junk_packet_max_size = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
if (line.startsWith("H1"))
|
||||
{
|
||||
enable_amnezia = true;
|
||||
init_packet_magic_header = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
if (line.startsWith("H2"))
|
||||
{
|
||||
enable_amnezia = true;
|
||||
response_packet_magic_header = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
if (line.startsWith("H3"))
|
||||
{
|
||||
enable_amnezia = true;
|
||||
underload_packet_magic_header = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
if (line.startsWith("H4"))
|
||||
{
|
||||
enable_amnezia = true;
|
||||
transport_packet_magic_header = line.mid(eqIdx + 1).toInt();
|
||||
}
|
||||
}
|
||||
name = "Wg file config";
|
||||
return true;
|
||||
};
|
||||
};
|
||||
} // namespace Configs
|
||||
|
||||
@ -11,6 +11,8 @@ namespace Subscription {
|
||||
|
||||
void updateSingBox(const QString &str);
|
||||
|
||||
void updateWireguardFileConfig(const QString &str);
|
||||
|
||||
int gid_add_to = -1;
|
||||
|
||||
QList<std::shared_ptr<Configs::ProxyEntity>> updated_order;
|
||||
|
||||
@ -396,6 +396,8 @@ namespace Configs {
|
||||
}
|
||||
|
||||
bool WireguardBean::TryParseLink(const QString &link) {
|
||||
if (parseWgConfig(link)) return true;
|
||||
|
||||
auto url = QUrl(link);
|
||||
if (!url.isValid()) return false;
|
||||
auto query = GetQuery(url);
|
||||
|
||||
@ -84,6 +84,13 @@ namespace Subscription {
|
||||
return;
|
||||
}
|
||||
|
||||
// Wireguard Config
|
||||
if (str.contains("[Interface]") && str.contains("[Peer]"))
|
||||
{
|
||||
updateWireguardFileConfig(str);
|
||||
return;
|
||||
}
|
||||
|
||||
// Multi line
|
||||
if (str.count("\n") > 0 && needParse) {
|
||||
auto list = Disect(str);
|
||||
@ -345,6 +352,15 @@ namespace Subscription {
|
||||
}
|
||||
}
|
||||
|
||||
void RawUpdater::updateWireguardFileConfig(const QString& str)
|
||||
{
|
||||
auto ent = Configs::ProfileManager::NewProxyEntity("wireguard");
|
||||
auto ok = ent->WireguardBean()->TryParseLink(str);
|
||||
if (!ok) return;
|
||||
updated_order += ent;
|
||||
}
|
||||
|
||||
|
||||
QString Node2QString(const fkyaml::node &n, const QString &def = "") {
|
||||
try {
|
||||
return n.as_str().c_str();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user