-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmess_new.cpp
172 lines (156 loc) · 5.88 KB
/
vmess_new.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) Qv2ray, A Qt frontend for V2Ray. Written in C++.
// This file is part of the Qv2ray VPN client.
//
// Qv2ray, A Qt frontend for V2Ray. Written in C++
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Copyright (c) 2024 AmneziaVPN
// This file has been modified for AmneziaVPN
//
// This file is based on the work of the Qv2ray VPN client.
// The original code of the Qv2ray, A Qt frontend for V2Ray. Written in C++ and licensed under GPL3.
//
// The modified version of this file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this file. If not, see <https://www.gnu.org/licenses/>.
#include "3rd/QJsonStruct/QJsonIO.hpp"
#include "3rd/QJsonStruct/QJsonStruct.hpp"
#include "transfer.h"
#include "serialization.h"
#include <QUrlQuery>
#define OUTBOUND_TAG_PROXY "PROXY"
namespace amnezia::serialization::vmess_new
{
const static QStringList NetworkType{ "tcp", "http", "ws", "kcp", "quic", "grpc" };
const static QStringList QuicSecurityTypes{ "none", "aes-128-gcm", "chacha20-poly1305" };
const static QStringList QuicKcpHeaderTypes{ "none", "srtp", "utp", "wechat-video", "dtls", "wireguard" };
const static QStringList FalseTypes{ "false", "False", "No", "Off", "0" };
QJsonObject Deserialize(const QString &vmessStr, QString *alias, QString *errMessage)
{
QUrl url{ vmessStr };
QUrlQuery query{ url };
//
#define default QJsonObject()
if (!url.isValid())
{
*errMessage = QObject::tr("vmess:// url is invalid");
return default;
}
// If previous alias is empty, just the PS is needed, else, append a "_"
const auto name = url.fragment(QUrl::FullyDecoded).trimmed();
*alias = alias->isEmpty() ? name : (*alias + "_" + name);
VMessServerObject server;
server.users << VMessServerObject::UserObject{};
StreamSettingsObject stream;
QString net;
bool tls = false;
// Check streamSettings
{
for (const auto &_protocol : url.userName().split("+"))
{
if (_protocol == "tls")
tls = true;
else
net = _protocol;
}
if (!NetworkType.contains(net))
{
*errMessage = QObject::tr("Invalid streamSettings protocol: ") + net;
return default;
}
stream.network = net;
stream.security = tls ? "tls" : "";
}
// Host Port UUID AlterID
{
const auto host = url.host();
int port = url.port();
QString uuid;
int aid;
{
const auto pswd = url.password();
const auto index = pswd.lastIndexOf("-");
uuid = pswd.mid(0, index);
aid = pswd.right(pswd.length() - index - 1).toInt();
}
server.address = host;
server.port = port;
server.users.first().id = uuid;
server.users.first().alterId = aid;
server.users.first().security = "auto";
}
const auto getQueryValue = [&query](const QString &key, const QString &defaultValue) {
if (query.hasQueryItem(key))
return query.queryItemValue(key, QUrl::FullyDecoded);
else
return defaultValue;
};
//
// Begin transport settings parser
{
if (net == "tcp")
{
stream.tcpSettings.header.type = getQueryValue("type", "none");
}
else if (net == "http")
{
stream.httpSettings.host.append(getQueryValue("host", ""));
stream.httpSettings.path = getQueryValue("path", "/");
}
else if (net == "ws")
{
stream.wsSettings.headers["Host"] = getQueryValue("host", "");
stream.wsSettings.path = getQueryValue("path", "/");
}
else if (net == "kcp")
{
stream.kcpSettings.seed = getQueryValue("seed", "");
stream.kcpSettings.header.type = getQueryValue("type", "none");
}
else if (net == "quic")
{
stream.quicSettings.security = getQueryValue("security", "none");
stream.quicSettings.key = getQueryValue("key", "");
stream.quicSettings.header.type = getQueryValue("type", "none");
}
else if (net == "grpc")
{
stream.grpcSettings.serviceName = getQueryValue("serviceName", "");
}
else
{
*errMessage = QObject::tr("Unknown transport method: ") + net;
return default;
}
}
#undef default
if (tls)
{
stream.tlsSettings.allowInsecure = !FalseTypes.contains(getQueryValue("allowInsecure", "false"));
stream.tlsSettings.serverName = getQueryValue("tlsServerName", "");
}
QJsonObject root;
QJsonObject vConf;
QJsonArray vnextArray;
vnextArray.append(server.toJson());
vConf["vnext"] = vnextArray;
auto outbound = outbounds::GenerateOutboundEntry(OUTBOUND_TAG_PROXY, "vmess", vConf, stream.toJson());
QJsonObject inbound = inbounds::GenerateInboundEntry();
//
root["outbounds"] = QJsonArray{ outbound };
root["inbound"] = QJsonArray{ inbound };
return root;
}
} // namespace amnezia::serialization::vmess_new