forked from MatsuriDayo/nekoray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBean2External.cpp
126 lines (107 loc) · 5.13 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
#include "db/ProxyEntity.hpp"
#include "fmt/includes.h"
#include <QFile>
#include <QDir>
#include <QFileInfo>
#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 NekoRay::fmt {
// 0: no external
// 1: Mapping External
// 2: Direct External
int NaiveBean::NeedExternal(bool isFirstProfile, bool isVPN) {
if (isFirstProfile && !isVPN) {
return 2;
}
return 1;
}
int CustomBean::NeedExternal(bool isFirstProfile, bool isVPN) {
if (core == "internal") return 0;
if (IS_NEKO_BOX && core == "hysteria") return 0;
if (core == "hysteria") {
if (isFirstProfile && !isVPN) {
return 2;
}
}
return 1;
}
ExternalBuildResult NaiveBean::BuildExternal(int mapping_port, int socks_port, int external_stat) {
ExternalBuildResult result{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);
result.arguments += "--log";
result.arguments += "--listen=socks://127.0.0.1:" + Int2String(socks_port);
result.arguments += "--proxy=" + protocol + "://" + username + ":" + password + "@" +
domain_address + ":" + Int2String(connect_port);
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.isEmpty()) result.arguments += "--extra-headers=" + extra_headers;
if (!certificate.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 CustomBean::BuildExternal(int mapping_port, int socks_port, int external_stat) {
ExternalBuildResult result{dataStore->extraCore->Get(core)};
result.arguments = command; // TODO split?
for (int i = 0; i < result.arguments.length(); i++) {
auto arg = result.arguments[i];
if (arg.contains("%mapping_port%")) {
arg = arg.replace("%mapping_port%", Int2String(mapping_port));
} else if (arg.contains("%socks_port%")) {
arg = arg.replace("%socks_port%", Int2String(socks_port));
} else {
continue;
}
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";
}
// known core direct out
if (external_stat == 2) {
if (core == "hysteria") {
config = config.replace(QString("\"127.0.0.1:%1\"").arg(mapping_port),
"\"" + DisplayAddress() + "\"");
}
}
// 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 NekoRay::fmt