Skip to content

Commit

Permalink
Add underlying-proxy, smart policy group support for surge and renami…
Browse files Browse the repository at this point in the history
…ng surge wireguard section (#747)

* Use short hash of wireguard name for surge section name
Add underlying-proxy support for clash parser and surge exporter

* Add surge smart group support

* Allow clash to fallback to url-test in smart proxygroup

* Fix duplicate break statement in binding.h
  • Loading branch information
mrhaoxx authored Sep 29, 2024
1 parent 644b904 commit 79a7e88
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ subconverter.exe
cmake-build-debug
.idea
base/cache
build
13 changes: 13 additions & 0 deletions src/config/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ namespace toml
case "ssid"_hash:
conf.Type = ProxyGroupType::SSID;
break;
case "smart"_hash:
conf.Type = ProxyGroupType::Smart;
conf.Url = toml::find<String>(v, "url");
conf.Interval = toml::find<Integer>(v, "interval");
conf.Tolerance = toml::find_or<Integer>(v, "tolerance", 0);
if(v.contains("lazy"))
conf.Lazy = toml::find_or<bool>(v, "lazy", false);
if(v.contains("evaluate-before-use"))
conf.EvaluateBeforeUse = toml::find_or(v, "evaluate-before-use", conf.EvaluateBeforeUse.get());
break;
default:
throw toml::syntax_error("Proxy Group has incorrect type, should be one of following:\n select, url-test, load-balance, fallback, relay, ssid", v.at("type").location());
}
Expand Down Expand Up @@ -220,6 +230,9 @@ namespace INIBinding
case "ssid"_hash:
conf.Type = ProxyGroupType::SSID;
break;
case "smart"_hash:
conf.Type = ProxyGroupType::Smart;
break;
default:
continue;
}
Expand Down
4 changes: 3 additions & 1 deletion src/config/proxygroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ enum class ProxyGroupType
Fallback,
LoadBalance,
Relay,
SSID
SSID,
Smart
};

enum class BalanceStrategy
Expand Down Expand Up @@ -45,6 +46,7 @@ struct ProxyGroupConfig
case ProxyGroupType::Fallback: return "fallback";
case ProxyGroupType::Relay: return "relay";
case ProxyGroupType::SSID: return "ssid";
case ProxyGroupType::Smart: return "smart";
}
return "";
}
Expand Down
22 changes: 17 additions & 5 deletions src/generator/config/subexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,10 @@ void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGr
string_array filtered_nodelist;

singlegroup["name"] = x.Name;
singlegroup["type"] = x.TypeStr();
if (x.Type == ProxyGroupType::Smart)
singlegroup["type"] = "url-test";
else
singlegroup["type"] = x.TypeStr();

switch(x.Type)
{
Expand All @@ -528,6 +531,8 @@ void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGr
case ProxyGroupType::LoadBalance:
singlegroup["strategy"] = x.StrategyStr();
[[fallthrough]];
case ProxyGroupType::Smart:
[[fallthrough]];
case ProxyGroupType::URLTest:
if(!x.Lazy.is_undef())
singlegroup["lazy"] = x.Lazy.get();
Expand Down Expand Up @@ -691,7 +696,7 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf

processRemark(x.Remark, remarks_list);

std::string &hostname = x.Hostname, &username = x.Username, &password = x.Password, &method = x.EncryptMethod, &id = x.UserId, &transproto = x.TransferProtocol, &host = x.Host, &edge = x.Edge, &path = x.Path, &protocol = x.Protocol, &protoparam = x.ProtocolParam, &obfs = x.OBFS, &obfsparam = x.OBFSParam, &plugin = x.Plugin, &pluginopts = x.PluginOption;
std::string &hostname = x.Hostname, &username = x.Username, &password = x.Password, &method = x.EncryptMethod, &id = x.UserId, &transproto = x.TransferProtocol, &host = x.Host, &edge = x.Edge, &path = x.Path, &protocol = x.Protocol, &protoparam = x.ProtocolParam, &obfs = x.OBFS, &obfsparam = x.OBFSParam, &plugin = x.Plugin, &pluginopts = x.PluginOption, &underlying_proxy = x.UnderlyingProxy;
std::string port = std::to_string(x.Port);
bool &tlssecure = x.TLSSecure;

Expand All @@ -704,7 +709,9 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf
std::string proxy, section, real_section;
string_array args, headers;

switch(x.Type)
std::stringstream ss;

switch (x.Type)
{
case ProxyType::Shadowsocks:
if(surge_ver >= 3 || surge_ver == -3)
Expand Down Expand Up @@ -833,7 +840,8 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf
case ProxyType::WireGuard:
if(surge_ver < 4 && surge_ver != -3)
continue;
section = randomStr(5);
ss << std::hex << hash_(x.Remark);
section = ss.str().substr(0, 5);
real_section = "WireGuard " + section;
proxy = "wireguard, section-name=" + section;
if(!x.TestUrl.empty())
Expand Down Expand Up @@ -861,7 +869,10 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf
if(!udp.is_undef())
proxy += ", udp-relay=" + udp.get_str();

if(ext.nodelist)
if (underlying_proxy != "")
proxy += ", underlying-proxy=" + underlying_proxy;

if (ext.nodelist)
output_nodelist += x.Remark + " = " + proxy + "\n";
else
{
Expand All @@ -884,6 +895,7 @@ std::string proxyToSurge(std::vector<Proxy> &nodes, const std::string &base_conf
switch(x.Type)
{
case ProxyGroupType::Select:
case ProxyGroupType::Smart:
case ProxyGroupType::URLTest:
case ProxyGroupType::Fallback:
break;
Expand Down
2 changes: 2 additions & 0 deletions src/parser/config/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ struct Proxy
tribool AllowInsecure;
tribool TLS13;

String UnderlyingProxy;

uint16_t SnellVersion = 0;
String ServerName;

Expand Down
Loading

0 comments on commit 79a7e88

Please sign in to comment.