diff --git a/src/main.cpp b/src/main.cpp index d88c32a..ef84226 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -353,13 +353,17 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) //for external configuration std::string ext_clash_base = clash_rule_base, ext_surge_base = surge_rule_base, ext_mellow_base = mellow_rule_base, ext_surfboard_base = surfboard_rule_base; + //validate urls if(!url.size()) url = default_url; if(!url.size() || !target.size()) return "Invalid request!"; + + //check if we need to read configuration if(!api_mode || cfw_child_process) readConf(); + //check for proxy settings std::string proxy; if(proxy_subscription == "SYSTEM") proxy = getSystemProxy(); @@ -368,6 +372,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) else proxy = proxy_subscription; + //load external configuration if(config.size()) { std::cerr<<"External configuration file provided. Loading...\n"; @@ -398,6 +403,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) } else { + //loading custom groups if(groups.size()) { extra_group = split(groups, "@"); @@ -407,6 +413,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) else extra_group = clash_extra_group; + //loading custom rulesets if(ruleset.size()) { extra_ruleset = split(ruleset, "@"); @@ -429,6 +436,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) } } + //check other flags if(emoji.size()) { ext.add_emoji = ext.remove_emoji = emoji == "true"; @@ -451,10 +459,12 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) ext.nodelist = nodelist == "true"; ext.surge_ssr_path = surge_ssr_path; + //loading urls string_array urls = split(url, "|"); std::vector nodes; int groupID = 0; + //check custom include/exclude settings if(include.size() && regValid(include)) include_remarks.emplace_back(include); else @@ -464,8 +474,11 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) else exclude_remarks = def_exclude_remarks; + //check custom group name if(group.size()) custom_group = group; + + //start parsing urls for(std::string &x : urls) { x = trim(x); @@ -473,6 +486,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) addNodes(x, nodes, groupID, proxy, exclude_remarks, include_remarks); groupID++; } + //exit if found nothing if(!nodes.size()) return "No nodes were found!"; @@ -490,7 +504,11 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) output_content = netchToClash(nodes, base_content, rca, extra_group, target == "clashr", ext); } else - output_content = YAML::Dump(netchToClash(nodes, clash_base, extra_group, target == "clashr", ext)); + { + YAML::Node yamlnode = clash_base; + netchToClash(nodes, yamlnode, extra_group, target == "clashr", ext); + output_content = YAML::Dump(yamlnode); + } if(upload == "true") uploadGist(target, upload_path, output_content, false); @@ -654,7 +672,9 @@ std::string simpleToClashR(RESPONSE_CALLBACK_ARGS) std::cerr<<"Generate target: ClashR\n"; - return YAML::Dump(netchToClash(nodes, clash_base, extra_group, true, ext)); + YAML::Node yamlnode = clash_base; + netchToClash(nodes, yamlnode, extra_group, true, ext); + return YAML::Dump(yamlnode); } void chkArg(int argc, char *argv[]) diff --git a/src/misc.cpp b/src/misc.cpp index bb352ff..6c1de08 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -689,12 +689,12 @@ int fileWrite(std::string path, std::string content, bool overwrite) return 0; } -bool isIPv4(std::string address) +bool isIPv4(std::string &address) { return regMatch(address, "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); } -bool isIPv6(std::string address) +bool isIPv6(std::string &address) { std::vector regLists = {"^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$", "^((?:[0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4})*)?)::((?:([0-9A-Fa-f]{1,4}:)*[0-9A-Fa-f]{1,4})?)$", "^(::(?:[0-9A-Fa-f]{1,4})(?::[0-9A-Fa-f]{1,4}){5})|((?:[0-9A-Fa-f]{1,4})(?::[0-9A-Fa-f]{1,4}){5}::)$"}; for(unsigned int i = 0; i < regLists.size(); i++) diff --git a/src/misc.h b/src/misc.h index 2e62315..2364dd1 100644 --- a/src/misc.h +++ b/src/misc.h @@ -46,8 +46,8 @@ std::string regReplace(std::string src, std::string match, std::string rep); bool regMatch(std::string src, std::string match); std::string speedCalc(double speed); std::string getMD5(std::string data); -bool isIPv4(std::string address); -bool isIPv6(std::string address); +bool isIPv4(std::string &address); +bool isIPv6(std::string &address); void urlParse(std::string url, std::string &host, std::string &path, int &port, bool &isTLS); void removeUTF8BOM(std::string &data); int shortAssemble(unsigned short num_a, unsigned short num_b); diff --git a/src/subexport.cpp b/src/subexport.cpp index dfcf408..b1c6625 100644 --- a/src/subexport.cpp +++ b/src/subexport.cpp @@ -21,6 +21,9 @@ extern bool add_emoji, remove_old_emoji; extern bool api_mode; extern string_array ss_ciphers, ssr_ciphers; +string_array clashr_protocols = {"auth_aes128_md5", "auth_aes128_sha1"}; +string_array clashr_obfs = {"plain", "http_simple", "http_post", "tls1.2_ticket_auth"}; + /* std::string hostnameToIPAddr(std::string host) { @@ -482,7 +485,7 @@ void groupGenerate(std::string &rule, std::vector &nodelist, std::vect } } -YAML::Node netchToClash(std::vector &nodes, YAML::Node &yamlnode, string_array &extra_proxy_group, bool clashR, extra_settings &ext) +void netchToClash(std::vector &nodes, YAML::Node &yamlnode, string_array &extra_proxy_group, bool clashR, extra_settings &ext) { try_config_lock(); YAML::Node proxies, singleproxy, singlegroup, original_groups; @@ -534,6 +537,9 @@ YAML::Node netchToClash(std::vector &nodes, YAML::Node &yamlnode, stri if(x.linkType == SPEEDTEST_MESSAGE_FOUNDSS) { + //latest clash core removed support for chacha20 encryption + if(method == "chacha20") + continue; plugin = GetMember(json, "Plugin"); pluginopts = replace_all_distinct(GetMember(json, "PluginOption"), ";", "&"); singleproxy["type"] = "ss"; @@ -582,9 +588,17 @@ YAML::Node netchToClash(std::vector &nodes, YAML::Node &yamlnode, stri } else if(x.linkType == SPEEDTEST_MESSAGE_FOUNDSSR && clashR) { + //latest clash core removed support for chacha20 encryption + if(method == "chacha20") + continue; + //ignoring all nodes with unsupported obfs and protocols protocol = GetMember(json, "Protocol"); - protoparam = GetMember(json, "ProtocolParam"); + if(std::find(clashr_protocols.cbegin(), clashr_protocols.cend(), protocol) == clashr_protocols.cend()) + continue; obfs = GetMember(json, "OBFS"); + if(std::find(clashr_obfs.cbegin(), clashr_obfs.cend(), obfs) == clashr_obfs.cend()) + continue; + protoparam = GetMember(json, "ProtocolParam"); obfsparam = GetMember(json, "OBFSParam"); singleproxy["type"] = "ssr"; singleproxy["cipher"] = method; @@ -622,7 +636,8 @@ YAML::Node netchToClash(std::vector &nodes, YAML::Node &yamlnode, stri { YAML::Node provider; provider["proxies"] = proxies; - return provider; + yamlnode = provider; + return; } yamlnode["Proxy"] = proxies; @@ -679,8 +694,6 @@ YAML::Node netchToClash(std::vector &nodes, YAML::Node &yamlnode, stri } yamlnode["Proxy Group"] = original_groups; - - return yamlnode; } std::string netchToClash(std::vector &nodes, std::string &base_conf, std::vector &ruleset_content_array, string_array &extra_proxy_group, bool clashR, extra_settings &ext) @@ -696,7 +709,7 @@ std::string netchToClash(std::vector &nodes, std::string &base_conf, s return std::string(); } - yamlnode = netchToClash(nodes, yamlnode, extra_proxy_group, clashR, ext); + netchToClash(nodes, yamlnode, extra_proxy_group, clashR, ext); if(ext.nodelist) return YAML::Dump(yamlnode); @@ -802,6 +815,8 @@ std::string netchToSurge(std::vector &nodes, std::string &base_conf, s { proxy += ", ws=true, ws-path=" + path + ", ws-headers=Host:" + host; } + if(ext.skip_cert_verify) + proxy += ", skip-cert-verify=1"; else if(transproto == "kcp" || transproto == "h2" || transproto == "quic") continue; } @@ -995,7 +1010,10 @@ std::string netchToSS(std::vector &nodes, extra_settings &ext) allLinks += proxyStr + "\n"; } - return base64_encode(allLinks); + if(ext.nodelist) + return allLinks; + else + return base64_encode(allLinks); } std::string netchToSSR(std::vector &nodes, extra_settings &ext) @@ -1175,6 +1193,8 @@ std::string netchToQuan(std::vector &nodes, extra_settings &ext) proxyStr += ", obfs=ws, obfs-path=" + path + ", obfs-header=\"Host: " + host + "\""; if(tlssecure) proxyStr += ", over-tls=true, tls-host=" + host; + if(ext.skip_cert_verify) + proxyStr += ", certificate=0"; proxyStr = "vmess://" + urlsafe_base64_encode(proxyStr); break; case SPEEDTEST_MESSAGE_FOUNDSSR: @@ -1259,6 +1279,8 @@ std::string netchToQuanX(std::vector &nodes, extra_settings &ext) proxyStr = "vmess = " + hostname + ":" + port + ", method=" + method + ", password=" + id; if(transproto == "ws") proxyStr += ", obfs=ws, obfs-host=" + host + ", obfs-uri=" + path; + if(ext.skip_cert_verify) + proxyStr += ", certificate=0"; break; case SPEEDTEST_MESSAGE_FOUNDSS: password = GetMember(json, "Password"); @@ -1304,6 +1326,7 @@ std::string netchToSSD(std::vector &nodes, std::string &group, extra_s std::string plugin, pluginopts; std::string protocol, protoparam, obfs, obfsparam; int port, index = 0; + if(!group.size()) group = "SSD"; diff --git a/src/subexport.h b/src/subexport.h index eabcfba..ad07075 100644 --- a/src/subexport.h +++ b/src/subexport.h @@ -27,7 +27,7 @@ struct extra_settings void rulesetToClash(YAML::Node &base_rule, std::vector &ruleset_content_array); void rulesetToSurge(INIReader &base_rule, std::vector &ruleset_content_array, int surge_ver); std::string netchToClash(std::vector &nodes, std::string &base_conf, std::vector &ruleset_content_array, string_array &extra_proxy_group, bool clashR, extra_settings &ext); -YAML::Node netchToClash(std::vector &nodes, YAML::Node &base, string_array &extra_proxy_group, bool clashR, extra_settings &ext); +void netchToClash(std::vector &nodes, YAML::Node &base, string_array &extra_proxy_group, bool clashR, extra_settings &ext); std::string netchToSurge(std::vector &nodes, std::string &base_conf, std::vector &ruleset_content_array, string_array &extra_proxy_group, int surge_ver, extra_settings &ext); std::string netchToMellow(std::vector &nodes, std::string &base_conf, std::vector &ruleset_content_array, string_array &extra_proxy_group, extra_settings &ext); void netchToMellow(std::vector &nodes, INIReader &ini, std::vector &ruleset_content_array, string_array &extra_proxy_group, extra_settings &ext);