forked from MatsuriDayo/nekoray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBean2External.cpp
350 lines (289 loc) · 13.3 KB
/
Bean2External.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "db/ProxyEntity.hpp"
#include "fmt/includes.h"
#include <QFile>
#include <QDir>
#include <QFileInfo>
#include <QUrl>
#define WriteTempFile(fn, data) \
QDir dir; \
if (!dir.exists("temp")) dir.mkdir("temp"); \
QFile f(QString("temp/") + fn); \
bool ok = f.open(QIODevice::WriteOnly | QIODevice::Truncate); \
if (ok) { \
f.write(data); \
} else { \
result.error = f.errorString(); \
} \
f.close(); \
auto TempFile = QFileInfo(f).absoluteFilePath();
namespace NekoGui_fmt {
// -1: Cannot use this config
// 0: Internal
// 1: Mapping External
// 2: Direct External
int NaiveBean::NeedExternal(bool isFirstProfile) {
if (isFirstProfile) {
if (NekoGui::dataStore->spmode_vpn) {
return 1;
}
return 2;
}
return 1;
}
int QUICBean::NeedExternal(bool isFirstProfile) {
auto hysteriaCore = [=] {
if (isFirstProfile) {
if (NekoGui::dataStore->spmode_vpn && hyProtocol != hysteria_protocol_facktcp && hopPort.trimmed().isEmpty()) {
return 1;
}
return 2;
} else {
if (hyProtocol == hysteria_protocol_facktcp || !hopPort.trimmed().isEmpty()) {
return -1;
}
}
return 1;
};
auto hysteria2Core = [=] {
if (isFirstProfile) {
if (NekoGui::dataStore->spmode_vpn) {
return 1;
}
return 2;
}
return 1;
};
auto tuicCore = [=] {
if (isFirstProfile) {
if (NekoGui::dataStore->spmode_vpn) {
return 1;
}
return 2;
}
return 1;
};
if (IS_NEKO_BOX) {
if (!forceExternal && (proxy_type == proxy_TUIC || hyProtocol == hysteria_protocol_udp)) {
// sing-box support
return 0;
} else {
// hysteria core support
return hysteriaCore();
}
} else if (proxy_type == proxy_TUIC) {
return tuicCore();
} else if (proxy_type == proxy_Hysteria2) {
return hysteria2Core();
} else {
return hysteriaCore();
}
}
int CustomBean::NeedExternal(bool isFirstProfile) {
if (core == "internal" || core == "internal-full") return 0;
return 1;
}
ExternalBuildResult NaiveBean::BuildExternal(int mapping_port, int socks_port, int external_stat) {
ExternalBuildResult result{NekoGui::dataStore->extraCore->Get("naive")};
auto is_direct = external_stat == 2;
auto domain_address = sni.isEmpty() ? serverAddress : sni;
auto connect_address = is_direct ? serverAddress : "127.0.0.1";
auto connect_port = is_direct ? serverPort : mapping_port;
domain_address = WrapIPV6Host(domain_address);
connect_address = WrapIPV6Host(connect_address);
auto proxy_url = QUrl();
proxy_url.setScheme(protocol);
proxy_url.setUserName(username);
proxy_url.setPassword(password);
proxy_url.setPort(connect_port);
proxy_url.setHost(domain_address);
if (!disable_log) result.arguments += "--log";
result.arguments += "--listen=socks://127.0.0.1:" + Int2String(socks_port);
result.arguments += "--proxy=" + proxy_url.toString(QUrl::FullyEncoded);
if (domain_address != connect_address)
result.arguments += "--host-resolver-rules=MAP " + domain_address + " " + connect_address;
if (insecure_concurrency > 0) result.arguments += "--insecure-concurrency=" + Int2String(insecure_concurrency);
if (!extra_headers.trimmed().isEmpty()) result.arguments += "--extra-headers=" + extra_headers;
if (!certificate.trimmed().isEmpty()) {
WriteTempFile("naive_" + GetRandomString(10) + ".crt", certificate.toUtf8());
result.env += "SSL_CERT_FILE=" + TempFile;
}
auto config_export = QStringList{result.program};
config_export += result.arguments;
result.config_export = QStringList2Command(config_export);
return result;
}
ExternalBuildResult QUICBean::BuildExternal(int mapping_port, int socks_port, int external_stat) {
if (proxy_type == proxy_TUIC) {
ExternalBuildResult result{NekoGui::dataStore->extraCore->Get("tuic")};
QJsonObject relay;
relay["uuid"] = uuid;
relay["password"] = password;
relay["udp_relay_mode"] = udpRelayMode;
relay["congestion_control"] = congestionControl;
relay["zero_rtt_handshake"] = zeroRttHandshake;
relay["disable_sni"] = disableSni;
if (!heartbeat.trimmed().isEmpty()) relay["heartbeat"] = heartbeat;
if (!alpn.trimmed().isEmpty()) relay["alpn"] = QList2QJsonArray(alpn.split(","));
if (!caText.trimmed().isEmpty()) {
WriteTempFile("tuic_" + GetRandomString(10) + ".crt", caText.toUtf8());
QJsonArray certificate;
certificate.append(TempFile);
relay["certificates"] = certificate;
}
// The most confused part of TUIC......
if (serverAddress == sni) {
relay["server"] = serverAddress + ":" + Int2String(serverPort);
} else {
relay["server"] = sni + ":" + Int2String(serverPort);
relay["ip"] = serverAddress;
}
QJsonObject local{
{"server", "127.0.0.1:" + Int2String(socks_port)},
};
QJsonObject config{
{"relay", relay},
{"local", local},
};
//
result.config_export = QJsonObject2QString(config, false);
WriteTempFile("tuic_" + GetRandomString(10) + ".json", result.config_export.toUtf8());
result.arguments = QStringList{"-c", TempFile};
return result;
} else if (proxy_type == proxy_Hysteria2) {
ExternalBuildResult result{NekoGui::dataStore->extraCore->Get("hysteria2")};
QJsonObject config;
auto server = serverAddress;
if (!hopPort.trimmed().isEmpty()) {
server = WrapIPV6Host(server) + ":" + hopPort;
} else {
server = WrapIPV6Host(server) + ":" + Int2String(serverPort);
}
QJsonObject transport;
transport["type"] = "udp";
transport["udp"] = QJsonObject{
{"hopInterval", hopInterval},
};
config["transport"] = transport;
config["server"] = server;
config["socks5"] = QJsonObject{
{"listen", "127.0.0.1:" + Int2String(socks_port)},
{"disableUDP", false},
};
config["auth"] = password;
QJsonObject bandwidth;
if (uploadMbps > 0) bandwidth["up"] = Int2String(uploadMbps) + " mbps";
if (downloadMbps > 0) bandwidth["down"] = Int2String(downloadMbps) + " mbps";
config["bandwidth"] = bandwidth;
QJsonObject quic;
if (streamReceiveWindow > 0) quic["initStreamReceiveWindow"] = streamReceiveWindow;
if (connectionReceiveWindow > 0) quic["initConnReceiveWindow"] = connectionReceiveWindow;
if (disableMtuDiscovery) quic["disablePathMTUDiscovery"] = true;
config["quic"] = quic;
config["fastOpen"] = true;
config["lazy"] = true;
if (!obfsPassword.isEmpty()) {
QJsonObject obfs;
obfs["type"] = "salamander";
obfs["salamander"] = QJsonObject{
{"password", obfsPassword},
};
config["obfs"] = obfs;
}
QJsonObject tls;
auto sniGen = sni;
if (sni.isEmpty() && !IsIpAddress(serverAddress)) sniGen = serverAddress;
tls["sni"] = sniGen;
if (allowInsecure) tls["insecure"] = true;
if (!caText.trimmed().isEmpty()) {
WriteTempFile("hysteria2_" + GetRandomString(10) + ".crt", caText.toUtf8());
QJsonArray certificate;
certificate.append(TempFile);
tls["certificates"] = certificate;
}
config["tls"] = tls;
result.config_export = QJsonObject2QString(config, false);
WriteTempFile("hysteria2_" + GetRandomString(10) + ".json", result.config_export.toUtf8());
result.arguments = QStringList{"-c", TempFile};
return result;
} else { // Hysteria
ExternalBuildResult result{NekoGui::dataStore->extraCore->Get("hysteria")};
QJsonObject config;
// determine server format
auto is_direct = external_stat == 2;
auto sniGen = sni;
if (sni.isEmpty() && !IsIpAddress(serverAddress)) sniGen = serverAddress;
auto server = serverAddress;
if (!hopPort.trimmed().isEmpty()) {
server = WrapIPV6Host(server) + ":" + hopPort;
} else {
server = WrapIPV6Host(server) + ":" + Int2String(serverPort);
}
config["server"] = is_direct ? server : "127.0.0.1:" + Int2String(mapping_port);
// listen
config["socks5"] = QJsonObject{
{"listen", "127.0.0.1:" + Int2String(socks_port)},
};
// misc
config["retry"] = 5;
config["fast_open"] = true;
config["lazy_start"] = true;
config["obfs"] = obfsPassword;
config["up_mbps"] = uploadMbps;
config["down_mbps"] = downloadMbps;
if (authPayloadType == hysteria_auth_base64) config["auth"] = authPayload;
if (authPayloadType == hysteria_auth_string) config["auth_str"] = authPayload;
if (hyProtocol == hysteria_protocol_facktcp) config["protocol"] = "faketcp";
if (hyProtocol == hysteria_protocol_wechat_video) config["protocol"] = "wechat-video";
if (!sniGen.isEmpty()) config["server_name"] = sniGen;
if (!alpn.isEmpty()) config["alpn"] = alpn;
if (!caText.trimmed().isEmpty()) {
WriteTempFile("hysteria_" + GetRandomString(10) + ".crt", caText.toUtf8());
config["ca"] = TempFile;
}
if (allowInsecure) config["insecure"] = true;
if (streamReceiveWindow > 0) config["recv_window_conn"] = streamReceiveWindow;
if (connectionReceiveWindow > 0) config["recv_window"] = connectionReceiveWindow;
if (disableMtuDiscovery) config["disable_mtu_discovery"] = true;
config["hop_interval"] = hopInterval;
//
result.config_export = QJsonObject2QString(config, false);
WriteTempFile("hysteria_" + GetRandomString(10) + ".json", result.config_export.toUtf8());
result.arguments = QStringList{"--no-check", "-c", TempFile};
return result;
}
}
ExternalBuildResult CustomBean::BuildExternal(int mapping_port, int socks_port, int external_stat) {
ExternalBuildResult result{NekoGui::dataStore->extraCore->Get(core)};
result.arguments = command; // TODO split?
for (int i = 0; i < result.arguments.length(); i++) {
auto arg = result.arguments[i];
arg = arg.replace("%mapping_port%", Int2String(mapping_port));
arg = arg.replace("%socks_port%", Int2String(socks_port));
arg = arg.replace("%server_addr%", serverAddress);
arg = arg.replace("%server_port%", Int2String(serverPort));
result.arguments[i] = arg;
}
if (!config_simple.trimmed().isEmpty()) {
auto config = config_simple;
config = config.replace("%mapping_port%", Int2String(mapping_port));
config = config.replace("%socks_port%", Int2String(socks_port));
config = config.replace("%server_addr%", serverAddress);
config = config.replace("%server_port%", Int2String(serverPort));
// suffix
QString suffix;
if (!config_suffix.isEmpty()) {
suffix = "." + config_suffix;
} else if (!QString2QJsonObject(config).isEmpty()) {
// trojan-go: unsupported config format: xxx.tmp. use .yaml or .json instead.
suffix = ".json";
}
// write config
WriteTempFile("custom_" + GetRandomString(10) + suffix, config.toUtf8());
for (int i = 0; i < result.arguments.count(); i++) {
result.arguments[i] = result.arguments[i].replace("%config%", TempFile);
}
result.config_export = config;
}
return result;
}
} // namespace NekoGui_fmt