mirror of
https://github.com/Mahdi-zarei/nekoray.git
synced 2025-12-18 20:50:09 +08:00
Fix wireguard parse/export
This commit is contained in:
parent
f1ff39f2be
commit
936774c3ed
@ -50,6 +50,14 @@ namespace Configs
|
||||
return server;
|
||||
}
|
||||
|
||||
virtual void SetPort(int newPort) {
|
||||
server_port = newPort;
|
||||
}
|
||||
|
||||
virtual QString GetPort() {
|
||||
return QString::number(server_port);
|
||||
}
|
||||
|
||||
virtual QString DisplayAddress()
|
||||
{
|
||||
return ::DisplayAddress(server, server_port);
|
||||
|
||||
@ -83,6 +83,8 @@ namespace Configs
|
||||
QJsonObject ExportToJson() override;
|
||||
BuildResult Build() override;
|
||||
|
||||
void SetPort(int newPort) override;
|
||||
QString GetPort();
|
||||
void SetAddress(QString newAddr) override;
|
||||
QString GetAddress() override;
|
||||
QString DisplayAddress() override;
|
||||
|
||||
@ -92,6 +92,8 @@ QList<int> QJsonArray2QListInt(const QJsonArray &arr);
|
||||
|
||||
QJsonObject QMapString2QJsonObject(const QMap<QString,QString> &mp);
|
||||
|
||||
QList<QString> QListInt2QListString(const QList<int> &list);
|
||||
|
||||
#define QJSONARRAY_ADD(arr, add) \
|
||||
for (const auto &a: (add)) { \
|
||||
(arr) += a; \
|
||||
|
||||
@ -13,8 +13,8 @@ namespace Configs {
|
||||
if (!url.isValid()) return false;
|
||||
auto query = QUrlQuery(url.query(QUrl::ComponentFormattingOption::FullyDecoded));
|
||||
|
||||
if (query.hasQueryItem("address")) address = query.queryItemValue("address");
|
||||
if (query.hasQueryItem("port")) port = query.queryItemValue("port").toInt();
|
||||
address = url.host();
|
||||
port = url.port();
|
||||
if (query.hasQueryItem("public_key")) public_key = query.queryItemValue("public_key");
|
||||
if (query.hasQueryItem("peer_public_key")) public_key = query.queryItemValue("peer_public_key");
|
||||
if (query.hasQueryItem("pre_shared_key")) pre_shared_key = query.queryItemValue("pre_shared_key");
|
||||
@ -49,8 +49,6 @@ namespace Configs {
|
||||
QString Peer::ExportToLink()
|
||||
{
|
||||
QUrlQuery query;
|
||||
if (!address.isEmpty()) query.addQueryItem("address", address);
|
||||
if (port > 0) query.addQueryItem("port", QString::number(port));
|
||||
if (!public_key.isEmpty()) query.addQueryItem("public_key", public_key);
|
||||
if (!pre_shared_key.isEmpty()) query.addQueryItem("pre_shared_key", pre_shared_key);
|
||||
if (!reserved.isEmpty()) {
|
||||
@ -174,12 +172,21 @@ namespace Configs {
|
||||
if (object.isEmpty() || object["type"].toString() != "wireguard") return false;
|
||||
outbound::ParseFromJson(object);
|
||||
if (object.contains("private_key")) private_key = object["private_key"].toString();
|
||||
if (object.contains("peer") && object["peer"].isObject()) peer->ParseFromJson(object["peer"].toObject());
|
||||
if (object.contains("peers") && object["peers"].isArray() && !object["peers"].toArray().empty()) peer->ParseFromJson(object["peers"].toArray()[0].toObject());
|
||||
if (object.contains("address")) address = QJsonArray2QListString(object["address"].toArray());
|
||||
if (object.contains("mtu")) mtu = object["mtu"].toInt();
|
||||
if (object.contains("system")) system = object["system"].toBool();
|
||||
if (object.contains("worker_count")) worker_count = object["worker_count"].toInt();
|
||||
if (object.contains("udp_timeout")) udp_timeout = object["udp_timeout"].toString();
|
||||
if (object.contains("junk_packet_count")) junk_packet_count = object["junk_packet_count"].toInt(), enable_amnezia = true;
|
||||
if (object.contains("junk_packet_min_size")) junk_packet_min_size = object["junk_packet_min_size"].toInt(), enable_amnezia = true;
|
||||
if (object.contains("junk_packet_max_size")) junk_packet_max_size = object["junk_packet_max_size"].toInt(), enable_amnezia = true;
|
||||
if (object.contains("init_packet_junk_size")) init_packet_junk_size = object["init_packet_junk_size"].toInt(), enable_amnezia = true;
|
||||
if (object.contains("response_packet_junk_size")) response_packet_junk_size = object["response_packet_junk_size"].toInt(), enable_amnezia = true;
|
||||
if (object.contains("init_packet_magic_header")) init_packet_magic_header = object["init_packet_magic_header"].toInt(), enable_amnezia = true;
|
||||
if (object.contains("response_packet_magic_header")) response_packet_magic_header = object["response_packet_magic_header"].toInt(), enable_amnezia = true;
|
||||
if (object.contains("underload_packet_magic_header")) underload_packet_magic_header = object["underload_packet_magic_header"].toInt(), enable_amnezia = true;
|
||||
if (object.contains("transport_packet_magic_header")) transport_packet_magic_header = object["transport_packet_magic_header"].toInt(), enable_amnezia = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -232,6 +239,17 @@ namespace Configs {
|
||||
if (system) object["system"] = system;
|
||||
if (worker_count > 0) object["worker_count"] = worker_count;
|
||||
if (!udp_timeout.isEmpty()) object["udp_timeout"] = udp_timeout;
|
||||
if (enable_amnezia) {
|
||||
if (junk_packet_count > 0) object["junk_packet_count"] = junk_packet_count;
|
||||
if (junk_packet_min_size > 0) object["junk_packet_min_size"] = junk_packet_min_size;
|
||||
if (junk_packet_max_size > 0) object["junk_packet_max_size"] = junk_packet_max_size;
|
||||
if (init_packet_junk_size > 0) object["init_packet_junk_size"] = init_packet_junk_size;
|
||||
if (response_packet_junk_size > 0) object["response_packet_junk_size"] = response_packet_junk_size;
|
||||
if (init_packet_magic_header > 0) object["init_packet_magic_header"] = init_packet_magic_header;
|
||||
if (response_packet_magic_header > 0) object["response_packet_magic_header"] = response_packet_magic_header;
|
||||
if (underload_packet_magic_header > 0) object["underload_packet_magic_header"] = underload_packet_magic_header;
|
||||
if (transport_packet_magic_header > 0) object["transport_packet_magic_header"] = transport_packet_magic_header;
|
||||
}
|
||||
|
||||
auto peerObj = peer->ExportToJson();
|
||||
if (!peerObj.isEmpty()) {
|
||||
@ -253,6 +271,17 @@ namespace Configs {
|
||||
if (system) object["system"] = system;
|
||||
if (worker_count > 0) object["worker_count"] = worker_count;
|
||||
if (!udp_timeout.isEmpty()) object["udp_timeout"] = udp_timeout;
|
||||
if (enable_amnezia) {
|
||||
if (junk_packet_count > 0) object["junk_packet_count"] = junk_packet_count;
|
||||
if (junk_packet_min_size > 0) object["junk_packet_min_size"] = junk_packet_min_size;
|
||||
if (junk_packet_max_size > 0) object["junk_packet_max_size"] = junk_packet_max_size;
|
||||
if (init_packet_junk_size > 0) object["init_packet_junk_size"] = init_packet_junk_size;
|
||||
if (response_packet_junk_size > 0) object["response_packet_junk_size"] = response_packet_junk_size;
|
||||
if (init_packet_magic_header > 0) object["init_packet_magic_header"] = init_packet_magic_header;
|
||||
if (response_packet_magic_header > 0) object["response_packet_magic_header"] = response_packet_magic_header;
|
||||
if (underload_packet_magic_header > 0) object["underload_packet_magic_header"] = underload_packet_magic_header;
|
||||
if (transport_packet_magic_header > 0) object["transport_packet_magic_header"] = transport_packet_magic_header;
|
||||
}
|
||||
|
||||
auto peerObj = peer->Build().object;
|
||||
if (!peerObj.isEmpty()) {
|
||||
@ -269,6 +298,13 @@ namespace Configs {
|
||||
return peer->address;
|
||||
}
|
||||
|
||||
void wireguard::SetPort(int newPort) {
|
||||
peer->port = newPort;
|
||||
}
|
||||
|
||||
QString wireguard::GetPort() {
|
||||
return QString::number(peer->port);
|
||||
}
|
||||
|
||||
QString wireguard::DisplayAddress()
|
||||
{
|
||||
|
||||
@ -159,6 +159,12 @@ QJsonObject QMapString2QJsonObject(const QMap<QString,QString> &mp) {
|
||||
return res;
|
||||
}
|
||||
|
||||
QList<QString> QListInt2QListString(const QList<int> &list) {
|
||||
QList<QString> resp;
|
||||
for (int item : list) resp << Int2String(item);
|
||||
return resp;
|
||||
}
|
||||
|
||||
QByteArray ReadFile(const QString &path) {
|
||||
QFile file(path);
|
||||
file.open(QFile::ReadOnly);
|
||||
|
||||
@ -341,8 +341,8 @@ void DialogEditProfile::typeSelected(const QString &newType) {
|
||||
|
||||
// 左边 common
|
||||
ui->name->setText(ent->outbound->name);
|
||||
ui->address->setText(ent->outbound->server);
|
||||
ui->port->setText(Int2String(ent->outbound->server_port));
|
||||
ui->address->setText(ent->outbound->GetAddress());
|
||||
ui->port->setText(ent->outbound->GetPort());
|
||||
ui->port->setValidator(QRegExpValidator_Number);
|
||||
|
||||
// 星号
|
||||
@ -403,8 +403,8 @@ bool DialogEditProfile::onEnd() {
|
||||
}
|
||||
|
||||
ent->outbound->name = ui->name->text();
|
||||
ent->outbound->server = ui->address->text().remove(' ');
|
||||
ent->outbound->server_port = ui->port->text().toInt();
|
||||
ent->outbound->SetAddress(ui->address->text().remove(' '));
|
||||
ent->outbound->SetPort(ui->port->text().toInt());
|
||||
|
||||
if (ent->outbound->HasTLS() || ent->outbound->HasTransport()) {
|
||||
auto tls = ent->outbound->GetTLS();
|
||||
|
||||
@ -19,8 +19,7 @@ void EditWireguard::onStart(std::shared_ptr<Configs::ProxyEntity> _ent) {
|
||||
ui->private_key->setText(outbound->private_key);
|
||||
ui->public_key->setText(outbound->peer->public_key);
|
||||
ui->preshared_key->setText(outbound->peer->pre_shared_key);
|
||||
// auto reservedStr = outbound->peer->reserved;
|
||||
// ui->reserved->setText(reservedStr);
|
||||
ui->reserved->setText(QListInt2QListString(outbound->peer->reserved).join(","));
|
||||
ui->persistent_keepalive->setText(Int2String(outbound->peer->persistent_keepalive));
|
||||
ui->mtu->setText(Int2String(outbound->mtu));
|
||||
ui->sys_ifc->setChecked(outbound->system);
|
||||
@ -46,11 +45,11 @@ bool EditWireguard::onEnd() {
|
||||
outbound->peer->public_key = ui->public_key->text();
|
||||
outbound->peer->pre_shared_key = ui->preshared_key->text();
|
||||
auto rawReserved = ui->reserved->text();
|
||||
// outbound->reserved = {};
|
||||
// for (const auto& item: rawReserved.split(",")) {
|
||||
// if (item.trimmed().isEmpty()) continue;
|
||||
// outbound->reserved += item.trimmed().toInt();
|
||||
// }
|
||||
outbound->peer->reserved = {};
|
||||
for (const auto& item: rawReserved.split(",")) {
|
||||
if (item.trimmed().isEmpty()) continue;
|
||||
outbound->peer->reserved += item.trimmed().toInt();
|
||||
}
|
||||
outbound->peer->persistent_keepalive = ui->persistent_keepalive->text().toInt();
|
||||
outbound->mtu = ui->mtu->text().toInt();
|
||||
outbound->system = ui->sys_ifc->isChecked();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user