From d13788e1d1e1457399f7445176d0bba849597c9c Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Sun, 19 Jan 2020 16:32:26 +0800 Subject: [PATCH] Enhancements Improve compatibility with complex rule when directly adding rule with surge_ruleset. Improve compatibility with incorrect format of single links. Optimize codes. --- src/ini_reader.h | 2 +- src/speedtestutil.cpp | 124 +----------------------------------------- src/subexport.cpp | 9 ++- 3 files changed, 10 insertions(+), 125 deletions(-) diff --git a/src/ini_reader.h b/src/ini_reader.h index 6cff585..0f84c3f 100644 --- a/src/ini_reader.h +++ b/src/ini_reader.h @@ -181,7 +181,7 @@ public: //remove UTF-8 BOM if(content.compare(0, 3, "\xEF\xBB\xBF") == 0) - content = content.substr(3); + content.erase(0, 3); bool inExcludedSection = false; std::string strLine, thisSection, curSection, itemName, itemVal; diff --git a/src/speedtestutil.cpp b/src/speedtestutil.cpp index 724b4cb..6557a8c 100644 --- a/src/speedtestutil.cpp +++ b/src/speedtestutil.cpp @@ -13,18 +13,7 @@ using namespace rapidjson; using namespace YAML; -/* -std::string base_ss_win = "{\"version\":\"4.1.6\",\"configs\":[?config?],\"strategy\":null,\"index\":0,\"global\":false,\"enabled\":false,\"shareOverLan\":true,\"isDefault\":false,\"localPort\":?localport?,\"portableMode\":true,\"pacUrl\":null,\"useOnlinePac\":false,\"secureLocalPac\":true,\"availabilityStatistics\":false,\"autoCheckUpdate\":true,\"checkPreRelease\":false,\"isVerboseLogging\":false,\"logViewer\":{\"topMost\":false,\"wrapText\":false,\"toolbarShown\":false,\"Font\":\"Consolas, 8pt\",\"BackgroundColor\":\"Black\",\"TextColor\":\"White\"},\"proxy\":{\"useProxy\":false,\"proxyType\":0,\"proxyServer\":\"\",\"proxyPort\":0,\"proxyTimeout\":3,\"useAuth\":false,\"authUser\":\"\",\"authPwd\":\"\"},\"hotkey\":{\"SwitchSystemProxy\":\"\",\"SwitchSystemProxyMode\":\"\",\"SwitchAllowLan\":\"\",\"ShowLogs\":\"\",\"ServerMoveUp\":\"\",\"ServerMoveDown\":\"\",\"RegHotkeysAtStartup\":false}}"; -std::string config_ss_win = "{\"server\":\"?server?\",\"server_port\":?port?,\"password\":\"?password?\",\"method\":\"?method?\",\"plugin\":\"?plugin?\",\"plugin_opts\":\"?plugin_opts?\",\"plugin_args\":\"\",\"remarks\":\"?remarks?\",\"timeout\":5}"; -std::string config_ss_libev = "{\"server\":\"?server?\",\"server_port\":?port?,\"password\":\"?password?\",\"method\":\"?method?\",\"plugin\":\"?plugin?\",\"plugin_opts\":\"?plugin_opts?\",\"plugin_args\":\"\",\"local_address\":\"127.0.0.1\",\"local_port\":?localport?}"; -std::string base_ssr_win = "{\"configs\":[?config?],\"index\":0,\"random\":true,\"sysProxyMode\":1,\"shareOverLan\":false,\"localPort\":?localport?,\"localAuthPassword\":null,\"localDnsServer\":\"\",\"dnsServer\":\"\",\"reconnectTimes\":2,\"balanceAlgorithm\":\"LowException\",\"randomInGroup\":false,\"TTL\":0,\"connectTimeout\":5,\"proxyRuleMode\":2,\"proxyEnable\":false,\"pacDirectGoProxy\":false,\"proxyType\":0,\"proxyHost\":null,\"proxyPort\":0,\"proxyAuthUser\":null,\"proxyAuthPass\":null,\"proxyUserAgent\":null,\"authUser\":null,\"authPass\":null,\"autoBan\":false,\"checkSwitchAutoCloseAll\":false,\"logEnable\":false,\"sameHostForSameTarget\":false,\"keepVisitTime\":180,\"isHideTips\":false,\"nodeFeedAutoUpdate\":true,\"serverSubscribes\":[],\"token\":{},\"portMap\":{}}"; -std::string config_ssr_win = "{\"remarks\":\"?remarks?\",\"id\":\"18C4949EBCFE46687AE4A7645725D35F\",\"server\":\"?server?\",\"server_port\":?port?,\"server_udp_port\":0,\"password\":\"?password?\",\"method\":\"?method?\",\"protocol\":\"?protocol?\",\"protocolparam\":\"?protoparam?\",\"obfs\":\"?obfs?\",\"obfsparam\":\"?obfsparam?\",\"remarks_base64\":\"?remarks_base64?\",\"group\":\"?group?\",\"enable\":true,\"udp_over_tcp\":false}"; -std::string config_ssr_libev = "{\"server\":\"?server?\",\"server_port\":?port?,\"protocol\":\"?protocol?\",\"method\":\"?method?\",\"obfs\":\"?obfs?\",\"password\":\"?password?\",\"obfs_param\":\"?obfsparam?\",\"protocol_param\":\"?protoparam?\",\"local_address\":\"127.0.0.1\",\"local_port\":?localport?}"; -std::string base_vmess = "{\"inbounds\":[{\"port\":?localport?,\"listen\":\"127.0.0.1\",\"protocol\":\"socks\"}],\"outbounds\":[{\"tag\":\"proxy\",\"protocol\":\"vmess\",\"settings\":{\"vnext\":[{\"address\":\"?add?\",\"port\":?port?,\"users\":[{\"id\":\"?id?\",\"alterId\":?aid?,\"email\":\"t@t.tt\",\"security\":\"?cipher?\"}]}]},\"streamSettings\":{\"network\":\"?net?\",\"security\":\"?tls?\",\"tlsSettings\":?tlsset?,\"tcpSettings\":?tcpset?,\"wsSettings\":?wsset?},\"mux\":{\"enabled\":true}}],\"routing\":{\"domainStrategy\":\"IPIfNonMatch\"}}"; -std::string wsset_vmess = "{\"connectionReuse\":true,\"path\":\"?path?\",\"headers\":{\"Host\":\"?host?\"}}"; -std::string tcpset_vmess = "{\"connectionReuse\":true,\"header\":{\"type\":\"?type?\",\"request\":{\"version\":\"1.1\",\"method\":\"GET\",\"path\":[\"?path?\"],\"headers\":{\"Host\":[\"?host?\"],\"User-Agent\":[\"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36\",\"Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/53.0.2785.109 Mobile/14A456 Safari/601.1.46\"],\"Accept-Encoding\":[\"gzip, deflate\"],\"Connection\":[\"keep-alive\"],\"Pragma\":\"no-cache\"}}}}"; -std::string tlsset_vmess = "{\"serverName\":\"?serverName?\",\"allowInsecure\":false,\"allowInsecureCiphers\":false}"; -*/ + string_array ss_ciphers = {"rc4-md5", "aes-128-gcm", "aes-192-gcm", "aes-256-gcm", "aes-128-cfb", "aes-192-cfb", "aes-256-cfb", "aes-128-ctr", "aes-192-ctr", "aes-256-ctr", "camellia-128-cfb", "camellia-192-cfb", "camellia-256-cfb", "bf-cfb", "chacha20-ietf-poly1305", "xchacha20-ietf-poly1305", "salsa20", "chacha20", "chacha20-ietf"}; string_array ssr_ciphers = {"none", "table", "rc4", "rc4-md5", "aes-128-cfb", "aes-192-cfb", "aes-256-cfb", "aes-128-ctr", "aes-192-ctr", "aes-256-ctr", "bf-cfb", "camellia-128-cfb", "camellia-192-cfb", "camellia-256-cfb", "cast5-cfb", "des-cfb", "idea-cfb", "rc2-cfb", "seed-cfb", "salsa20", "chacha20", "chacha20-ietf"}; @@ -37,115 +26,6 @@ template void operator >> (const YAML::Node& node, T& i) { i = node.as(); }; -/* -std::string vmessConstruct(std::string add, std::string port, std::string type, std::string id, std::string aid, std::string net, std::string cipher, std::string path, std::string host, std::string tls, int local_port) -{ - std::string base = base_vmess; - std::string wsset = wsset_vmess; - std::string tcpset = tcpset_vmess; - std::string tlsset = tlsset_vmess; - base = replace_all_distinct(base, "?localport?", to_string(local_port)); - base = replace_all_distinct(base, "?add?", add); - base = replace_all_distinct(base, "?port?", port); - base = replace_all_distinct(base, "?id?", id); - base = replace_all_distinct(base, "?aid?", aid); - base = replace_all_distinct(base, "?net?", net); - base = replace_all_distinct(base, "?cipher?", cipher); - if(net == "ws") - { - wsset = replace_all_distinct(wsset, "?host?", host == "" ? add : host); - wsset = replace_all_distinct(wsset, "?path?", path == "" ? "/" : path); - base = replace_all_distinct(base, "?wsset?", wsset); - } - if(type == "http") - { - tcpset = replace_all_distinct(tcpset, "?host?", host == "" ? add : host); - tcpset = replace_all_distinct(tcpset, "?type?", type); - tcpset = replace_all_distinct(tcpset, "?path?", path == "" ? "/" : path); - base = replace_all_distinct(base, "?tcpset?", tcpset); - } - if(host == "" && !isIPv4(add) && !isIPv6(add)) - host = add; - if(host != "") - { - tlsset = replace_all_distinct(tlsset, "?serverName?", host); - base = replace_all_distinct(base, "?tlsset?", tlsset); - } - - base = replace_all_distinct(base, "?tls?", tls); - base = replace_all_distinct(base, "?tcpset?", "null"); - base = replace_all_distinct(base, "?wsset?", "null"); - base = replace_all_distinct(base, "?tlsset?", "null"); - - return base; -} - -std::string ssrConstruct(std::string group, std::string remarks, std::string remarks_base64, std::string server, std::string port, std::string protocol, std::string method, std::string obfs, std::string password, std::string obfsparam, std::string protoparam, int local_port, bool libev) -{ - std::string base = base_ssr_win; - std::string config = config_ssr_win; - std::string config_libev = config_ssr_libev; - if(libev == true) - config = config_libev; - if(isIPv6(server)) - server = "[" + server + "]"; - config = replace_all_distinct(config, "?group?", group); - config = replace_all_distinct(config, "?remarks?", remarks); - config = replace_all_distinct(config, "?remarks_base64?", remarks_base64); - config = replace_all_distinct(config, "?server?", server); - config = replace_all_distinct(config, "?port?", port); - config = replace_all_distinct(config, "?protocol?", protocol); - config = replace_all_distinct(config, "?method?", method); - config = replace_all_distinct(config, "?obfs?", obfs); - config = replace_all_distinct(config, "?password?", password); - config = replace_all_distinct(config, "?obfsparam?", obfsparam); - config = replace_all_distinct(config, "?protoparam?", protoparam); - if(libev == true) - base = config; - else - base = replace_all_distinct(base, "?config?", config); - base = replace_all_distinct(base, "?localport?", to_string(local_port)); - - return base; -} - -std::string ssConstruct(std::string server, std::string port, std::string password, std::string method, std::string plugin, std::string pluginopts, std::string remarks, int local_port, bool libev) -{ - std::string base = base_ss_win; - std::string config = config_ss_win; - std::string config_libev = config_ss_libev; - if(plugin == "obfs-local") - plugin = "simple-obfs"; - if(libev == true) - config = config_libev; - if(isIPv6(server)) - server = "[" + server + "]"; - config = replace_all_distinct(config, "?server?", server); - config = replace_all_distinct(config, "?port?", port); - config = replace_all_distinct(config, "?password?", password); - config = replace_all_distinct(config, "?method?", method); - config = replace_all_distinct(config, "?plugin?", plugin); - config = replace_all_distinct(config, "?plugin_opts?", pluginopts); - config = replace_all_distinct(config, "?remarks?", remarks); - if(libev == true) - base = config; - else - base = replace_all_distinct(base, "?config?", config); - base = replace_all_distinct(base, "?localport?", to_string(local_port)); - - return base; -} - -std::string socksConstruct(std::string remarks, std::string server, std::string port, std::string username, std::string password) -{ - return "user=" + username + "&pass=" + password; -} - -std::string httpConstruct(std::string remarks, std::string server, std::string port, std::string username, std::string password) -{ - return "user=" + username + "&pass=" + password; -} -*/ void explodeVmess(std::string vmess, std::string custom_port, int local_port, nodeInfo &node) { @@ -358,6 +238,8 @@ void explodeSS(std::string ss, bool libev, std::string custom_port, int local_po } else { + if(!regMatch(urlsafe_base64_decode(ss), "(.*?):(.*?)@(.*):(.*)")) + return; ss = regReplace(urlsafe_base64_decode(ss), "(.*?):(.*?)@(.*):(.*)", "$1|$2|$3|$4"); args = split(ss, "|"); method = args[0]; diff --git a/src/subexport.cpp b/src/subexport.cpp index 087a143..b5a45cc 100644 --- a/src/subexport.cpp +++ b/src/subexport.cpp @@ -313,9 +313,12 @@ void rulesetToClash(YAML::Node &base_rule, std::vector &ruleset if(retrived_rules.find("[]") == 0) { strLine = retrived_rules.substr(2); - if(strLine == "FINAL") - strLine = "MATCH"; - allRules.emplace_back(strLine + "," + rule_group); + if(strLine.find("FINAL") == 0) + strLine.replace(0, 5, "MATCH"); + strLine += "," + rule_group; + if(std::count(strLine.begin(), strLine.end(), ',') > 2) + strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2"); + allRules.emplace_back(strLine); continue; } char delimiter = count(retrived_rules.begin(), retrived_rules.end(), '\n') < 1 ? '\r' : '\n';