Fixing WireGuard, hy and hy2 Protocol export Issues.

This commit is contained in:
asdlokj1qpi23
2023-12-21 19:27:31 +08:00
parent 03b6f02573
commit 5b85906be1
5 changed files with 610 additions and 622 deletions

View File

@@ -2112,6 +2112,15 @@ static rapidjson::Value stringArrayToJsonArray(const std::string &array, const s
return result;
}
bool isNumeric(const std::string &str) {
for (char c: str) {
if (!std::isdigit(static_cast<unsigned char>(c))) {
return false;
}
}
return true;
}
void
proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector<RulesetContent> &ruleset_content_array,
const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext) {
@@ -2120,6 +2129,7 @@ proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector
rapidjson::Value outbounds(rapidjson::kArrayType), route(rapidjson::kArrayType);
std::vector<Proxy> nodelist;
string_array remarks_list;
std::string search = " Mbps";
if (!ext.nodelist) {
auto direct = buildObject(allocator, "type", "direct", "tag", "DIRECT");
@@ -2241,13 +2251,15 @@ proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector
case ProxyType::WireGuard: {
proxy.AddMember("type", "wireguard", allocator);
proxy.AddMember("tag", rapidjson::StringRef(x.Remark.c_str()), allocator);
proxy.AddMember("inet4_bind_address", rapidjson::StringRef(x.SelfIP.c_str()), allocator);
rapidjson::Value addresses(rapidjson::kArrayType);
addresses.PushBack(rapidjson::StringRef(x.SelfIP.c_str()), allocator);
if (!x.SelfIPv6.empty())
addresses.PushBack(rapidjson::StringRef(x.SelfIPv6.c_str()), allocator);
addresses.PushBack(rapidjson::StringRef(x.SelfIP.append("/32").c_str()), allocator);
// if (!x.SelfIPv6.empty())
// addresses.PushBack(rapidjson::StringRef(x.SelfIPv6.c_str()), allocator);
proxy.AddMember("local_address", addresses, allocator);
if (!x.SelfIPv6.empty())
proxy.AddMember("inet6_bind_address", rapidjson::StringRef(x.SelfIPv6.c_str()), allocator);
proxy.AddMember("private_key", rapidjson::StringRef(x.PrivateKey.c_str()), allocator);
rapidjson::Value peer(rapidjson::kObjectType);
peer.AddMember("server", rapidjson::StringRef(x.Hostname.c_str()), allocator);
peer.AddMember("server_port", x.Port, allocator);
@@ -2288,18 +2300,41 @@ proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector
case ProxyType::Hysteria: {
addSingBoxCommonMembers(proxy, x, "hysteria", allocator);
proxy.AddMember("auth_str", rapidjson::StringRef(x.Auth.c_str()), allocator);
proxy.AddMember("up", rapidjson::StringRef(x.UpMbps.c_str()), allocator);
proxy.AddMember("down", rapidjson::StringRef(x.DownMbps.c_str()), allocator);
rapidjson::Value tls(rapidjson::kObjectType);
tls.AddMember("enabled", true, allocator);
proxy.AddMember("tls", tls, allocator);
if (!tfo.is_undef())
proxy.AddMember("tcp_fast_open", tfo.get(), allocator);
if (isNumeric(x.UpMbps)) {
proxy.AddMember("up_mbps", std::stoi(x.UpMbps), allocator);
// x.UpMbps.append(search);
// proxy.AddMember("up", rapidjson::StringRef(x.UpMbps.c_str()), allocator);
} else {
// proxy.AddMember("up", rapidjson::StringRef(x.UpMbps.c_str()), allocator);
size_t pos = x.UpMbps.find(search);
if (pos != std::string::npos) {
x.UpMbps.replace(pos, search.length(), "");
}
proxy.AddMember("up_mbps", std::stoi(x.UpMbps), allocator);
}
if (isNumeric(x.DownMbps)) {
proxy.AddMember("down_mbps", std::stoi(x.DownMbps), allocator);
// x.DownMbps.append(search);
// proxy.AddMember("down", rapidjson::StringRef(x.DownMbps.c_str()), allocator);
} else {
// proxy.AddMember("down", rapidjson::StringRef(x.DownMbps.c_str()), allocator);
size_t pos = x.DownMbps.find(search);
if (pos != std::string::npos) {
x.DownMbps.replace(pos, search.length(), "");
}
proxy.AddMember("down_mbps", std::stoi(x.DownMbps), allocator);
}
if (!x.TLSSecure) {
rapidjson::Value tls(rapidjson::kObjectType);
tls.AddMember("enabled", true, allocator);
if (!x.Alpn.empty()) {
auto alpns = stringArrayToJsonArray(x.Alpn, ",", allocator);
tls.AddMember("alpn", alpns, allocator);
}
proxy.AddMember("tls", tls, allocator);
}
if (!x.FakeType.empty())
proxy.AddMember("network", rapidjson::StringRef(x.FakeType.c_str()), allocator);
if (!x.Alpn.empty())
proxy.AddMember("tls", rapidjson::StringRef(
std::string(R"({ "enabled": true,"alpn": [)" + x.Alpn + "],}").c_str()), allocator);
if (!x.OBFSParam.empty())
proxy.AddMember("obfs", rapidjson::StringRef(x.OBFSParam.c_str()), allocator);
break;
@@ -2307,19 +2342,33 @@ proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector
case ProxyType::Hysteria2: {
addSingBoxCommonMembers(proxy, x, "hysteria2", allocator);
proxy.AddMember("password", rapidjson::StringRef(x.Password.c_str()), allocator);
if(!x.TLSSecure) {
if (!x.TLSSecure) {
rapidjson::Value tls(rapidjson::kObjectType);
tls.AddMember("enabled", true, allocator);
if (!x.Alpn.empty()){
auto alpns = stringArrayToJsonArray(x.Alpn, ",", allocator);
if (!x.Alpn.empty()) {
auto alpns = stringArrayToJsonArray(x.Alpn, ",", allocator);
tls.AddMember("alpn", alpns, allocator);
}
proxy.AddMember("tls", tls, allocator);
}
if (!x.UpMbps.empty())
proxy.AddMember("up_mbps", rapidjson::StringRef(x.UpMbps.c_str()), allocator);
if (!x.DownMbps.empty())
proxy.AddMember("down_mbps", rapidjson::StringRef(x.DownMbps.c_str()), allocator);
if (!x.UpMbps.empty()) {
if (!isNumeric(x.UpMbps)) {
size_t pos = x.UpMbps.find(search);
if (pos != std::string::npos) {
x.UpMbps.replace(pos, search.length(), "");
}
}
proxy.AddMember("up_mbps", std::stoi(x.UpMbps), allocator);
}
if (!x.DownMbps.empty()) {
if (!isNumeric(x.DownMbps)) {
size_t pos = x.DownMbps.find(search);
if (pos != std::string::npos) {
x.DownMbps.replace(pos, search.length(), "");
}
}
proxy.AddMember("down_mbps", std::stoi(x.DownMbps), allocator);
}
if (!x.OBFSParam.empty()) {
if (!x.OBFSPassword.empty()) {
proxy.AddMember("obfs", rapidjson::StringRef(std::string(
@@ -2341,8 +2390,8 @@ proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector
tls.AddMember("enabled", true, allocator);
if (!x.ServerName.empty())
tls.AddMember("server_name", rapidjson::StringRef(x.ServerName.c_str()), allocator);
if (!x.Alpn.empty()){
auto alpns = stringArrayToJsonArray(x.Alpn, ",", allocator);
if (!x.Alpn.empty()) {
auto alpns = stringArrayToJsonArray(x.Alpn, ",", allocator);
tls.AddMember("alpn", alpns, allocator);
}
tls.AddMember("insecure", buildBooleanValue(scv), allocator);
@@ -2361,7 +2410,7 @@ proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector
if (!x.PublicKey.empty()) {
reality.AddMember("public_key", rapidjson::StringRef(x.PublicKey.c_str()), allocator);
}
auto shortIds= stringArrayToJsonArray(x.Alpn, ",", allocator);
auto shortIds = stringArrayToJsonArray(x.Alpn, ",", allocator);
reality.AddMember("short_id", shortIds, allocator);
tls.AddMember("reality", reality, allocator);
}

File diff suppressed because it is too large Load Diff

View File

@@ -78,6 +78,8 @@ struct Proxy {
uint16_t AlterId = 0;
String TransferProtocol;
String FakeType;
String AuthStr;
bool TLSSecure = false;
String Host;

View File

@@ -156,7 +156,7 @@ void wireguardConstruct(Proxy &node, const std::string &group, const std::string
}
void hysteriaConstruct(Proxy &node, const std::string &group, const std::string &remarks, const std::string &add,
const std::string &port, const std::string &type, const std::string &auth,
const std::string &port, const std::string &type, const std::string &auth,const std::string &auth_str,
const std::string &host, const std::string &up, const std::string &down, const std::string &alpn,
const std::string &obfsParam, const std::string &insecure, tribool udp, tribool tfo, tribool scv,
tribool tls13) {
@@ -169,6 +169,7 @@ void hysteriaConstruct(Proxy &node, const std::string &group, const std::string
node.OBFSParam = obfsParam;
node.Insecure = insecure;
node.FakeType = type;
node.AuthStr = auth_str;
}
@@ -1304,6 +1305,12 @@ void explodeClash(Node yamlnode, std::vector<Proxy> &nodes) {
case "hysteria"_hash:
group = HYSTERIA_DEFAULT_GROUP;
singleproxy["auth_str"] >> auth;
if (auth.empty()) {
singleproxy["auth-str"] >> auth;
if (auth.empty()) {
singleproxy["password"] >> auth;
}
}
singleproxy["up"] >> up;
singleproxy["down"] >> down;
singleproxy["obfs"] >> obfsParam;
@@ -1312,7 +1319,7 @@ void explodeClash(Node yamlnode, std::vector<Proxy> &nodes) {
singleproxy["alpn"][0] >> alpn;
singleproxy["protocol"] >> insecure;
hysteriaConstruct(node, group, ps, server, port, type, auth, host, up, down, alpn, obfsParam, insecure,
hysteriaConstruct(node, group, ps, server, port, type, auth,"", host, up, down, alpn, obfsParam, insecure,
udp, tfo, scv);
break;
case "hysteria2"_hash:
@@ -1382,7 +1389,7 @@ void explodeStdVMess(std::string vmess, Proxy &node) {
void explodeStdHysteria(std::string hysteria, Proxy &node) {
std::string add, port, type, auth, host, insecure, up, down, alpn, obfsParam, remarks;
std::string add, port, type, auth, host, insecure, up, down, alpn, obfsParam, remarks,auth_str;
std::string addition;
hysteria = hysteria.substr(11);
string_size pos;
@@ -1397,17 +1404,17 @@ void explodeStdHysteria(std::string hysteria, Proxy &node) {
return;
type = getUrlArg(addition, "protocol");
auth = getUrlArg(addition, "auth");
auth_str = getUrlArg(addition, "auth_str");
host = getUrlArg(addition, "peer");
insecure = getUrlArg(addition, "insecure");
up = getUrlArg(addition, "upmbps");
down = getUrlArg(addition, "downmbps");
alpn = getUrlArg(addition, "alpn");
obfsParam = getUrlArg(addition, "obfsParam");
if (remarks.empty())
remarks = add + ":" + port;
hysteriaConstruct(node, HYSTERIA_DEFAULT_GROUP, remarks, add, port, type, auth, host, up, down, alpn, obfsParam,
hysteriaConstruct(node, HYSTERIA_DEFAULT_GROUP, remarks, add, port, type, auth,auth_str, host, up, down, alpn, obfsParam,
insecure);
return;
}

View File

@@ -19,7 +19,7 @@ enum class ConfType
SUB,
Local
};
void hysteriaConstruct(Proxy &node, const std::string &group, const std::string &remarks, const std::string &add, const std::string &port, const std::string &type, const std::string &auth, const std::string &host, const std::string &up, const std::string &down, const std::string &alpn, const std::string &obfsParam, const std::string &insecure ,tribool udp = tribool(), tribool tfo = tribool(), tribool scv = tribool(), tribool tls13 = tribool());
void hysteriaConstruct(Proxy &node, const std::string &group, const std::string &remarks, const std::string &add, const std::string &port, const std::string &type, const std::string &auth, const std::string &auth_str, const std::string &host, const std::string &up, const std::string &down, const std::string &alpn, const std::string &obfsParam, const std::string &insecure ,tribool udp = tribool(), tribool tfo = tribool(), tribool scv = tribool(), tribool tls13 = tribool());
void hysteria2Construct(Proxy &node, const std::string &group, const std::string &remarks, const std::string &add, const std::string &port, const std::string &password, const std::string &host, const std::string &up, const std::string &down, const std::string &alpn, const std::string &obfsParam, const std::string &obfsPassword, const std::string &insecure ,tribool udp = tribool(), tribool tfo = tribool(), tribool scv = tribool(), tribool tls13 = tribool());
void vlessConstruct(Proxy &node, const std::string &group, const std::string &remarks, const std::string &add, const std::string &port, const std::string &type, const std::string &id, const std::string &aid, const std::string &net, const std::string &cipher, const std::string &flow, const std::string &mode, const std::string &path, const std::string &host, const std::string &edge, const std::string &tls,const std::string &pkd, const std::string &sid, const std::string &fp, tribool udp = tribool(), tribool tfo = tribool(), tribool scv = tribool(), tribool tls13 = tribool());
void vmessConstruct(Proxy &node, const std::string &group, const std::string &remarks, const std::string &add, const std::string &port, const std::string &type, const std::string &id, const std::string &aid, const std::string &net, const std::string &cipher, const std::string &path, const std::string &host, const std::string &edge, const std::string &tls, const std::string &sni, tribool udp = tribool(), tribool tfo = tribool(), tribool scv = tribool(), tribool tls13 = tribool());