add vless to singbox

This commit is contained in:
asdlokj1qpi23 committed 2023-12-18 11:18:07 +08:00
1 parent 615b3a404e
commit 4563be3d45
1 file changed
+74 -67
+74 -67
View File
@@ -2075,6 +2075,16 @@ static void addSingBoxCommonMembers(rapidjson::Value &proxy, const Proxy &x,
proxy.AddMember("server_port", x.Port, allocator); proxy.AddMember("server_port", x.Port, allocator);
} }
static void addHeaders(rapidjson::Value &transport, const Proxy &x,
rapidjson::MemoryPoolAllocator<> &allocator) {
rapidjson::Value headers(rapidjson::kObjectType);
if (!x.Host.empty())
headers.AddMember("Host", rapidjson::StringRef(x.Host.c_str()), allocator);
if (!x.Edge.empty())
headers.AddMember("Edge", rapidjson::StringRef(x.Edge.c_str()), allocator);
transport.AddMember("headers", headers, allocator);
}
static rapidjson::Value stringArrayToJsonArray(const std::string &array, const std::string &delimiter, static rapidjson::Value stringArrayToJsonArray(const std::string &array, const std::string &delimiter,
rapidjson::MemoryPoolAllocator<> &allocator) { rapidjson::MemoryPoolAllocator<> &allocator) {
rapidjson::Value result(rapidjson::kArrayType); rapidjson::Value result(rapidjson::kArrayType);
@@ -2151,64 +2161,54 @@ proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector
break; break;
} }
// TODO VLESS后续支持 // TODO VLESS后续支持
// case ProxyType::VLESS: { case ProxyType::VLESS: {
// addSingBoxCommonMembers(proxy, x, "vmess", allocator); addSingBoxCommonMembers(proxy, x, "vmess", allocator);
// proxy.AddMember("uuid", rapidjson::StringRef(x.UserId.c_str()), allocator); proxy.AddMember("uuid", rapidjson::StringRef(x.UserId.c_str()), allocator);
// proxy.AddMember("tls", rapidjson::StringRef((std::string ("{\"enabled\": ") + std::string ((x.TLSSecure ? "true" : "false")) + "}").c_str()), allocator); if (xudp && udp)
// if (!tfo.is_undef()) proxy.AddMember("packet_encoding", rapidjson::StringRef("xudp"), allocator);
// singleproxy["tfo"] = tfo.get(); if (!x.Host.empty())
// if (xudp && udp) proxy.AddMember("server", rapidjson::StringRef(x.Host.c_str()), allocator);
// singleproxy["xudp"] = true; if (!x.Flow.empty())
// if (!x.Host.empty()) proxy.AddMember("flow", rapidjson::StringRef(x.Flow.c_str()), allocator);
// singleproxy["servername"] = x.Host; rapidjson::Value transport(rapidjson::kObjectType);
// if (!x.Flow.empty()) switch (hash_(x.TransferProtocol)) {
// singleproxy["flow"] = x.Flow; case "tcp"_hash:
// if (!scv.is_undef()) proxy.AddMember("network", rapidjson::StringRef("ws"), allocator);
// singleproxy["skip-cert-verify"] = scv.get(); break;
// switch (hash_(x.TransferProtocol)) { case "ws"_hash:
// case "tcp"_hash: transport.AddMember("type", rapidjson::StringRef("ws"), allocator);
// break; transport.AddMember("host", rapidjson::StringRef(x.Host.c_str()), allocator);
// case "ws"_hash: transport.AddMember("path", rapidjson::StringRef(x.Path.c_str()), allocator);
// singleproxy["network"] = x.TransferProtocol; addHeaders(transport, x, allocator);
// if (ext.clash_new_field_name) { proxy.AddMember("transport", transport, allocator);
// singleproxy["ws-opts"]["path"] = x.Path; break;
// if (!x.Host.empty()) case "http"_hash:
// singleproxy["ws-opts"]["headers"]["Host"] = x.Host; transport.AddMember("type", rapidjson::StringRef("http"), allocator);
// if (!x.Edge.empty()) transport.AddMember("host", rapidjson::StringRef(x.Host.c_str()), allocator);
// singleproxy["ws-opts"]["headers"]["Edge"] = x.Edge; transport.AddMember("method", rapidjson::StringRef("GET"), allocator);
// } else { transport.AddMember("path", rapidjson::StringRef(x.Path.c_str()), allocator);
// singleproxy["ws-path"] = x.Path; addHeaders(transport, x, allocator);
// if (!x.Host.empty()) proxy.AddMember("transport", transport, allocator);
// singleproxy["ws-headers"]["Host"] = x.Host; break;
// if (!x.Edge.empty()) case "h2"_hash:
// singleproxy["ws-headers"]["Edge"] = x.Edge; transport.AddMember("type", rapidjson::StringRef("httpupgrade"), allocator);
// } transport.AddMember("host", rapidjson::StringRef(x.Host.c_str()), allocator);
// break; transport.AddMember("path", rapidjson::StringRef(x.Path.c_str()), allocator);
// case "http"_hash: proxy.AddMember("transport", transport, allocator);
// singleproxy["network"] = x.TransferProtocol; break;
// singleproxy["http-opts"]["method"] = "GET"; case "grpc"_hash:
// singleproxy["http-opts"]["path"].push_back(x.Path); transport.AddMember("type", rapidjson::StringRef("grpc"), allocator);
// if (!x.Host.empty()) transport.AddMember("service_name", rapidjson::StringRef(x.GRPCServiceName.c_str()), allocator);
// singleproxy["http-opts"]["headers"]["Host"].push_back(x.Host); if (!x.GRPCMode.empty()) {
// if (!x.Edge.empty()) transport.AddMember("permit_without_stream", rapidjson::StringRef("true"), allocator);
// singleproxy["http-opts"]["headers"]["Edge"].push_back(x.Edge); }
// break; proxy.AddMember("transport", transport, allocator);
// case "h2"_hash: break;
// singleproxy["network"] = x.TransferProtocol; default:
// singleproxy["h2-opts"]["path"] = x.Path; continue;
// if (!x.Host.empty()) }
// singleproxy["h2-opts"]["host"].push_back(x.Host); break;
// break; }
// case "grpc"_hash:
// singleproxy["network"] = x.TransferProtocol;
// singleproxy["grpc-opts"]["grpc-mode"] = x.GRPCMode;
// singleproxy["grpc-opts"]["grpc-service-name"] = x.GRPCServiceName;
// break;
// default:
// continue;
// }
// break;
// }
case ProxyType::Trojan: { case ProxyType::Trojan: {
addSingBoxCommonMembers(proxy, x, "trojan", allocator); addSingBoxCommonMembers(proxy, x, "trojan", allocator);
proxy.AddMember("password", rapidjson::StringRef(x.Password.c_str()), allocator); proxy.AddMember("password", rapidjson::StringRef(x.Password.c_str()), allocator);
@@ -2274,11 +2274,13 @@ proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector
tls.AddMember("enabled", true, allocator); tls.AddMember("enabled", true, allocator);
proxy.AddMember("tls", tls, allocator); proxy.AddMember("tls", tls, allocator);
if (!tfo.is_undef()) if (!tfo.is_undef())
proxy.AddMember("tcp_fast_open", rapidjson::StringRef(std::string(tfo.get() ? "true" : "false").c_str()), allocator); proxy.AddMember("tcp_fast_open",
rapidjson::StringRef(std::string(tfo.get() ? "true" : "false").c_str()), allocator);
if (!x.FakeType.empty()) if (!x.FakeType.empty())
proxy.AddMember("network", rapidjson::StringRef(x.FakeType.c_str()), allocator); proxy.AddMember("network", rapidjson::StringRef(x.FakeType.c_str()), allocator);
if (!x.Alpn.empty()) if (!x.Alpn.empty())
proxy.AddMember("tls", rapidjson::StringRef(std::string(R"({ "enabled": true,"alpn": [)"+x.Alpn+"],}").c_str()), allocator); proxy.AddMember("tls", rapidjson::StringRef(
std::string(R"({ "enabled": true,"alpn": [)" + x.Alpn + "],}").c_str()), allocator);
if (!x.OBFSParam.empty()) if (!x.OBFSParam.empty())
proxy.AddMember("obfs", rapidjson::StringRef(x.OBFSParam.c_str()), allocator); proxy.AddMember("obfs", rapidjson::StringRef(x.OBFSParam.c_str()), allocator);
break; break;
@@ -2294,12 +2296,17 @@ proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector
if (!x.DownMbps.empty()) if (!x.DownMbps.empty())
proxy.AddMember("down_mbps", rapidjson::StringRef(x.DownMbps.c_str()), allocator); proxy.AddMember("down_mbps", rapidjson::StringRef(x.DownMbps.c_str()), allocator);
if (!x.Alpn.empty()) if (!x.Alpn.empty())
proxy.AddMember("tls", rapidjson::StringRef(std::string(R"({ "enabled": true,"alpn": [)"+x.Alpn+"],}").c_str()), allocator); proxy.AddMember("tls", rapidjson::StringRef(
if (!x.OBFSParam.empty()){ std::string(R"({ "enabled": true,"alpn": [)" + x.Alpn + "],}").c_str()), allocator);
if (!x.OBFSPassword.empty()){ if (!x.OBFSParam.empty()) {
proxy.AddMember("obfs", rapidjson::StringRef(std::string(R"({ "type": )"+x.OBFSParam+",password: \""+x.OBFSPassword+"\"}").c_str()), allocator); if (!x.OBFSPassword.empty()) {
} else{ proxy.AddMember("obfs", rapidjson::StringRef(std::string(
proxy.AddMember("obfs", rapidjson::StringRef(std::string(R"({ "type": )"+x.OBFSParam+"}").c_str()), allocator); R"({ "type": )" + x.OBFSParam + ",password: \"" + x.OBFSPassword + "\"}").c_str()),
allocator);
} else {
proxy.AddMember("obfs",
rapidjson::StringRef(std::string(R"({ "type": )" + x.OBFSParam + "}").c_str()),
allocator);
} }
} }
break; break;
@@ -2313,7 +2320,7 @@ proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector
if (!x.ServerName.empty()) if (!x.ServerName.empty())
tls.AddMember("server_name", rapidjson::StringRef(x.ServerName.c_str()), allocator); tls.AddMember("server_name", rapidjson::StringRef(x.ServerName.c_str()), allocator);
if (!x.Alpn.empty()) if (!x.Alpn.empty())
tls.AddMember("alpn", rapidjson::StringRef(("["+x.Alpn+"]").c_str()), allocator); tls.AddMember("alpn", rapidjson::StringRef(("[" + x.Alpn + "]").c_str()), allocator);
tls.AddMember("insecure", buildBooleanValue(scv), allocator); tls.AddMember("insecure", buildBooleanValue(scv), allocator);
proxy.AddMember("tls", tls, allocator); proxy.AddMember("tls", tls, allocator);
} }