forked from MatsuriDayo/nekoray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLink2Bean.cpp
214 lines (181 loc) · 8.53 KB
/
Link2Bean.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "db/ProxyEntity.hpp"
#include "fmt/includes.h"
#include <QUrlQuery>
namespace NekoGui_fmt {
#define DECODE_V2RAY_N_1 \
QString linkN = DecodeB64IfValid(SubStrBefore(SubStrAfter(link, "://"), "#"), QByteArray::Base64Option::Base64UrlEncoding); \
if (linkN.isEmpty()) return false; \
auto hasRemarks = link.contains("#"); \
if (hasRemarks) linkN += "#" + SubStrAfter(link, "#"); \
auto url = QUrl("https://" + linkN);
bool SocksHttpBean::TryParseLink(const QString &link) {
auto url = QUrl(link);
if (!url.isValid()) return false;
auto query = GetQuery(url);
if (link.startsWith("socks4")) socks_http_type = type_Socks4;
if (link.startsWith("http")) socks_http_type = type_HTTP;
name = url.fragment(QUrl::FullyDecoded);
serverAddress = url.host();
serverPort = url.port();
username = url.userName();
password = url.password();
if (serverPort == -1) serverPort = socks_http_type == type_HTTP ? 443 : 1080;
// v2rayN fmt
if (password.isEmpty() && !username.isEmpty()) {
QString n = DecodeB64IfValid(username);
if (!n.isEmpty()) {
username = SubStrBefore(n, ":");
password = SubStrAfter(n, ":");
}
}
stream->security = GetQueryValue(query, "security", "");
stream->sni = GetQueryValue(query, "sni");
if (link.startsWith("https")) stream->security = "tls";
return !serverAddress.isEmpty();
}
bool TrojanVLESSBean::TryParseLink(const QString &link) {
auto url = QUrl(link);
if (!url.isValid()) return false;
auto query = GetQuery(url);
name = url.fragment(QUrl::FullyDecoded);
serverAddress = url.host();
serverPort = url.port();
password = url.userName();
if (serverPort == -1) serverPort = 443;
// security
stream->network = GetQueryValue(query, "type", "tcp");
stream->security = GetQueryValue(query, "security", "tls").replace("reality", "tls");
auto sni1 = GetQueryValue(query, "sni");
auto sni2 = GetQueryValue(query, "peer");
if (!sni1.isEmpty()) stream->sni = sni1;
if (!sni2.isEmpty()) stream->sni = sni2;
if (!query.queryItemValue("allowInsecure").isEmpty()) stream->allow_insecure = true;
stream->reality_pbk = GetQueryValue(query, "pbk", "");
stream->reality_sid = GetQueryValue(query, "sid", "");
stream->reality_spx = GetQueryValue(query, "spx", "");
stream->utlsFingerprint = GetQueryValue(query, "fp", "");
// type
if (stream->network == "ws") {
stream->path = GetQueryValue(query, "path", "");
stream->host = GetQueryValue(query, "host", "");
} else if (stream->network == "http") {
stream->path = GetQueryValue(query, "path", "");
stream->host = GetQueryValue(query, "host", "").replace("|", ",");
} else if (stream->network == "grpc") {
stream->path = GetQueryValue(query, "serviceName", "");
} else if (stream->network == "tcp") {
if (GetQueryValue(query, "headerType") == "http") {
stream->header_type = "http";
stream->host = GetQueryValue(query, "host", "");
}
}
// protocol
if (proxy_type == proxy_VLESS) {
flow = GetQueryValue(query, "flow", "");
}
return !(password.isEmpty() || serverAddress.isEmpty());
}
bool ShadowSocksBean::TryParseLink(const QString &link) {
if (SubStrBefore(link, "#").contains("@")) {
// SS
auto url = QUrl(link);
if (!url.isValid()) return false;
name = url.fragment(QUrl::FullyDecoded);
serverAddress = url.host();
serverPort = url.port();
if (url.password().isEmpty()) {
// traditional format
auto method_password = DecodeB64IfValid(url.userName(), QByteArray::Base64Option::Base64UrlEncoding);
if (method_password.isEmpty()) return false;
method = SubStrBefore(method_password, ":");
password = SubStrAfter(method_password, ":");
} else {
// 2022 format
method = url.userName();
password = url.password();
}
auto query = GetQuery(url);
plugin = query.queryItemValue("plugin").replace("simple-obfs;", "obfs-local;");
} else {
// v2rayN
DECODE_V2RAY_N_1
if (hasRemarks) name = url.fragment(QUrl::FullyDecoded);
serverAddress = url.host();
serverPort = url.port();
method = url.userName();
password = url.password();
}
return !(serverAddress.isEmpty() || method.isEmpty() || password.isEmpty());
}
bool VMessBean::TryParseLink(const QString &link) {
// V2RayN Format
auto linkN = DecodeB64IfValid(SubStrAfter(link, "vmess://"));
if (!linkN.isEmpty()) {
auto objN = QString2QJsonObject(linkN);
if (objN.isEmpty()) return false;
// REQUIRED
uuid = objN["id"].toString();
serverAddress = objN["add"].toString();
serverPort = objN["port"].toVariant().toInt();
// OPTIONAL
name = objN["ps"].toString();
aid = objN["aid"].toVariant().toInt();
stream->host = objN["host"].toString();
stream->path = objN["path"].toString();
stream->sni = objN["sni"].toString();
stream->header_type = objN["type"].toString();
auto net = objN["net"].toString();
if (!net.isEmpty()) stream->network = net;
auto scy = objN["scy"].toString();
if (!scy.isEmpty()) security = scy;
// TLS (XTLS?)
stream->security = objN["tls"].toString();
// TODO quic & kcp
return true;
}
// Std Format
return false;
}
bool NaiveBean::TryParseLink(const QString &link) {
auto url = QUrl(link);
if (!url.isValid()) return false;
protocol = url.scheme().replace("naive+", "");
if (protocol != "https" && protocol != "quic") return false;
name = url.fragment(QUrl::FullyDecoded);
serverAddress = url.host();
serverPort = url.port();
username = url.userName();
password = url.password();
return !(username.isEmpty() || password.isEmpty() || serverAddress.isEmpty());
}
bool HysteriaBean::TryParseLink(const QString &link) {
// https://hysteria.network/docs/uri-scheme/
auto url = QUrl(link);
auto query = QUrlQuery(url.query());
if (url.host().isEmpty() || url.port() == -1 || !query.hasQueryItem("upmbps") || !query.hasQueryItem("downmbps")) return false;
name = url.fragment();
serverAddress = url.host();
serverPort = url.port();
serverAddress = url.host(); // default sni
hopPort = query.queryItemValue("mport");
obfsPassword = query.queryItemValue("obfsParam");
allowInsecure = query.queryItemValue("insecure") == "1";
uploadMbps = query.queryItemValue("upmbps").toInt();
downloadMbps = query.queryItemValue("downmbps").toInt();
auto protocolStr = (query.hasQueryItem("protocol") ? query.queryItemValue("protocol") : "udp").toLower();
if (protocolStr == "faketcp") {
protocol = NekoGui_fmt::HysteriaBean::hysteria_protocol_facktcp;
} else if (protocolStr.startsWith("wechat")) {
protocol = NekoGui_fmt::HysteriaBean::hysteria_protocol_wechat_video;
}
if (query.hasQueryItem("auth")) {
authPayload = query.queryItemValue("auth");
authPayloadType = NekoGui_fmt::HysteriaBean::hysteria_auth_string;
}
alpn = query.queryItemValue("alpn");
sni = FIRST_OR_SECOND(query.queryItemValue("peer"), query.queryItemValue("sni"));
connectionReceiveWindow = query.queryItemValue("recv_window").toInt();
streamReceiveWindow = query.queryItemValue("recv_window_conn").toInt();
return true;
}
} // namespace NekoGui_fmt