speedtestutil.cpp 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <time.h>
  5. #define PCRE2_CODE_UNIT_WIDTH 8
  6. #include <pcre2.h>
  7. #include "misc.h"
  8. #include "printout.h"
  9. #include "logger.h"
  10. #include "speedtestutil.h"
  11. #include "webget.h"
  12. #include "rapidjson_extra.h"
  13. #include "ini_reader.h"
  14. #include "string_hash.h"
  15. using namespace rapidjson;
  16. using namespace YAML;
  17. 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"};
  18. 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"};
  19. std::map<std::string, std::string> parsedMD5;
  20. std::string modSSMD5 = "f7653207090ce3389115e9c88541afe0";
  21. //remake from speedtestutil
  22. template <typename T> void operator >> (const YAML::Node& node, T& i)
  23. {
  24. if(node.IsDefined() && !node.IsNull()) //fail-safe
  25. i = node.as<T>();
  26. };
  27. template <typename T> T safe_as (const YAML::Node& node)
  28. {
  29. if(node.IsDefined() && !node.IsNull())
  30. return node.as<T>();
  31. return T();
  32. };
  33. template <typename T> void operator >>= (const YAML::Node& node, T& i)
  34. {
  35. i = safe_as<T>(node);
  36. };
  37. void explodeVmess(std::string vmess, const std::string &custom_port, nodeInfo &node)
  38. {
  39. std::string version, ps, add, port, type, id, aid, net, path, host, tls;
  40. Document jsondata;
  41. std::vector<std::string> vArray;
  42. if(regMatch(vmess, "vmess://(.*?)\\?(.*)")) //shadowrocket style link
  43. {
  44. explodeShadowrocket(vmess, custom_port, node);
  45. return;
  46. }
  47. else if(regMatch(vmess, "vmess1://(.*?)\\?(.*)")) //kitsunebi style link
  48. {
  49. explodeKitsunebi(vmess, custom_port, node);
  50. return;
  51. }
  52. vmess = urlsafe_base64_decode(regReplace(vmess, "(vmess|vmess1)://", ""));
  53. if(regMatch(vmess, "(.*?) = (.*)"))
  54. {
  55. explodeQuan(vmess, custom_port, node);
  56. return;
  57. }
  58. jsondata.Parse(vmess.data());
  59. if(jsondata.HasParseError())
  60. return;
  61. version = "1"; //link without version will treat as version 1
  62. GetMember(jsondata, "v", version); //try to get version
  63. GetMember(jsondata, "ps", ps);
  64. GetMember(jsondata, "add", add);
  65. port = custom_port.size() ? custom_port : GetMember(jsondata, "port");
  66. if(port == "0")
  67. return;
  68. GetMember(jsondata, "type", type);
  69. GetMember(jsondata, "id", id);
  70. GetMember(jsondata, "aid", aid);
  71. GetMember(jsondata, "net", net);
  72. GetMember(jsondata, "tls", tls);
  73. GetMember(jsondata, "host", host);
  74. switch(to_int(version))
  75. {
  76. case 1:
  77. if(host.size())
  78. {
  79. vArray = split(host, ";");
  80. if(vArray.size() == 2)
  81. {
  82. host = vArray[0];
  83. path = vArray[1];
  84. }
  85. }
  86. break;
  87. case 2:
  88. path = GetMember(jsondata, "path");
  89. break;
  90. }
  91. node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
  92. node.group = V2RAY_DEFAULT_GROUP;
  93. node.remarks = ps;
  94. node.server = add;
  95. node.port = to_int(port, 1);
  96. node.proxyStr = vmessConstruct(node.group, ps, add, port, type, id, aid, net, "auto", path, host, "", tls);
  97. }
  98. void explodeVmessConf(std::string content, const std::string &custom_port, bool libev, std::vector<nodeInfo> &nodes)
  99. {
  100. nodeInfo node;
  101. Document json;
  102. rapidjson::Value nodejson, settings;
  103. std::string group, ps, add, port, type, id, aid, net, path, host, edge, tls, cipher, subid;
  104. tribool udp, tfo, scv;
  105. int configType, index = nodes.size();
  106. std::map<std::string, std::string> subdata;
  107. std::map<std::string, std::string>::iterator iter;
  108. std::string streamset = "streamSettings", tcpset = "tcpSettings", wsset = "wsSettings";
  109. regGetMatch(content, "((?i)streamsettings)", 2, NULL, &streamset);
  110. regGetMatch(content, "((?i)tcpsettings)", 2, NULL, &tcpset);
  111. regGetMatch(content, "((?1)wssettings)", 2, NULL, &wsset);
  112. json.Parse(content.data());
  113. if(json.HasParseError())
  114. return;
  115. try
  116. {
  117. if(json.HasMember("outbounds")) //single config
  118. {
  119. if(json["outbounds"].Size() > 0 && json["outbounds"][0].HasMember("settings") && json["outbounds"][0]["settings"].HasMember("vnext") && json["outbounds"][0]["settings"]["vnext"].Size() > 0)
  120. {
  121. nodejson = json["outbounds"][0];
  122. add = GetMember(nodejson["settings"]["vnext"][0], "address");
  123. port = custom_port.size() ? custom_port : GetMember(nodejson["settings"]["vnext"][0], "port");
  124. if(port == "0")
  125. return;
  126. if(nodejson["settings"]["vnext"][0]["users"].Size())
  127. {
  128. id = GetMember(nodejson["settings"]["vnext"][0]["users"][0], "id");
  129. aid = GetMember(nodejson["settings"]["vnext"][0]["users"][0], "alterId");
  130. cipher = GetMember(nodejson["settings"]["vnext"][0]["users"][0], "security");
  131. }
  132. if(nodejson.HasMember(streamset.data()))
  133. {
  134. net = GetMember(nodejson[streamset.data()], "network");
  135. tls = GetMember(nodejson[streamset.data()], "security");
  136. if(net == "ws")
  137. {
  138. if(nodejson[streamset.data()].HasMember(wsset.data()))
  139. settings = nodejson[streamset.data()][wsset.data()];
  140. else
  141. settings.RemoveAllMembers();
  142. path = GetMember(settings, "path");
  143. if(settings.HasMember("headers"))
  144. {
  145. host = GetMember(settings["headers"], "Host");
  146. edge = GetMember(settings["headers"], "Edge");
  147. }
  148. }
  149. if(nodejson[streamset.data()].HasMember(tcpset.data()))
  150. settings = nodejson[streamset.data()][tcpset.data()];
  151. else
  152. settings.RemoveAllMembers();
  153. if(settings.IsObject() && settings.HasMember("header"))
  154. {
  155. type = GetMember(settings["header"], "type");
  156. if(type == "http")
  157. {
  158. if(settings["header"].HasMember("request"))
  159. {
  160. if(settings["header"]["request"].HasMember("path") && settings["header"]["request"]["path"].Size())
  161. settings["header"]["request"]["path"][0] >> path;
  162. if(settings["header"]["request"].HasMember("headers"))
  163. {
  164. host = GetMember(settings["header"]["request"]["headers"], "Host");
  165. edge = GetMember(settings["header"]["request"]["headers"], "Edge");
  166. }
  167. }
  168. }
  169. }
  170. }
  171. node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
  172. node.group = V2RAY_DEFAULT_GROUP;
  173. node.remarks = add + ":" + port;
  174. node.server = add;
  175. node.port = to_int(port, 1);
  176. node.proxyStr = vmessConstruct(node.group, node.remarks, add, port, type, id, aid, net, cipher, path, host, edge, tls, udp, tfo, scv);
  177. nodes.push_back(node);
  178. }
  179. return;
  180. }
  181. }
  182. catch(std::exception & e)
  183. {
  184. writeLog(0, "VMessConf parser throws an error. Leaving...", LOG_LEVEL_WARNING);
  185. return;
  186. //ignore
  187. }
  188. //read all subscribe remark as group name
  189. for(unsigned int i = 0; i < json["subItem"].Size(); i++)
  190. subdata.insert(std::pair<std::string, std::string>(json["subItem"][i]["id"].GetString(), json["subItem"][i]["remarks"].GetString()));
  191. for(unsigned int i = 0; i < json["vmess"].Size(); i++)
  192. {
  193. if(json["vmess"][i]["address"].IsNull() || json["vmess"][i]["port"].IsNull() || json["vmess"][i]["id"].IsNull())
  194. continue;
  195. //common info
  196. json["vmess"][i]["remarks"] >> ps;
  197. json["vmess"][i]["address"] >> add;
  198. port = custom_port.size() ? custom_port : GetMember(json["vmess"][i], "port");
  199. if(port == "0")
  200. continue;
  201. json["vmess"][i]["subid"] >> subid;
  202. if(subid.size())
  203. {
  204. iter = subdata.find(subid);
  205. if(iter != subdata.end())
  206. group = iter->second;
  207. }
  208. if(ps.empty())
  209. ps = add + ":" + port;
  210. scv = GetMember(json["vmess"][i], "allowInsecure");
  211. json["vmess"][i]["configType"] >> configType;
  212. switch(configType)
  213. {
  214. case 1: //vmess config
  215. json["vmess"][i]["headerType"] >> type;
  216. json["vmess"][i]["id"] >> id;
  217. json["vmess"][i]["alterId"] >> aid;
  218. json["vmess"][i]["network"] >> net;
  219. json["vmess"][i]["path"] >> path;
  220. json["vmess"][i]["requestHost"] >> host;
  221. json["vmess"][i]["streamSecurity"] >> tls;
  222. json["vmess"][i]["security"] >> cipher;
  223. group = V2RAY_DEFAULT_GROUP;
  224. node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
  225. node.proxyStr = vmessConstruct(group, ps, add, port, type, id, aid, net, cipher, path, host, "", tls, udp, tfo, scv);
  226. break;
  227. case 3: //ss config
  228. json["vmess"][i]["id"] >> id;
  229. json["vmess"][i]["security"] >> cipher;
  230. group = SS_DEFAULT_GROUP;
  231. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  232. node.proxyStr = ssConstruct(group, ps, add, port, id, cipher, "", "", libev, udp, tfo, scv);
  233. break;
  234. case 4: //socks config
  235. group = SOCKS_DEFAULT_GROUP;
  236. node.linkType = SPEEDTEST_MESSAGE_FOUNDSOCKS;
  237. node.proxyStr = socksConstruct(group, ps, add, port, "", "", udp, tfo, scv);
  238. break;
  239. default:
  240. continue;
  241. }
  242. node.group = group;
  243. node.remarks = ps;
  244. node.id = index;
  245. node.server = add;
  246. node.port = to_int(port, 1);
  247. nodes.push_back(node);
  248. }
  249. return;
  250. }
  251. void explodeSS(std::string ss, bool libev, const std::string &custom_port, nodeInfo &node)
  252. {
  253. std::string ps, password, method, server, port, plugins, plugin, pluginopts, addition, group = SS_DEFAULT_GROUP, secret;
  254. //std::vector<std::string> args, secret;
  255. ss = replace_all_distinct(ss.substr(5), "/?", "?");
  256. if(strFind(ss, "#"))
  257. {
  258. ps = UrlDecode(ss.substr(ss.find("#") + 1));
  259. ss.erase(ss.find("#"));
  260. }
  261. if(strFind(ss, "?"))
  262. {
  263. addition = ss.substr(ss.find("?") + 1);
  264. plugins = UrlDecode(getUrlArg(addition, "plugin"));
  265. plugin = plugins.substr(0, plugins.find(";"));
  266. pluginopts = plugins.substr(plugins.find(";") + 1);
  267. if(getUrlArg(addition, "group").size())
  268. group = urlsafe_base64_decode(getUrlArg(addition, "group"));
  269. ss.erase(ss.find("?"));
  270. }
  271. if(strFind(ss, "@"))
  272. {
  273. /*
  274. ss = regReplace(ss, "(.*?)@(.*):(.*)", "$1|$2|$3");
  275. args = split(ss, "|");
  276. secret = split(urlsafe_base64_decode(args[0]), ":");
  277. if(args.size() < 3 || secret.size() < 2)
  278. return;
  279. method = secret[0];
  280. password = secret[1];
  281. server = args[1];
  282. port = custom_port.empty() ? args[2] : custom_port;
  283. */
  284. if(regGetMatch(ss, "(.*?)@(.*):(.*)", 4, NULL, &secret, &server, &port))
  285. return;
  286. if(regGetMatch(urlsafe_base64_decode(secret), "(.*?):(.*)", 3, NULL, &method, &password))
  287. return;
  288. }
  289. else
  290. {
  291. /*
  292. if(!regMatch(urlsafe_base64_decode(ss), "(.*?):(.*?)@(.*):(.*)"))
  293. return;
  294. ss = regReplace(urlsafe_base64_decode(ss), "(.*?):(.*?)@(.*):(.*)", "$1|$2|$3|$4");
  295. args = split(ss, "|");
  296. if(args.size() < 4)
  297. return;
  298. method = args[0];
  299. password = args[1];
  300. server = args[2];
  301. port = custom_port.empty() ? args[3] : custom_port;
  302. */
  303. if(regGetMatch(urlsafe_base64_decode(ss), "(.*?):(.*)@(.*):(.*)", 5, NULL, &method, &password, &server, &port))
  304. return;
  305. }
  306. if(custom_port.size())
  307. port = custom_port;
  308. if(port == "0")
  309. return;
  310. if(ps.empty())
  311. ps = server + ":" + port;
  312. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  313. node.group = group;
  314. node.remarks = ps;
  315. node.server = server;
  316. node.port = to_int(port, 1);
  317. node.proxyStr = ssConstruct(group, ps, server, port, password, method, plugin, pluginopts, libev);
  318. }
  319. void explodeSSD(std::string link, bool libev, const std::string &custom_port, std::vector<nodeInfo> &nodes)
  320. {
  321. Document jsondata;
  322. nodeInfo node;
  323. unsigned int index = nodes.size(), listType = 0, listCount = 0;
  324. std::string group, port, method, password, server, remarks;
  325. std::string plugin, pluginopts;
  326. std::map<unsigned int, std::string> node_map;
  327. link = urlsafe_base64_decode(link.substr(6));
  328. jsondata.Parse(link.c_str());
  329. if(jsondata.HasParseError())
  330. return;
  331. if(!jsondata.HasMember("servers"))
  332. return;
  333. GetMember(jsondata, "airport", group);
  334. if(jsondata["servers"].IsArray())
  335. {
  336. listType = 0;
  337. listCount = jsondata["servers"].Size();
  338. }
  339. else if(jsondata["servers"].IsObject())
  340. {
  341. listType = 1;
  342. listCount = jsondata["servers"].MemberCount();
  343. unsigned int node_index = 0;
  344. for(rapidjson::Value::MemberIterator iter = jsondata["servers"].MemberBegin(); iter != jsondata["servers"].MemberEnd(); iter++)
  345. {
  346. node_map.emplace(node_index, iter->name.GetString());
  347. node_index++;
  348. }
  349. }
  350. else
  351. return;
  352. rapidjson::Value singlenode;
  353. for(unsigned int i = 0; i < listCount; i++)
  354. {
  355. //get default info
  356. GetMember(jsondata, "port", port);
  357. GetMember(jsondata, "encryption", method);
  358. GetMember(jsondata, "password", password);
  359. GetMember(jsondata, "plugin", plugin);
  360. GetMember(jsondata, "plugin_options", pluginopts);
  361. //get server-specific info
  362. switch(listType)
  363. {
  364. case 0:
  365. singlenode = jsondata["servers"][i];
  366. break;
  367. case 1:
  368. singlenode = jsondata["servers"].FindMember(node_map[i].data())->value;
  369. break;
  370. default:
  371. continue;
  372. }
  373. singlenode["server"] >> server;
  374. GetMember(singlenode, "remarks", remarks);
  375. GetMember(singlenode, "port", port);
  376. GetMember(singlenode, "encryption", method);
  377. GetMember(singlenode, "password", password);
  378. GetMember(singlenode, "plugin", plugin);
  379. GetMember(singlenode, "plugin_options", pluginopts);
  380. if(custom_port.size())
  381. port = custom_port;
  382. if(port == "0")
  383. continue;
  384. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  385. node.group = group;
  386. node.remarks = remarks;
  387. node.server = server;
  388. node.port = to_int(port, 1);
  389. node.proxyStr = ssConstruct(group, remarks, server, port, password, method, plugin, pluginopts, libev);
  390. node.id = index;
  391. nodes.push_back(node);
  392. index++;
  393. }
  394. return;
  395. }
  396. void explodeSSAndroid(std::string ss, bool libev, const std::string &custom_port, std::vector<nodeInfo> &nodes)
  397. {
  398. std::string ps, password, method, server, port, group = SS_DEFAULT_GROUP;
  399. std::string plugin, pluginopts;
  400. Document json;
  401. nodeInfo node;
  402. int index = nodes.size();
  403. //first add some extra data before parsing
  404. ss = "{\"nodes\":" + ss + "}";
  405. json.Parse(ss.data());
  406. if(json.HasParseError())
  407. return;
  408. for(unsigned int i = 0; i < json["nodes"].Size(); i++)
  409. {
  410. server = GetMember(json["nodes"][i], "server");
  411. if(server.empty())
  412. continue;
  413. ps = GetMember(json["nodes"][i], "remarks");
  414. port = custom_port.size() ? custom_port : GetMember(json["nodes"][i], "server_port");
  415. if(port == "0")
  416. continue;
  417. if(ps.empty())
  418. ps = server + ":" + port;
  419. password = GetMember(json["nodes"][i], "password");
  420. method = GetMember(json["nodes"][i], "method");
  421. plugin = GetMember(json["nodes"][i], "plugin");
  422. pluginopts = GetMember(json["nodes"][i], "plugin_opts");
  423. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  424. node.id = index;
  425. node.group = group;
  426. node.remarks = ps;
  427. node.server = server;
  428. node.port = to_int(port, 1);
  429. node.proxyStr = ssConstruct(group, ps, server, port, password, method, plugin, pluginopts, libev);
  430. nodes.push_back(node);
  431. index++;
  432. }
  433. }
  434. void explodeSSConf(std::string content, const std::string &custom_port, bool libev, std::vector<nodeInfo> &nodes)
  435. {
  436. nodeInfo node;
  437. Document json;
  438. std::string ps, password, method, server, port, plugin, pluginopts, group = SS_DEFAULT_GROUP;
  439. int index = nodes.size();
  440. json.Parse(content.data());
  441. if(json.HasParseError())
  442. return;
  443. const char *section = json.HasMember("version") && json.HasMember("remarks") && json.HasMember("servers") ? "servers" : "configs";
  444. if(!json.HasMember(section))
  445. return;
  446. GetMember(json, "remarks", group);
  447. for(unsigned int i = 0; i < json[section].Size(); i++)
  448. {
  449. ps = GetMember(json[section][i], "remarks");
  450. port = custom_port.size() ? custom_port : GetMember(json[section][i], "server_port");
  451. if(port == "0")
  452. continue;
  453. if(ps.empty())
  454. ps = server + ":" + port;
  455. password = GetMember(json[section][i], "password");
  456. method = GetMember(json[section][i], "method");
  457. server = GetMember(json[section][i], "server");
  458. plugin = GetMember(json[section][i], "plugin");
  459. pluginopts = GetMember(json[section][i], "plugin_opts");
  460. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  461. node.group = group;
  462. node.remarks = ps;
  463. node.id = index;
  464. node.server = server;
  465. node.port = to_int(port, 1);
  466. node.proxyStr = ssConstruct(group, ps, server, port, password, method, plugin, pluginopts, libev);
  467. nodes.push_back(node);
  468. index++;
  469. }
  470. return;
  471. }
  472. void explodeSSR(std::string ssr, bool ss_libev, bool ssr_libev, const std::string &custom_port, nodeInfo &node)
  473. {
  474. std::string strobfs;
  475. std::vector<std::string> strcfg;
  476. std::string remarks, group, server, port, method, password, protocol, protoparam, obfs, obfsparam, remarks_base64;
  477. ssr = replace_all_distinct(ssr.substr(6), "\r", "");
  478. ssr = urlsafe_base64_decode(ssr);
  479. if(strFind(ssr, "/?"))
  480. {
  481. strobfs = ssr.substr(ssr.find("/?") + 2);
  482. ssr = ssr.substr(0, ssr.find("/?"));
  483. group = urlsafe_base64_decode(getUrlArg(strobfs, "group"));
  484. remarks = urlsafe_base64_decode(getUrlArg(strobfs, "remarks"));
  485. remarks_base64 = urlsafe_base64_reverse(getUrlArg(strobfs, "remarks"));
  486. obfsparam = regReplace(urlsafe_base64_decode(getUrlArg(strobfs, "obfsparam")), "\\s", "");
  487. protoparam = regReplace(urlsafe_base64_decode(getUrlArg(strobfs, "protoparam")), "\\s", "");
  488. }
  489. /*
  490. ssr = regReplace(ssr, "(.*):(.*?):(.*?):(.*?):(.*?):(.*)", "$1|$2|$3|$4|$5|$6");
  491. strcfg = split(ssr, "|");
  492. if(strcfg.size() != 6)
  493. return;
  494. server = strcfg[0];
  495. port = custom_port.empty() ? strcfg[1] : custom_port;
  496. protocol = strcfg[2];
  497. method = strcfg[3];
  498. obfs = strcfg[4];
  499. password = urlsafe_base64_decode(strcfg[5]);
  500. */
  501. if(regGetMatch(ssr, "(.*):(.*?):(.*?):(.*?):(.*?):(.*)", 7, NULL, &server, &port, &protocol, &method, &obfs, &password))
  502. return;
  503. password = urlsafe_base64_decode(password);
  504. if(custom_port.size())
  505. port = custom_port;
  506. if(port == "0")
  507. return;
  508. if(group.empty())
  509. group = SSR_DEFAULT_GROUP;
  510. if(remarks.empty())
  511. {
  512. remarks = server + ":" + port;
  513. remarks_base64 = base64_encode(remarks);
  514. }
  515. node.group = group;
  516. node.remarks = remarks;
  517. node.server = server;
  518. node.port = to_int(port, 1);
  519. if(find(ss_ciphers.begin(), ss_ciphers.end(), method) != ss_ciphers.end() && (obfs.empty() || obfs == "plain") && (protocol.empty() || protocol == "origin"))
  520. {
  521. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  522. node.proxyStr = ssConstruct(group, remarks, server, port, password, method, "", "", ss_libev);
  523. }
  524. else
  525. {
  526. node.linkType = SPEEDTEST_MESSAGE_FOUNDSSR;
  527. node.proxyStr = ssrConstruct(group, remarks, remarks_base64, server, port, protocol, method, obfs, password, obfsparam, protoparam, ssr_libev);
  528. }
  529. }
  530. void explodeSSRConf(std::string content, const std::string &custom_port, bool ss_libev, bool ssr_libev, std::vector<nodeInfo> &nodes)
  531. {
  532. nodeInfo node;
  533. Document json;
  534. std::string remarks, remarks_base64, group, server, port, method, password, protocol, protoparam, obfs, obfsparam, plugin, pluginopts;
  535. int index = nodes.size();
  536. json.Parse(content.data());
  537. if(json.HasParseError())
  538. return;
  539. if(json.HasMember("local_port") && json.HasMember("local_address")) //single libev config
  540. {
  541. server = GetMember(json, "server");
  542. port = GetMember(json, "server_port");
  543. node.remarks = server + ":" + port;
  544. node.server = server;
  545. node.port = to_int(port, 1);
  546. method = GetMember(json, "method");
  547. obfs = GetMember(json, "obfs");
  548. protocol = GetMember(json, "protocol");
  549. if(find(ss_ciphers.begin(), ss_ciphers.end(), method) != ss_ciphers.end() && (obfs.empty() || obfs == "plain") && (protocol.empty() || protocol == "origin"))
  550. {
  551. plugin = GetMember(json, "plugin");
  552. pluginopts = GetMember(json, "plugin_opts");
  553. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  554. node.group = SS_DEFAULT_GROUP;
  555. node.proxyStr = ssConstruct(node.group, node.remarks, server, port, password, method, plugin, pluginopts, ss_libev);
  556. }
  557. else
  558. {
  559. protoparam = GetMember(json, "protocol_param");
  560. obfsparam = GetMember(json, "obfs_param");
  561. node.linkType = SPEEDTEST_MESSAGE_FOUNDSSR;
  562. node.group = SSR_DEFAULT_GROUP;
  563. node.proxyStr = ssrConstruct(node.group, node.remarks, base64_encode(node.remarks), server, port, protocol, method, obfs, password, obfsparam, protoparam, ssr_libev);
  564. }
  565. nodes.push_back(node);
  566. return;
  567. }
  568. for(unsigned int i = 0; i < json["configs"].Size(); i++)
  569. {
  570. json["configs"][i]["group"] >> group;
  571. if(group.empty())
  572. group = SSR_DEFAULT_GROUP;
  573. json["configs"][i]["remarks"] >> remarks;
  574. json["configs"][i]["server"] >> server;
  575. port = custom_port.size() ? custom_port : GetMember(json["configs"][i], "server_port");
  576. if(port == "0")
  577. continue;
  578. if(remarks.empty())
  579. remarks = server + ":" + port;
  580. json["configs"][i]["remarks_base64"] >> remarks_base64;
  581. json["configs"][i]["password"] >> password;
  582. json["configs"][i]["method"] >> method;
  583. json["configs"][i]["protocol"] >> protocol;
  584. json["configs"][i]["protocolparam"] >> protoparam;
  585. json["configs"][i]["obfs"] >> obfs;
  586. json["configs"][i]["obfsparam"] >> obfsparam;
  587. node.linkType = SPEEDTEST_MESSAGE_FOUNDSSR;
  588. node.group = group;
  589. node.remarks = remarks;
  590. node.id = index;
  591. node.server = server;
  592. node.port = to_int(port, 1);
  593. node.proxyStr = ssrConstruct(group, remarks, remarks_base64, server, port, protocol, method, obfs, password, obfsparam, protoparam, ssr_libev);
  594. nodes.push_back(node);
  595. index++;
  596. }
  597. return;
  598. }
  599. void explodeSocks(std::string link, const std::string &custom_port, nodeInfo &node)
  600. {
  601. std::string group, remarks, server, port, username, password;
  602. if(strFind(link, "socks://")) //v2rayn socks link
  603. {
  604. std::vector<std::string> arguments;
  605. if(strFind(link, "#"))
  606. {
  607. remarks = UrlDecode(link.substr(link.find("#") + 1));
  608. link.erase(link.find("#"));
  609. }
  610. link = urlsafe_base64_decode(link.substr(8));
  611. arguments = split(link, ":");
  612. if(arguments.size() < 2)
  613. return;
  614. server = arguments[0];
  615. port = arguments[1];
  616. }
  617. else if(strFind(link, "https://t.me/socks") || strFind(link, "tg://socks")) //telegram style socks link
  618. {
  619. server = getUrlArg(link, "server");
  620. port = getUrlArg(link, "port");
  621. username = UrlDecode(getUrlArg(link, "user"));
  622. password = UrlDecode(getUrlArg(link, "pass"));
  623. remarks = UrlDecode(getUrlArg(link, "remarks"));
  624. group = UrlDecode(getUrlArg(link, "group"));
  625. }
  626. if(group.empty())
  627. group = SOCKS_DEFAULT_GROUP;
  628. if(remarks.empty())
  629. remarks = server + ":" + port;
  630. if(custom_port.size())
  631. port = custom_port;
  632. if(port == "0")
  633. return;
  634. node.linkType = SPEEDTEST_MESSAGE_FOUNDSOCKS;
  635. node.group = group;
  636. node.remarks = remarks;
  637. node.server = server;
  638. node.port = to_int(port, 1);
  639. node.proxyStr = socksConstruct(group, remarks, server, port, username, password);
  640. }
  641. void explodeHTTP(std::string link, const std::string &custom_port, nodeInfo &node)
  642. {
  643. std::string group, remarks, server, port, username, password;
  644. server = getUrlArg(link, "server");
  645. port = getUrlArg(link, "port");
  646. username = UrlDecode(getUrlArg(link, "user"));
  647. password = UrlDecode(getUrlArg(link, "pass"));
  648. remarks = UrlDecode(getUrlArg(link, "remarks"));
  649. group = UrlDecode(getUrlArg(link, "group"));
  650. if(group.empty())
  651. group = HTTP_DEFAULT_GROUP;
  652. if(remarks.empty())
  653. remarks = server + ":" + port;
  654. if(custom_port.size())
  655. port = custom_port;
  656. if(port == "0")
  657. return;
  658. node.linkType = SPEEDTEST_MESSAGE_FOUNDHTTP;
  659. node.group = group;
  660. node.remarks = remarks;
  661. node.server = server;
  662. node.port = to_int(port, 1);
  663. node.proxyStr = httpConstruct(group, remarks, server, port, username, password, strFind(link, "/https"));
  664. }
  665. void explodeHTTPSub(std::string link, const std::string &custom_port, nodeInfo &node)
  666. {
  667. std::string group, remarks, server, port, username, password;
  668. std::string addition;
  669. string_array configs;
  670. bool tls = strFind(link, "https://");
  671. string_size pos = link.find("?");
  672. if(pos != link.npos)
  673. {
  674. addition = link.substr(pos + 1);
  675. link.erase(pos);
  676. remarks = UrlDecode(getUrlArg(addition, "remarks"));
  677. group = UrlDecode(getUrlArg(addition, "group"));
  678. }
  679. link.erase(0, link.find("://") + 3);
  680. link = urlsafe_base64_decode(link);
  681. if(strFind(link, "@"))
  682. {
  683. /*
  684. link = regReplace(link, "(.*?):(.*?)@(.*):(.*)", "$1|$2|$3|$4");
  685. configs = split(link, "|");
  686. if(configs.size() != 4)
  687. return;
  688. username = configs[0];
  689. password = configs[1];
  690. server = configs[2];
  691. port = configs[3];
  692. */
  693. if(regGetMatch(link, "(.*?):(.*?)@(.*):(.*)", 5, NULL, &username, &password, &server, &port))
  694. return;
  695. }
  696. else
  697. {
  698. /*
  699. link = regReplace(link, "(.*):(.*)", "$1|$2");
  700. configs = split(link, "|");
  701. if(configs.size() != 2)
  702. return;
  703. server = configs[1];
  704. port = configs[2];
  705. */
  706. if(regGetMatch(link, "(.*):(.*)", 3, NULL, &server, &port))
  707. return;
  708. }
  709. if(group.empty())
  710. group = HTTP_DEFAULT_GROUP;
  711. if(remarks.empty())
  712. remarks = server + ":" + port;
  713. if(custom_port.size())
  714. port = custom_port;
  715. if(port == "0")
  716. return;
  717. node.linkType = SPEEDTEST_MESSAGE_FOUNDHTTP;
  718. node.group = group;
  719. node.remarks = remarks;
  720. node.server = server;
  721. node.port = to_int(port, 1);
  722. node.proxyStr = httpConstruct(group, remarks, server, port, username, password, tls);
  723. }
  724. void explodeTrojan(std::string trojan, const std::string &custom_port, nodeInfo &node)
  725. {
  726. std::string server, port, psk, addition, remark, host;
  727. string_array vArray;
  728. trojan.erase(0, 9);
  729. string_size pos = trojan.rfind("#");
  730. if(pos != trojan.npos)
  731. {
  732. remark = UrlDecode(trojan.substr(pos + 1));
  733. trojan.erase(pos);
  734. }
  735. pos = trojan.rfind("?");
  736. if(pos != trojan.npos)
  737. {
  738. addition = trojan.substr(pos + 1);
  739. trojan.erase(pos);
  740. }
  741. /*
  742. trojan = regReplace(trojan, "(.*?)@(.*):(.*)", "$1|$2|$3");
  743. vArray = split(trojan, "|");
  744. if(vArray.size() != 3)
  745. return;
  746. psk = vArray[0];
  747. server = vArray[1];
  748. port = custom_port.empty() ? vArray[2] : custom_port;
  749. */
  750. if(regGetMatch(trojan, "(.*?)@(.*):(.*)", 4, NULL, &psk, &server, &port))
  751. return;
  752. if(custom_port.size())
  753. port = custom_port;
  754. if(port == "0")
  755. return;
  756. host = getUrlArg(addition, "peer");
  757. if(remark.empty())
  758. remark = server + ":" + port;
  759. if(host.empty() && !isIPv4(server) && !isIPv6(server))
  760. host = server;
  761. node.linkType = SPEEDTEST_MESSAGE_FOUNDTROJAN;
  762. node.group = TROJAN_DEFAULT_GROUP;
  763. node.remarks = remark;
  764. node.server = server;
  765. node.port = to_int(port, 1);
  766. node.proxyStr = trojanConstruct(node.group, remark, server, port, psk, host, true);
  767. }
  768. void explodeQuan(std::string quan, const std::string &custom_port, nodeInfo &node)
  769. {
  770. std::string strTemp, itemName, itemVal;
  771. std::string group = V2RAY_DEFAULT_GROUP, ps, add, port, cipher = "auto", type = "none", id, aid = "0", net = "tcp", path, host, edge, tls;
  772. string_array configs, vArray, headers;
  773. strTemp = regReplace(quan, "(.*?) = (.*)", "$1,$2");
  774. configs = split(strTemp, ",");
  775. if(configs[1] == "vmess")
  776. {
  777. if(configs.size() < 6)
  778. return;
  779. ps = trim(configs[0]);
  780. add = trim(configs[2]);
  781. port = custom_port.size() ? custom_port : trim(configs[3]);
  782. if(port == "0")
  783. return;
  784. cipher = trim(configs[4]);
  785. id = trim(replace_all_distinct(configs[5], "\"", ""));
  786. //read link
  787. for(unsigned int i = 6; i < configs.size(); i++)
  788. {
  789. vArray = split(configs[i], "=");
  790. if(vArray.size() < 2)
  791. continue;
  792. itemName = trim(vArray[0]);
  793. itemVal = trim(vArray[1]);
  794. switch(hash_(itemName))
  795. {
  796. case "group"_hash: group = itemVal; break;
  797. case "over-tls"_hash: tls = itemVal == "true" ? "tls" : ""; break;
  798. case "tls-host"_hash: host = itemVal; break;
  799. case "obfs-path"_hash: path = replace_all_distinct(itemVal, "\"", ""); break;
  800. case "obfs-header"_hash:
  801. headers = split(replace_all_distinct(replace_all_distinct(itemVal, "\"", ""), "[Rr][Nn]", "|"), "|");
  802. for(std::string &x : headers)
  803. {
  804. if(regFind(x, "(?i)Host: "))
  805. host = x.substr(6);
  806. else if(regFind(x, "(?i)Edge: "))
  807. edge = x.substr(6);
  808. }
  809. break;
  810. case "obfs"_hash:
  811. if(itemVal == "ws")
  812. net = "ws";
  813. break;
  814. default: continue;
  815. }
  816. }
  817. if(path.empty())
  818. path = "/";
  819. node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
  820. node.group = group;
  821. node.remarks = ps;
  822. node.server = add;
  823. node.port = to_int(port, 1);
  824. node.proxyStr = vmessConstruct(group, ps, add, port, type, id, aid, net, cipher, path, host, edge, tls);
  825. }
  826. }
  827. void explodeNetch(std::string netch, bool ss_libev, bool ssr_libev, const std::string &custom_port, nodeInfo &node)
  828. {
  829. Document json;
  830. std::string type, group, remark, address, port, username, password, method, plugin, pluginopts, protocol, protoparam, obfs, obfsparam, id, aid, transprot, faketype, host, edge, path, tls;
  831. tribool udp, tfo, scv;
  832. netch = urlsafe_base64_decode(netch.substr(8));
  833. json.Parse(netch.data());
  834. if(json.HasParseError())
  835. return;
  836. type = GetMember(json, "Type");
  837. group = GetMember(json, "Group");
  838. remark = GetMember(json, "Remark");
  839. address = GetMember(json, "Hostname");
  840. udp = GetMember(json, "EnableUDP");
  841. tfo = GetMember(json, "EnableTFO");
  842. scv = GetMember(json, "AllowInsecure");
  843. port = custom_port.size() ? custom_port : GetMember(json, "Port");
  844. if(port == "0")
  845. return;
  846. method = GetMember(json, "EncryptMethod");
  847. password = GetMember(json, "Password");
  848. if(remark.empty())
  849. remark = address + ":" + port;
  850. switch(hash_(type))
  851. {
  852. case "SS"_hash:
  853. plugin = GetMember(json, "Plugin");
  854. pluginopts = GetMember(json, "PluginOption");
  855. if(group.empty())
  856. group = SS_DEFAULT_GROUP;
  857. node.group = group;
  858. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  859. node.proxyStr = ssConstruct(group, remark, address, port, password, method, plugin, pluginopts, ss_libev, udp, tfo, scv);
  860. break;
  861. case "SSR"_hash:
  862. protocol = GetMember(json, "Protocol");
  863. obfs = GetMember(json, "OBFS");
  864. if(find(ss_ciphers.begin(), ss_ciphers.end(), method) != ss_ciphers.end() && (obfs.empty() || obfs == "plain") && (protocol.empty() || protocol == "origin"))
  865. {
  866. plugin = GetMember(json, "Plugin");
  867. pluginopts = GetMember(json, "PluginOption");
  868. if(group.empty())
  869. group = SS_DEFAULT_GROUP;
  870. node.group = group;
  871. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  872. node.proxyStr = ssConstruct(group, remark, address, port, password, method, plugin, pluginopts, ss_libev, udp, tfo, scv);
  873. }
  874. else
  875. {
  876. protoparam = GetMember(json, "ProtocolParam");
  877. obfsparam = GetMember(json, "OBFSParam");
  878. if(group.empty())
  879. group = SSR_DEFAULT_GROUP;
  880. node.group = group;
  881. node.linkType = SPEEDTEST_MESSAGE_FOUNDSSR;
  882. node.proxyStr = ssrConstruct(group, remark, base64_encode(remark), address, port, protocol, method, obfs, password, obfsparam, protoparam, ssr_libev, udp, tfo, scv);
  883. }
  884. break;
  885. case "VMess"_hash:
  886. id = GetMember(json, "UserID");
  887. aid = GetMember(json, "AlterID");
  888. transprot = GetMember(json, "TransferProtocol");
  889. faketype = GetMember(json, "FakeType");
  890. host = GetMember(json, "Host");
  891. path = GetMember(json, "Path");
  892. edge = GetMember(json, "Edge");
  893. tls = GetMember(json, "TLSSecure");
  894. node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
  895. if(group.empty())
  896. group = V2RAY_DEFAULT_GROUP;
  897. node.group = group;
  898. node.proxyStr = vmessConstruct(group, remark, address, port, faketype, id, aid, transprot, method, path, host, edge, tls, udp, tfo, scv);
  899. break;
  900. case "Socks5"_hash:
  901. username = GetMember(json, "Username");
  902. node.linkType = SPEEDTEST_MESSAGE_FOUNDSOCKS;
  903. if(group.empty())
  904. group = SOCKS_DEFAULT_GROUP;
  905. node.group = group;
  906. node.proxyStr = socksConstruct(group, remark, address, port, username, password, udp, tfo, scv);
  907. break;
  908. case "HTTP"_hash:
  909. case "HTTPS"_hash:
  910. node.linkType = SPEEDTEST_MESSAGE_FOUNDHTTP;
  911. if(group.empty())
  912. group = HTTP_DEFAULT_GROUP;
  913. node.group = group;
  914. node.proxyStr = httpConstruct(group, remark, address, port, username, password, type == "HTTPS", scv);
  915. break;
  916. case "Trojan"_hash:
  917. host = GetMember(json, "Host");
  918. tls = GetMember(json, "TLSSecure");
  919. node.linkType = SPEEDTEST_MESSAGE_FOUNDTROJAN;
  920. if(group.empty())
  921. group = TROJAN_DEFAULT_GROUP;
  922. node.group = group;
  923. node.proxyStr = trojanConstruct(group, remark, address, port, username, password, tls == "true", udp, tfo, scv);
  924. break;
  925. case "Snell"_hash:
  926. obfs = GetMember(json, "OBFS");
  927. host = GetMember(json, "Host");
  928. node.linkType = SPEEDTEST_MESSAGE_FOUNDSNELL;
  929. if(group.empty())
  930. group = SNELL_DEFAULT_GROUP;
  931. node.group = group;
  932. node.proxyStr = snellConstruct(group, remark, address, port, password, obfs, host, udp, tfo, scv);
  933. break;
  934. default:
  935. return;
  936. }
  937. node.remarks = remark;
  938. node.server = address;
  939. node.port = (unsigned short)to_int(port, 1);
  940. }
  941. void explodeClash(Node yamlnode, const std::string &custom_port, std::vector<nodeInfo> &nodes, bool ss_libev, bool ssr_libev)
  942. {
  943. std::string proxytype, ps, server, port, cipher, group, password; //common
  944. std::string type = "none", id, aid = "0", net = "tcp", path, host, edge, tls; //vmess
  945. std::string plugin, pluginopts, pluginopts_mode, pluginopts_host, pluginopts_mux; //ss
  946. std::string protocol, protoparam, obfs, obfsparam; //ssr
  947. std::string user; //socks
  948. tribool udp, tfo, scv;
  949. nodeInfo node;
  950. Node singleproxy;
  951. unsigned int index = nodes.size();
  952. const std::string section = yamlnode["proxies"].IsDefined() ? "proxies" : "Proxy";
  953. for(unsigned int i = 0; i < yamlnode[section].size(); i++)
  954. {
  955. singleproxy = yamlnode[section][i];
  956. singleproxy["type"] >>= proxytype;
  957. singleproxy["name"] >>= ps;
  958. singleproxy["server"] >>= server;
  959. port = custom_port.empty() ? safe_as<std::string>(singleproxy["port"]) : custom_port;
  960. if(port.empty() || port == "0")
  961. continue;
  962. udp = safe_as<std::string>(singleproxy["udp"]);
  963. scv = safe_as<std::string>(singleproxy["skip-cert-verify"]);
  964. switch(hash_(proxytype))
  965. {
  966. case "vmess"_hash:
  967. group = V2RAY_DEFAULT_GROUP;
  968. singleproxy["uuid"] >>= id;
  969. singleproxy["alterId"] >>= aid;
  970. singleproxy["cipher"] >>= cipher;
  971. net = singleproxy["network"].IsDefined() ? safe_as<std::string>(singleproxy["network"]) : "tcp";
  972. path = singleproxy["ws-path"].IsDefined() ? safe_as<std::string>(singleproxy["ws-path"]) : "/";
  973. tls = safe_as<std::string>(singleproxy["tls"]) == "true" ? "tls" : "";
  974. singleproxy["ws-headers"]["Host"] >>= host;
  975. singleproxy["ws-headers"]["Edge"] >>= edge;
  976. node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
  977. node.proxyStr = vmessConstruct(group, ps, server, port, type, id, aid, net, cipher, path, host, edge, tls, udp, tfo, scv);
  978. break;
  979. case "ss"_hash:
  980. group = SS_DEFAULT_GROUP;
  981. singleproxy["cipher"] >>= cipher;
  982. singleproxy["password"] >>= password;
  983. if(singleproxy["plugin"].IsDefined())
  984. {
  985. switch(hash_(safe_as<std::string>(singleproxy["plugin"])))
  986. {
  987. case "obfs"_hash:
  988. plugin = "simple-obfs";
  989. if(singleproxy["plugin-opts"].IsDefined())
  990. {
  991. singleproxy["plugin-opts"]["mode"] >>= pluginopts_mode;
  992. singleproxy["plugin-opts"]["host"] >>= pluginopts_host;
  993. }
  994. break;
  995. case "v2ray-plugin"_hash:
  996. plugin = "v2ray-plugin";
  997. if(singleproxy["plugin-opts"].IsDefined())
  998. {
  999. singleproxy["plugin-opts"]["mode"] >>= pluginopts_mode;
  1000. singleproxy["plugin-opts"]["host"] >>= pluginopts_host;
  1001. tls = safe_as<bool>(singleproxy["plugin-opts"]["tls"]) ? "tls;" : "";
  1002. singleproxy["plugin-opts"]["path"] >>= path;
  1003. pluginopts_mux = safe_as<bool>(singleproxy["plugin-opts"]["mux"]) ? "mux=4;" : "";
  1004. }
  1005. break;
  1006. default: break;
  1007. }
  1008. }
  1009. else if(singleproxy["obfs"].IsDefined())
  1010. {
  1011. plugin = "simple-obfs";
  1012. singleproxy["obfs"] >>= pluginopts_mode;
  1013. singleproxy["obfs-host"] >>= pluginopts_host;
  1014. }
  1015. else
  1016. plugin.clear();
  1017. switch(hash_(plugin))
  1018. {
  1019. case "simple-obfs"_hash:
  1020. case "obfs-local"_hash:
  1021. pluginopts = "obfs=" + pluginopts_mode;
  1022. pluginopts += pluginopts_host.empty() ? "" : ";obfs-host=" + pluginopts_host;
  1023. break;
  1024. case "v2ray-plugin"_hash:
  1025. pluginopts = "mode=" + pluginopts_mode + ";" + tls + pluginopts_mux;
  1026. if(pluginopts_host.size())
  1027. pluginopts += "host=" + pluginopts_host + ";";
  1028. if(path.size())
  1029. pluginopts += "path=" + path + ";";
  1030. if(pluginopts_mux.size())
  1031. pluginopts += "mux=" + pluginopts_mux + ";";
  1032. break;
  1033. }
  1034. //support for go-shadowsocks2
  1035. if(cipher == "AEAD_CHACHA20_POLY1305")
  1036. cipher = "chacha20-ietf-poly1305";
  1037. else if(strFind(cipher, "AEAD"))
  1038. {
  1039. cipher = replace_all_distinct(replace_all_distinct(cipher, "AEAD_", ""), "_", "-");
  1040. std::transform(cipher.begin(), cipher.end(), cipher.begin(), ::tolower);
  1041. }
  1042. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  1043. node.proxyStr = ssConstruct(group, ps, server, port, password, cipher, plugin, pluginopts, ss_libev, udp, tfo, scv);
  1044. break;
  1045. case "socks"_hash:
  1046. group = SOCKS_DEFAULT_GROUP;
  1047. singleproxy["username"] >>= user;
  1048. singleproxy["password"] >>= password;
  1049. node.linkType = SPEEDTEST_MESSAGE_FOUNDSOCKS;
  1050. node.proxyStr = socksConstruct(group, ps, server, port, user, password);
  1051. break;
  1052. case "ssr"_hash:
  1053. group = SSR_DEFAULT_GROUP;
  1054. singleproxy["cipher"] >>= cipher;
  1055. singleproxy["password"] >>= password;
  1056. singleproxy["protocol"] >>= protocol;
  1057. singleproxy["protocolparam"] >>= protoparam;
  1058. singleproxy["obfs"] >>= obfs;
  1059. singleproxy["obfsparam"] >>= obfsparam;
  1060. node.linkType = SPEEDTEST_MESSAGE_FOUNDSSR;
  1061. node.proxyStr = ssrConstruct(group, ps, base64_encode(ps), server, port, protocol, cipher, obfs, password, obfsparam, protoparam, ssr_libev, udp, tfo, scv);
  1062. break;
  1063. case "http"_hash:
  1064. group = HTTP_DEFAULT_GROUP;
  1065. singleproxy["username"] >>= user;
  1066. singleproxy["password"] >>= password;
  1067. singleproxy["tls"] >>= tls;
  1068. node.linkType = SPEEDTEST_MESSAGE_FOUNDHTTP;
  1069. node.proxyStr = httpConstruct(group, ps, server, port, user, password, tls == "true", scv);
  1070. break;
  1071. case "trojan"_hash:
  1072. group = TROJAN_DEFAULT_GROUP;
  1073. singleproxy["password"] >>= password;
  1074. singleproxy["sni"] >>= host;
  1075. node.linkType = SPEEDTEST_MESSAGE_FOUNDTROJAN;
  1076. node.proxyStr = trojanConstruct(group, ps, server, port, password, host, true, udp, tfo, scv);
  1077. break;
  1078. case "snell"_hash:
  1079. group = SNELL_DEFAULT_GROUP;
  1080. singleproxy["psk"] >> password;
  1081. singleproxy["obfs-opts"]["mode"] >>= obfs;
  1082. singleproxy["obfs-opts"]["host"] >>= host;
  1083. node.linkType = SPEEDTEST_MESSAGE_FOUNDSNELL;
  1084. node.proxyStr = snellConstruct(group, ps, server, port, password, obfs, host, udp, tfo, scv);
  1085. break;
  1086. default:
  1087. continue;
  1088. }
  1089. node.group = group;
  1090. node.remarks = ps;
  1091. node.server = server;
  1092. node.port = to_int(port, 1);
  1093. node.id = index;
  1094. nodes.push_back(node);
  1095. index++;
  1096. }
  1097. return;
  1098. }
  1099. void explodeShadowrocket(std::string rocket, const std::string &custom_port, nodeInfo &node)
  1100. {
  1101. std::string add, port, type, id, aid, net = "tcp", path, host, tls, cipher, remarks;
  1102. std::string obfs; //for other style of link
  1103. std::string addition;
  1104. string_array userinfo;
  1105. rocket = rocket.substr(8);
  1106. addition = rocket.substr(rocket.find("?") + 1);
  1107. rocket = rocket.substr(0, rocket.find("?"));
  1108. /*
  1109. userinfo = split(regReplace(urlsafe_base64_decode(rocket), "(.*?):(.*?)@(.*):(.*)", "$1,$2,$3,$4"), ",");
  1110. if(userinfo.size() != 4) // broken link
  1111. return;
  1112. cipher = userinfo[0];
  1113. id = userinfo[1];
  1114. add = userinfo[2];
  1115. port = custom_port.size() ? custom_port : userinfo[3];
  1116. */
  1117. if(regGetMatch(urlsafe_base64_decode(rocket), "(.*?):(.*)@(.*):(.*)", 5, NULL, &cipher, &id, &add, &port))
  1118. return;
  1119. if(custom_port.size())
  1120. port = custom_port;
  1121. if(port == "0")
  1122. return;
  1123. remarks = UrlDecode(getUrlArg(addition, "remark"));
  1124. obfs = getUrlArg(addition, "obfs");
  1125. if(obfs.size())
  1126. {
  1127. if(obfs == "websocket")
  1128. {
  1129. net = "ws";
  1130. host = getUrlArg(addition, "obfsParam");
  1131. path = getUrlArg(addition, "path");
  1132. }
  1133. }
  1134. else
  1135. {
  1136. net = getUrlArg(addition, "network");
  1137. host = getUrlArg(addition, "wsHost");
  1138. path = getUrlArg(addition, "wspath");
  1139. }
  1140. tls = getUrlArg(addition, "tls") == "1" ? "tls" : "";
  1141. aid = getUrlArg(addition, "aid");
  1142. if(aid.empty())
  1143. aid = "0";
  1144. if(remarks.empty())
  1145. remarks = add + ":" + port;
  1146. node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
  1147. node.group = V2RAY_DEFAULT_GROUP;
  1148. node.remarks = remarks;
  1149. node.server = add;
  1150. node.port = to_int(port, 0);
  1151. node.proxyStr = vmessConstruct(node.group, remarks, add, port, type, id, aid, net, cipher, path, host, "", tls);
  1152. }
  1153. void explodeKitsunebi(std::string kit, const std::string &custom_port, nodeInfo &node)
  1154. {
  1155. std::string add, port, type, id, aid = "0", net = "tcp", path, host, tls, cipher = "auto", remarks;
  1156. std::string addition;
  1157. string_size pos;
  1158. string_array userinfo;
  1159. kit = kit.substr(9);
  1160. pos = kit.find("#");
  1161. if(pos != kit.npos)
  1162. {
  1163. remarks = kit.substr(pos + 1);
  1164. kit = kit.substr(0, pos);
  1165. }
  1166. pos = kit.find("?");
  1167. addition = kit.substr(pos + 1);
  1168. kit = kit.substr(0, pos);
  1169. /*
  1170. userinfo = split(regReplace(kit, "(.*?)@(.*):(.*)", "$1,$2,$3"), ",");
  1171. if(userinfo.size() != 3)
  1172. return;
  1173. id = userinfo[0];
  1174. add = userinfo[1];
  1175. pos = userinfo[2].find("/");
  1176. */
  1177. if(regGetMatch(kit, "(.*?)@(.*):(.*)", 4, NULL, &id, &add, &port))
  1178. return;
  1179. pos = port.find("/");
  1180. if(pos != port.npos)
  1181. {
  1182. path = port.substr(pos);
  1183. port.erase(pos);
  1184. }
  1185. if(custom_port.size())
  1186. port = custom_port;
  1187. if(port == "0")
  1188. return;
  1189. net = getUrlArg(addition, "network");
  1190. tls = getUrlArg(addition, "tls") == "true" ? "tls" : "";
  1191. host = getUrlArg(addition, "ws.host");
  1192. if(remarks.empty())
  1193. remarks = add + ":" + port;
  1194. node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
  1195. node.group = V2RAY_DEFAULT_GROUP;
  1196. node.remarks = remarks;
  1197. node.server = add;
  1198. node.port = to_int(port, 0);
  1199. node.proxyStr = vmessConstruct(node.group, remarks, add, port, type, id, aid, net, cipher, path, host, "", tls);
  1200. }
  1201. bool explodeSurge(std::string surge, const std::string &custom_port, std::vector<nodeInfo> &nodes, bool libev)
  1202. {
  1203. std::multimap<std::string, std::string> proxies;
  1204. nodeInfo node;
  1205. unsigned int i, index = nodes.size();
  1206. INIReader ini;
  1207. /*
  1208. if(!strFind(surge, "[Proxy]"))
  1209. return false;
  1210. */
  1211. ini.store_isolated_line = true;
  1212. ini.keep_empty_section = false;
  1213. ini.allow_dup_section_titles = true;
  1214. ini.SetIsolatedItemsSection("Proxy");
  1215. ini.IncludeSection("Proxy");
  1216. ini.AddDirectSaveSection("Proxy");
  1217. if(surge.find("[Proxy]") != surge.npos)
  1218. surge = regReplace(surge, "^[\\S\\s]*?\\[", "[", false);
  1219. ini.Parse(surge);
  1220. if(!ini.SectionExist("Proxy"))
  1221. return false;
  1222. ini.EnterSection("Proxy");
  1223. ini.GetItems(proxies);
  1224. const std::string proxystr = "(.*?)\\s*=\\s*(.*)";
  1225. for(auto &x : proxies)
  1226. {
  1227. std::string remarks, server, port, method, username, password; //common
  1228. std::string plugin, pluginopts, pluginopts_mode, pluginopts_host = "cloudfront.net", mod_url, mod_md5; //ss
  1229. std::string id, net, tls, host, edge, path; //v2
  1230. std::string protocol, protoparam; //ssr
  1231. std::string itemName, itemVal, config;
  1232. std::vector<std::string> configs, vArray, headers, header;
  1233. tribool udp, tfo, scv;
  1234. /*
  1235. remarks = regReplace(x.second, proxystr, "$1");
  1236. configs = split(regReplace(x.second, proxystr, "$2"), ",");
  1237. */
  1238. regGetMatch(x.second, proxystr, 3, NULL, &remarks, &config);
  1239. configs = split(config, ",");
  1240. if(configs.size() < 2)
  1241. continue;
  1242. switch(hash_(configs[0]))
  1243. {
  1244. case "direct"_hash:
  1245. case "reject"_hash:
  1246. case "reject-tinygif"_hash:
  1247. continue;
  1248. case "custom"_hash: //surge 2 style custom proxy
  1249. //remove module detection to speed up parsing and compatible with broken module
  1250. /*
  1251. mod_url = trim(configs[5]);
  1252. if(parsedMD5.count(mod_url) > 0)
  1253. {
  1254. mod_md5 = parsedMD5[mod_url]; //read calculated MD5 from map
  1255. }
  1256. else
  1257. {
  1258. mod_md5 = getMD5(webGet(mod_url)); //retrieve module and calculate MD5
  1259. parsedMD5.insert(std::pair<std::string, std::string>(mod_url, mod_md5)); //save unrecognized module MD5 to map
  1260. }
  1261. */
  1262. //if(mod_md5 == modSSMD5) //is SSEncrypt module
  1263. {
  1264. server = trim(configs[1]);
  1265. port = custom_port.empty() ? trim(configs[2]) : custom_port;
  1266. if(port == "0")
  1267. continue;
  1268. method = trim(configs[3]);
  1269. password = trim(configs[4]);
  1270. for(i = 6; i < configs.size(); i++)
  1271. {
  1272. vArray = split(configs[i], "=");
  1273. if(vArray.size() < 2)
  1274. continue;
  1275. itemName = trim(vArray[0]);
  1276. itemVal = trim(vArray[1]);
  1277. switch(hash_(itemName))
  1278. {
  1279. case "obfs"_hash:
  1280. plugin = "simple-obfs";
  1281. pluginopts_mode = itemVal;
  1282. break;
  1283. case "obfs-host"_hash: pluginopts_host = itemVal; break;
  1284. case "udp-relay"_hash: udp = itemVal; break;
  1285. case "tfo"_hash: tfo = itemVal; break;
  1286. default: continue;
  1287. }
  1288. }
  1289. if(plugin.size())
  1290. {
  1291. pluginopts = "obfs=" + pluginopts_mode;
  1292. pluginopts += pluginopts_host.empty() ? "" : ";obfs-host=" + pluginopts_host;
  1293. }
  1294. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  1295. node.group = SS_DEFAULT_GROUP;
  1296. node.proxyStr = ssConstruct(node.group, remarks, server, port, password, method, plugin, pluginopts, libev, udp, tfo, scv);
  1297. }
  1298. //else
  1299. // continue;
  1300. break;
  1301. case "ss"_hash: //surge 3 style ss proxy
  1302. server = trim(configs[1]);
  1303. port = custom_port.empty() ? trim(configs[2]) : custom_port;
  1304. if(port == "0")
  1305. continue;
  1306. for(i = 3; i < configs.size(); i++)
  1307. {
  1308. vArray = split(configs[i], "=");
  1309. if(vArray.size() < 2)
  1310. continue;
  1311. itemName = trim(vArray[0]);
  1312. itemVal = trim(vArray[1]);
  1313. switch(hash_(itemName))
  1314. {
  1315. case "encrypt-method"_hash: method = itemVal; break;
  1316. case "password"_hash: password = itemVal; break;
  1317. case "obfs"_hash:
  1318. plugin = "simple-obfs";
  1319. pluginopts_mode = itemVal;
  1320. break;
  1321. case "obfs-host"_hash: pluginopts_host = itemVal; break;
  1322. case "udp-relay"_hash: udp = itemVal; break;
  1323. case "tfo"_hash: tfo = itemVal; break;
  1324. default: continue;
  1325. }
  1326. }
  1327. if(plugin.size())
  1328. {
  1329. pluginopts = "obfs=" + pluginopts_mode;
  1330. pluginopts += pluginopts_host.empty() ? "" : ";obfs-host=" + pluginopts_host;
  1331. }
  1332. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  1333. node.group = SS_DEFAULT_GROUP;
  1334. node.proxyStr = ssConstruct(node.group, remarks, server, port, password, method, plugin, pluginopts, libev, udp, tfo, scv);
  1335. break;
  1336. case "socks5"_hash: //surge 3 style socks5 proxy
  1337. node.linkType = SPEEDTEST_MESSAGE_FOUNDSOCKS;
  1338. node.group = SOCKS_DEFAULT_GROUP;
  1339. server = trim(configs[1]);
  1340. port = custom_port.empty() ? trim(configs[2]) : custom_port;
  1341. if(port == "0")
  1342. continue;
  1343. if(configs.size() >= 5)
  1344. {
  1345. username = trim(configs[3]);
  1346. password = trim(configs[4]);
  1347. }
  1348. for(i = 5; i < configs.size(); i++)
  1349. {
  1350. vArray = split(configs[i], "=");
  1351. if(vArray.size() < 2)
  1352. continue;
  1353. itemName = trim(vArray[0]);
  1354. itemVal = trim(vArray[1]);
  1355. switch(hash_(itemName))
  1356. {
  1357. case "udp-relay"_hash: udp = itemVal; break;
  1358. case "tfo"_hash: tfo = itemVal; break;
  1359. case "skip-cert-verify"_hash: scv = itemVal; break;
  1360. default: continue;
  1361. }
  1362. }
  1363. node.proxyStr = socksConstruct(node.group, remarks, server, port, username, password, udp, tfo, scv);
  1364. break;
  1365. case "vmess"_hash: //surge 4 style vmess proxy
  1366. server = trim(configs[1]);
  1367. port = custom_port.empty() ? trim(configs[2]) : custom_port;
  1368. if(port == "0")
  1369. continue;
  1370. net = "tcp";
  1371. method = "auto";
  1372. for(i = 3; i < configs.size(); i++)
  1373. {
  1374. vArray = split(configs[i], "=");
  1375. if(vArray.size() != 2)
  1376. continue;
  1377. itemName = trim(vArray[0]);
  1378. itemVal = trim(vArray[1]);
  1379. switch(hash_(itemName))
  1380. {
  1381. case "username"_hash: id = itemVal; break;
  1382. case "ws"_hash: net = itemVal == "true" ? "ws" : "tcp"; break;
  1383. case "tls"_hash: tls = itemVal == "true" ? "tls" : ""; break;
  1384. case "ws-path"_hash: path = itemVal; break;
  1385. case "obfs-host"_hash: host = itemVal; break;
  1386. case "ws-headers"_hash:
  1387. headers = split(itemVal, "|");
  1388. for(auto &y : headers)
  1389. {
  1390. header = split(trim(y), ":");
  1391. if(header.size() != 2)
  1392. continue;
  1393. else if(regMatch(header[0], "(?i)host"))
  1394. host = trim_quote(header[1]);
  1395. else if(regMatch(header[0], "(?i)edge"))
  1396. edge = trim_quote(header[1]);
  1397. }
  1398. break;
  1399. case "udp-relay"_hash: udp = itemVal; break;
  1400. case "tfo"_hash: tfo = itemVal; break;
  1401. case "skip-cert-verify"_hash: scv = itemVal; break;
  1402. default: continue;
  1403. }
  1404. }
  1405. if(host.empty() && !isIPv4(server) && !isIPv6(server))
  1406. host = server;
  1407. node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
  1408. node.group = V2RAY_DEFAULT_GROUP;
  1409. node.proxyStr = vmessConstruct(node.group, remarks, server, port, "", id, "0", net, method, path, host, edge, tls, udp, tfo, scv);
  1410. break;
  1411. case "http"_hash: //http proxy
  1412. node.linkType = SPEEDTEST_MESSAGE_FOUNDHTTP;
  1413. node.group = HTTP_DEFAULT_GROUP;
  1414. server = trim(configs[1]);
  1415. port = custom_port.empty() ? trim(configs[2]) : custom_port;
  1416. if(port == "0")
  1417. continue;
  1418. for(i = 3; i < configs.size(); i++)
  1419. {
  1420. vArray = split(configs[i], "=");
  1421. if(vArray.size() < 2)
  1422. continue;
  1423. itemName = trim(vArray[0]);
  1424. itemVal = trim(vArray[1]);
  1425. switch(hash_(itemName))
  1426. {
  1427. case "username"_hash: username = itemVal; break;
  1428. case "password"_hash: password = itemVal; break;
  1429. case "skip-cert-verify"_hash: scv = itemVal; break;
  1430. default: continue;
  1431. }
  1432. }
  1433. node.proxyStr = httpConstruct(node.group, remarks, server, port, username, password, false, scv);
  1434. break;
  1435. case "trojan"_hash: // surge 4 style trojan proxy
  1436. node.linkType = SPEEDTEST_MESSAGE_FOUNDTROJAN;
  1437. node.group = TROJAN_DEFAULT_GROUP;
  1438. server = trim(configs[1]);
  1439. port = custom_port.empty() ? trim(configs[2]) : custom_port;
  1440. if(port == "0")
  1441. continue;
  1442. for(i = 3; i < configs.size(); i++)
  1443. {
  1444. vArray = split(configs[i], "=");
  1445. if(vArray.size() != 2)
  1446. continue;
  1447. itemName = trim(vArray[0]);
  1448. itemVal = trim(vArray[1]);
  1449. switch(hash_(itemName))
  1450. {
  1451. case "password"_hash: password = itemVal; break;
  1452. case "sni"_hash: host = itemVal; break;
  1453. case "udp-relay"_hash: udp = itemVal; break;
  1454. case "tfo"_hash: tfo = itemVal; break;
  1455. case "skip-cert-verify"_hash: scv = itemVal; break;
  1456. default: continue;
  1457. }
  1458. }
  1459. if(host.empty() && !isIPv4(server) && !isIPv6(server))
  1460. host = server;
  1461. node.proxyStr = trojanConstruct(node.group, remarks, server, port, password, host, true, udp, tfo, scv);
  1462. break;
  1463. case "snell"_hash:
  1464. node.linkType = SPEEDTEST_MESSAGE_FOUNDSNELL;
  1465. node.group = SNELL_DEFAULT_GROUP;
  1466. server = trim(configs[1]);
  1467. port = custom_port.empty() ? trim(configs[2]) : custom_port;
  1468. if(port == "0")
  1469. continue;
  1470. for(i = 3; i < configs.size(); i++)
  1471. {
  1472. vArray = split(configs[i], "=");
  1473. if(vArray.size() != 2)
  1474. continue;
  1475. itemName = trim(vArray[0]);
  1476. itemVal = trim(vArray[1]);
  1477. switch(hash_(itemName))
  1478. {
  1479. case "psk"_hash: password = itemVal; break;
  1480. case "obfs"_hash: plugin = itemVal; break;
  1481. case "obfs-host"_hash: host = itemVal; break;
  1482. case "udp-relay"_hash: udp = itemVal; break;
  1483. case "tfo"_hash: tfo = itemVal; break;
  1484. case "skip-cert-verify"_hash: scv = itemVal; break;
  1485. default: continue;
  1486. }
  1487. }
  1488. if(host.empty() && !isIPv4(server) && !isIPv6(server))
  1489. host = server;
  1490. node.proxyStr = snellConstruct(node.group, remarks, server, port, password, plugin, host, udp, tfo, scv);
  1491. break;
  1492. default:
  1493. switch(hash_(remarks))
  1494. {
  1495. case "shadowsocks"_hash: //quantumult x style ss/ssr link
  1496. server = trim(configs[0].substr(0, configs[0].rfind(":")));
  1497. port = custom_port.empty() ? trim(configs[0].substr(configs[0].rfind(":") + 1)) : custom_port;
  1498. if(port == "0")
  1499. continue;
  1500. for(i = 1; i < configs.size(); i++)
  1501. {
  1502. vArray = split(trim(configs[i]), "=");
  1503. if(vArray.size() != 2)
  1504. continue;
  1505. itemName = trim(vArray[0]);
  1506. itemVal = trim(vArray[1]);
  1507. switch(hash_(itemName))
  1508. {
  1509. case "method"_hash: method = itemVal; break;
  1510. case "password"_hash: password = itemVal; break;
  1511. case "tag"_hash: remarks = itemVal; break;
  1512. case "ssr-protocol"_hash: protocol = itemVal; break;
  1513. case "ssr-protocol-param"_hash: protoparam = itemVal; break;
  1514. case "obfs"_hash:
  1515. plugin = "simple-obfs";
  1516. pluginopts_mode = itemVal;
  1517. break;
  1518. case "obfs-host"_hash: pluginopts_host = itemVal; break;
  1519. case "udp-relay"_hash: udp = itemVal; break;
  1520. case "fast-open"_hash: tfo = itemVal; break;
  1521. default: continue;
  1522. }
  1523. }
  1524. if(remarks.empty())
  1525. remarks = server + ":" + port;
  1526. if(plugin.size())
  1527. {
  1528. pluginopts = "obfs=" + pluginopts_mode;
  1529. if(pluginopts_host.size())
  1530. pluginopts += ";obfs-host=" + pluginopts_host;
  1531. }
  1532. if(protocol.size())
  1533. {
  1534. node.linkType = SPEEDTEST_MESSAGE_FOUNDSSR;
  1535. node.group = SSR_DEFAULT_GROUP;
  1536. node.proxyStr = ssrConstruct(node.group, remarks, base64_encode(remarks), server, port, protocol, method, pluginopts_mode, password, pluginopts_host, protoparam, libev, udp, tfo, scv);
  1537. }
  1538. else
  1539. {
  1540. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  1541. node.group = SS_DEFAULT_GROUP;
  1542. node.proxyStr = ssConstruct(node.group, remarks, server, port, password, method, plugin, pluginopts, libev, udp, tfo, scv);
  1543. }
  1544. break;
  1545. case "vmess"_hash: //quantumult x style vmess link
  1546. server = trim(configs[0].substr(0, configs[0].rfind(":")));
  1547. port = custom_port.empty() ? trim(configs[0].substr(configs[0].rfind(":") + 1)) : custom_port;
  1548. if(port == "0")
  1549. continue;
  1550. net = "tcp";
  1551. for(i = 1; i < configs.size(); i++)
  1552. {
  1553. vArray = split(trim(configs[i]), "=");
  1554. if(vArray.size() != 2)
  1555. continue;
  1556. itemName = trim(vArray[0]);
  1557. itemVal = trim(vArray[1]);
  1558. switch(hash_(itemName))
  1559. {
  1560. case "method"_hash: method = itemVal; break;
  1561. case "password"_hash: id = itemVal; break;
  1562. case "tag"_hash: remarks = itemVal; break;
  1563. case "obfs"_hash:
  1564. switch(hash_(itemVal))
  1565. {
  1566. case "ws"_hash: net = "ws"; break;
  1567. case "over-tls"_hash: tls = "tls"; break;
  1568. }
  1569. break;
  1570. case "obfs-host"_hash: host = itemVal; break;
  1571. case "obfs-uri"_hash: path = itemVal; break;
  1572. case "over-tls"_hash: tls = itemVal == "true" ? "tls" : ""; break;
  1573. case "udp-relay"_hash: udp = itemVal; break;
  1574. case "fast-open"_hash: tfo = itemVal; break;
  1575. default: continue;
  1576. }
  1577. }
  1578. if(remarks.empty())
  1579. remarks = server + ":" + port;
  1580. if(host.empty() && !isIPv4(server) && !isIPv6(server))
  1581. host = server;
  1582. node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
  1583. node.group = V2RAY_DEFAULT_GROUP;
  1584. node.proxyStr = vmessConstruct(node.group, remarks, server, port, "", id, "0", net, method, path, host, "", tls, udp, tfo, scv);
  1585. break;
  1586. case "trojan"_hash: //quantumult x style trojan link
  1587. server = trim(configs[0].substr(0, configs[0].rfind(":")));
  1588. port = custom_port.empty() ? trim(configs[0].substr(configs[0].rfind(":") + 1)) : custom_port;
  1589. if(port == "0")
  1590. continue;
  1591. for(i = 1; i < configs.size(); i++)
  1592. {
  1593. vArray = split(trim(configs[i]), "=");
  1594. if(vArray.size() != 2)
  1595. continue;
  1596. itemName = trim(vArray[0]);
  1597. itemVal = trim(vArray[1]);
  1598. switch(hash_(itemName))
  1599. {
  1600. case "password"_hash: password = itemVal; break;
  1601. case "tag"_hash: remarks = itemVal; break;
  1602. case "over-tls"_hash: tls = itemVal; break;
  1603. case "tls-host"_hash: host = itemVal; break;
  1604. case "udp-relay"_hash: udp = itemVal; break;
  1605. case "fast-open"_hash: tfo = itemVal; break;
  1606. case "tls-verification"_hash: scv = itemVal == "false"; break;
  1607. default: continue;
  1608. }
  1609. }
  1610. if(remarks.empty())
  1611. remarks = server + ":" + port;
  1612. if(host.empty() && !isIPv4(server) && !isIPv6(server))
  1613. host = server;
  1614. node.linkType = SPEEDTEST_MESSAGE_FOUNDTROJAN;
  1615. node.group = TROJAN_DEFAULT_GROUP;
  1616. node.proxyStr = trojanConstruct(node.group, remarks, server, port, password, host, tls == "true", udp, tfo, scv);
  1617. break;
  1618. case "http"_hash: //quantumult x style http links
  1619. server = trim(configs[0].substr(0, configs[0].rfind(":")));
  1620. port = custom_port.empty() ? trim(configs[0].substr(configs[0].rfind(":") + 1)) : custom_port;
  1621. if(port == "0")
  1622. continue;
  1623. for(i = 1; i < configs.size(); i++)
  1624. {
  1625. vArray = split(trim(configs[i]), "=");
  1626. if(vArray.size() != 2)
  1627. continue;
  1628. itemName = trim(vArray[0]);
  1629. itemVal = trim(vArray[1]);
  1630. switch(hash_(itemName))
  1631. {
  1632. case "username"_hash: username = itemVal; break;
  1633. case "password"_hash: password = itemVal; break;
  1634. case "tag"_hash: remarks = itemVal; break;
  1635. case "over-tls"_hash: tls = itemVal; break;
  1636. case "tls-verification"_hash: scv = itemVal == "false"; break;
  1637. default: continue;
  1638. }
  1639. }
  1640. if(remarks.empty())
  1641. remarks = server + ":" + port;
  1642. if(host.empty() && !isIPv4(server) && !isIPv6(server))
  1643. host = server;
  1644. if(username == "none")
  1645. username.clear();
  1646. if(password == "none")
  1647. password.clear();
  1648. node.linkType = SPEEDTEST_MESSAGE_FOUNDHTTP;
  1649. node.group = HTTP_DEFAULT_GROUP;
  1650. node.proxyStr = httpConstruct(node.group, remarks, server, port, username, password, tls == "true", scv);
  1651. break;
  1652. default:
  1653. continue;
  1654. }
  1655. break;
  1656. }
  1657. node.remarks = remarks;
  1658. node.server = server;
  1659. node.port = to_int(port);
  1660. node.id = index;
  1661. nodes.push_back(node);
  1662. index++;
  1663. }
  1664. return index;
  1665. }
  1666. void explodeSSTap(std::string sstap, const std::string &custom_port, std::vector<nodeInfo> &nodes, bool ss_libev, bool ssr_libev)
  1667. {
  1668. std::string configType, group, remarks, server, port;
  1669. std::string cipher;
  1670. std::string user, pass;
  1671. std::string protocol, protoparam, obfs, obfsparam;
  1672. Document json;
  1673. nodeInfo node;
  1674. unsigned int index = nodes.size();
  1675. json.Parse(sstap.data());
  1676. if(json.HasParseError())
  1677. return;
  1678. for(unsigned int i = 0; i < json["configs"].Size(); i++)
  1679. {
  1680. json["configs"][i]["group"] >> group;
  1681. json["configs"][i]["remarks"] >> remarks;
  1682. json["configs"][i]["server"] >> server;
  1683. port = custom_port.size() ? custom_port : GetMember(json["configs"][i], "server_port");
  1684. if(port == "0")
  1685. continue;
  1686. if(remarks.empty())
  1687. remarks = server + ":" + port;
  1688. json["configs"][i]["password"] >> pass;
  1689. json["configs"][i]["type"] >> configType;
  1690. switch(to_int(configType, 0))
  1691. {
  1692. case 5: //socks 5
  1693. json["configs"][i]["username"] >> user;
  1694. node.linkType = SPEEDTEST_MESSAGE_FOUNDSOCKS;
  1695. node.proxyStr = socksConstruct(group, remarks, server, port, user, pass);
  1696. break;
  1697. case 6: //ss/ssr
  1698. json["configs"][i]["protocol"] >> protocol;
  1699. json["configs"][i]["obfs"] >> obfs;
  1700. json["configs"][i]["method"] >> cipher;
  1701. if(find(ss_ciphers.begin(), ss_ciphers.end(), cipher) != ss_ciphers.end() && protocol == "origin" && obfs == "plain") //is ss
  1702. {
  1703. node.linkType = SPEEDTEST_MESSAGE_FOUNDSS;
  1704. node.proxyStr = ssConstruct(group, remarks, server, port, pass, cipher, "", "", ss_libev);
  1705. }
  1706. else //is ssr cipher
  1707. {
  1708. json["configs"][i]["obfsparam"] >> obfsparam;
  1709. json["configs"][i]["protocolparam"] >> protoparam;
  1710. node.linkType = SPEEDTEST_MESSAGE_FOUNDSSR;
  1711. node.proxyStr = ssrConstruct(group, remarks, base64_encode(remarks), server, port, protocol, cipher, obfs, pass, obfsparam, protoparam, ssr_libev);
  1712. }
  1713. break;
  1714. default:
  1715. continue;
  1716. }
  1717. node.group = group;
  1718. node.remarks = remarks;
  1719. node.id = index;
  1720. node.server = server;
  1721. node.port = to_int(port, 1);
  1722. nodes.push_back(node);
  1723. }
  1724. }
  1725. void explodeNetchConf(std::string netch, bool ss_libev, bool ssr_libev, const std::string &custom_port, std::vector<nodeInfo> &nodes)
  1726. {
  1727. Document json;
  1728. nodeInfo node;
  1729. unsigned int index = nodes.size();
  1730. json.Parse(netch.data());
  1731. if(json.HasParseError())
  1732. return;
  1733. if(!json.HasMember("Server"))
  1734. return;
  1735. for(unsigned int i = 0; i < json["Server"].Size(); i++)
  1736. {
  1737. explodeNetch("Netch://" + base64_encode(SerializeObject(json["Server"][i])), ss_libev, ssr_libev, custom_port, node);
  1738. node.id = index;
  1739. nodes.push_back(node);
  1740. index++;
  1741. }
  1742. }
  1743. bool chkIgnore(const nodeInfo &node, string_array &exclude_remarks, string_array &include_remarks)
  1744. {
  1745. bool excluded = false, included = false;
  1746. //std::string remarks = UTF8ToACP(node.remarks);
  1747. std::string remarks = node.remarks;
  1748. //writeLog(LOG_TYPE_INFO, "Comparing exclude remarks...");
  1749. excluded = std::any_of(exclude_remarks.cbegin(), exclude_remarks.cend(), [&remarks](auto &x)
  1750. {
  1751. return regFind(remarks, x);
  1752. });
  1753. if(include_remarks.size() != 0)
  1754. {
  1755. //writeLog(LOG_TYPE_INFO, "Comparing include remarks...");
  1756. included = std::any_of(include_remarks.cbegin(), include_remarks.cend(), [&remarks](auto &x)
  1757. {
  1758. return regFind(remarks, x);
  1759. });
  1760. }
  1761. else
  1762. {
  1763. included = true;
  1764. }
  1765. return excluded || !included;
  1766. }
  1767. int explodeConf(std::string filepath, const std::string &custom_port, bool sslibev, bool ssrlibev, std::vector<nodeInfo> &nodes)
  1768. {
  1769. std::ifstream infile;
  1770. std::stringstream contentstrm;
  1771. infile.open(filepath);
  1772. contentstrm << infile.rdbuf();
  1773. infile.close();
  1774. return explodeConfContent(contentstrm.str(), custom_port, sslibev, ssrlibev, nodes);
  1775. }
  1776. int explodeConfContent(std::string content, const std::string &custom_port, bool sslibev, bool ssrlibev, std::vector<nodeInfo> &nodes)
  1777. {
  1778. int filetype = -1;
  1779. if(strFind(content, "\"version\""))
  1780. filetype = SPEEDTEST_MESSAGE_FOUNDSS;
  1781. else if(strFind(content, "\"serverSubscribes\""))
  1782. filetype = SPEEDTEST_MESSAGE_FOUNDSSR;
  1783. else if(strFind(content, "\"uiItem\"") || strFind(content, "vnext"))
  1784. filetype = SPEEDTEST_MESSAGE_FOUNDVMESS;
  1785. else if(strFind(content, "\"proxy_apps\""))
  1786. filetype = SPEEDTEST_MESSAGE_FOUNDSSCONF;
  1787. else if(strFind(content, "\"idInUse\""))
  1788. filetype = SPEEDTEST_MESSAGE_FOUNDSSTAP;
  1789. else if(strFind(content, "\"local_address\"") && strFind(content, "\"local_port\""))
  1790. filetype = SPEEDTEST_MESSAGE_FOUNDSSR; //use ssr config parser
  1791. else if(strFind(content, "\"ModeFileNameType\""))
  1792. filetype = SPEEDTEST_MESSAGE_FOUNDNETCH;
  1793. switch(filetype)
  1794. {
  1795. case SPEEDTEST_MESSAGE_FOUNDSS:
  1796. explodeSSConf(content, custom_port, sslibev, nodes);
  1797. break;
  1798. case SPEEDTEST_MESSAGE_FOUNDSSR:
  1799. explodeSSRConf(content, custom_port, sslibev, ssrlibev, nodes);
  1800. break;
  1801. case SPEEDTEST_MESSAGE_FOUNDVMESS:
  1802. explodeVmessConf(content, custom_port, sslibev, nodes);
  1803. break;
  1804. case SPEEDTEST_MESSAGE_FOUNDSSCONF:
  1805. explodeSSAndroid(content, sslibev, custom_port, nodes);
  1806. break;
  1807. case SPEEDTEST_MESSAGE_FOUNDSSTAP:
  1808. explodeSSTap(content, custom_port, nodes, sslibev, ssrlibev);
  1809. break;
  1810. case SPEEDTEST_MESSAGE_FOUNDNETCH:
  1811. explodeNetchConf(content, sslibev, ssrlibev, custom_port, nodes);
  1812. break;
  1813. default:
  1814. //try to parse as a local subscription
  1815. explodeSub(content, sslibev, ssrlibev, custom_port, nodes);
  1816. }
  1817. if(nodes.size() == 0)
  1818. return SPEEDTEST_ERROR_UNRECOGFILE;
  1819. else
  1820. return SPEEDTEST_ERROR_NONE;
  1821. }
  1822. void explode(std::string link, bool sslibev, bool ssrlibev, const std::string &custom_port, nodeInfo &node)
  1823. {
  1824. // TODO: replace strFind with startsWith if appropriate
  1825. if(strFind(link, "ssr://"))
  1826. explodeSSR(link, sslibev, ssrlibev, custom_port, node);
  1827. else if(strFind(link, "vmess://") || strFind(link, "vmess1://"))
  1828. explodeVmess(link, custom_port, node);
  1829. else if(strFind(link, "ss://"))
  1830. explodeSS(link, sslibev, custom_port, node);
  1831. else if(strFind(link, "socks://") || strFind(link, "https://t.me/socks") || strFind(link, "tg://socks"))
  1832. explodeSocks(link, custom_port, node);
  1833. else if(strFind(link, "https://t.me/http") || strFind(link, "tg://http")) //telegram style http link
  1834. explodeHTTP(link, custom_port, node);
  1835. else if(strFind(link, "Netch://"))
  1836. explodeNetch(link, sslibev, ssrlibev, custom_port, node);
  1837. else if(strFind(link, "trojan://"))
  1838. explodeTrojan(link, custom_port, node);
  1839. else if(strFind(link, "http://") || strFind(link, "https://"))
  1840. explodeHTTPSub(link, custom_port, node);
  1841. }
  1842. void explodeSub(std::string sub, bool sslibev, bool ssrlibev, const std::string &custom_port, std::vector<nodeInfo> &nodes)
  1843. {
  1844. std::stringstream strstream;
  1845. std::string strLink;
  1846. bool processed = false;
  1847. nodeInfo node;
  1848. //try to parse as SSD configuration
  1849. if(strFind(sub, "ssd://"))
  1850. {
  1851. explodeSSD(sub, sslibev, custom_port, nodes);
  1852. processed = true;
  1853. }
  1854. //try to parse as clash configuration
  1855. try
  1856. {
  1857. if(!processed && (strFind(sub, "Proxy:") || strFind(sub, "proxies:")))
  1858. {
  1859. regGetMatch(sub, R"(^(?:Proxy|proxies):$\s(?:(?:^ +?.*$| *?-.*$|)\s)+)", 1, &sub);
  1860. Node yamlnode = Load(sub);
  1861. if(yamlnode.size() && (yamlnode["Proxy"].IsDefined() || yamlnode["proxies"].IsDefined()))
  1862. {
  1863. explodeClash(yamlnode, custom_port, nodes, sslibev, ssrlibev);
  1864. processed = true;
  1865. }
  1866. }
  1867. }
  1868. catch (std::exception &e)
  1869. {
  1870. //ignore
  1871. }
  1872. //try to parse as surge configuration
  1873. if(!processed && explodeSurge(sub, custom_port, nodes, sslibev))
  1874. {
  1875. processed = true;
  1876. }
  1877. //try to parse as normal subscription
  1878. if(!processed)
  1879. {
  1880. sub = urlsafe_base64_decode(trim(sub));
  1881. if(regFind(sub, "(vmess|shadowsocks|http|trojan)\\s*?="))
  1882. {
  1883. if(explodeSurge(sub, custom_port, nodes, sslibev))
  1884. return;
  1885. }
  1886. strstream << sub;
  1887. char delimiter = count(sub.begin(), sub.end(), '\n') < 1 ? count(sub.begin(), sub.end(), '\r') < 1 ? ' ' : '\r' : '\n';
  1888. while(getline(strstream, strLink, delimiter))
  1889. {
  1890. if(strLink.rfind("\r") != strLink.npos)
  1891. strLink.erase(strLink.size() - 1);
  1892. node.linkType = -1;
  1893. explode(strLink, sslibev, ssrlibev, custom_port, node);
  1894. if(strLink.size() == 0 || node.linkType == -1)
  1895. {
  1896. continue;
  1897. }
  1898. nodes.push_back(node);
  1899. }
  1900. }
  1901. }
  1902. void filterNodes(std::vector<nodeInfo> &nodes, string_array &exclude_remarks, string_array &include_remarks, int groupID)
  1903. {
  1904. int node_index = 0;
  1905. std::vector<nodeInfo>::iterator iter = nodes.begin();
  1906. /*
  1907. while(iter != nodes.end())
  1908. {
  1909. if(chkIgnore(*iter, exclude_remarks, include_remarks))
  1910. {
  1911. writeLog(LOG_TYPE_INFO, "Node " + iter->group + " - " + iter->remarks + " has been ignored and will not be added.");
  1912. nodes.erase(iter);
  1913. }
  1914. else
  1915. {
  1916. writeLog(LOG_TYPE_INFO, "Node " + iter->group + " - " + iter->remarks + " has been added.");
  1917. iter->id = node_index;
  1918. iter->groupID = groupID;
  1919. ++node_index;
  1920. ++iter;
  1921. }
  1922. }
  1923. */
  1924. bool excluded = false, included = false;
  1925. std::vector<std::unique_ptr<pcre2_code, decltype(&pcre2_code_free)>> exclude_patterns, include_patterns;
  1926. std::vector<std::unique_ptr<pcre2_match_data, decltype(&pcre2_match_data_free)>> exclude_match_data, include_match_data;
  1927. unsigned int i = 0;
  1928. PCRE2_SIZE erroroffset;
  1929. int errornumber, rc;
  1930. for(i = 0; i < exclude_remarks.size(); i++)
  1931. {
  1932. std::unique_ptr<pcre2_code, decltype(&pcre2_code_free)> pattern(pcre2_compile(reinterpret_cast<const unsigned char*>(exclude_remarks[i].c_str()), PCRE2_ZERO_TERMINATED | PCRE2_MULTILINE, 0, &errornumber, &erroroffset, NULL), &pcre2_code_free);
  1933. if(!pattern)
  1934. return;
  1935. exclude_patterns.push_back(std::move(pattern));
  1936. pcre2_jit_compile(exclude_patterns[i].get(), 0);
  1937. std::unique_ptr<pcre2_match_data, decltype(&pcre2_match_data_free)> match_data(pcre2_match_data_create_from_pattern(exclude_patterns[i].get(), NULL), &pcre2_match_data_free);
  1938. exclude_match_data.push_back(std::move(match_data));
  1939. }
  1940. for(i = 0; i < include_remarks.size(); i++)
  1941. {
  1942. std::unique_ptr<pcre2_code, decltype(&pcre2_code_free)> pattern(pcre2_compile(reinterpret_cast<const unsigned char*>(include_remarks[i].c_str()), PCRE2_ZERO_TERMINATED | PCRE2_MULTILINE, 0, &errornumber, &erroroffset, NULL), &pcre2_code_free);
  1943. if(!pattern)
  1944. return;
  1945. include_patterns.push_back(std::move(pattern));
  1946. pcre2_jit_compile(include_patterns[i].get(), 0);
  1947. std::unique_ptr<pcre2_match_data, decltype(&pcre2_match_data_free)> match_data(pcre2_match_data_create_from_pattern(include_patterns[i].get(), NULL), &pcre2_match_data_free);
  1948. include_match_data.push_back(std::move(match_data));
  1949. }
  1950. writeLog(LOG_TYPE_INFO, "Filter started.");
  1951. while(iter != nodes.end())
  1952. {
  1953. excluded = false;
  1954. included = false;
  1955. for(i = 0; i < exclude_patterns.size(); i++)
  1956. {
  1957. rc = pcre2_match(exclude_patterns[i].get(), reinterpret_cast<const unsigned char*>(iter->remarks.c_str()), iter->remarks.size(), 0, 0, exclude_match_data[i].get(), NULL);
  1958. if (rc < 0)
  1959. {
  1960. switch(rc)
  1961. {
  1962. case PCRE2_ERROR_NOMATCH: break;
  1963. default: return;
  1964. }
  1965. }
  1966. else
  1967. excluded = true;
  1968. }
  1969. if(include_patterns.size() > 0)
  1970. for(i = 0; i < include_patterns.size(); i++)
  1971. {
  1972. rc = pcre2_match(include_patterns[i].get(), reinterpret_cast<const unsigned char*>(iter->remarks.c_str()), iter->remarks.size(), 0, 0, include_match_data[i].get(), NULL);
  1973. if (rc < 0)
  1974. {
  1975. switch(rc)
  1976. {
  1977. case PCRE2_ERROR_NOMATCH: break;
  1978. default: return;
  1979. }
  1980. }
  1981. else
  1982. included = true;
  1983. }
  1984. else
  1985. included = true;
  1986. if(excluded || !included)
  1987. {
  1988. writeLog(LOG_TYPE_INFO, "Node " + iter->group + " - " + iter->remarks + " has been ignored and will not be added.");
  1989. nodes.erase(iter);
  1990. }
  1991. else
  1992. {
  1993. writeLog(LOG_TYPE_INFO, "Node " + iter->group + " - " + iter->remarks + " has been added.");
  1994. iter->id = node_index;
  1995. iter->groupID = groupID;
  1996. ++node_index;
  1997. ++iter;
  1998. }
  1999. }
  2000. writeLog(LOG_TYPE_INFO, "Filter done.");
  2001. }
  2002. static inline unsigned long long streamToInt(const std::string &stream)
  2003. {
  2004. if(!stream.size())
  2005. return 0;
  2006. double streamval = 1.0;
  2007. std::vector<std::string> units = {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
  2008. size_t index = units.size();
  2009. do
  2010. {
  2011. index--;
  2012. if(endsWith(stream, units[index]))
  2013. {
  2014. streamval = std::pow(1024, index) * to_number<float>(stream.substr(0, stream.size() - units[index].size()), 0.0);
  2015. break;
  2016. }
  2017. } while(index != 0);
  2018. return (unsigned long long)streamval;
  2019. }
  2020. static inline double percentToDouble(const std::string &percent)
  2021. {
  2022. return stof(percent.substr(0, percent.size() - 1)) / 100.0;
  2023. }
  2024. time_t dateStringToTimestamp(std::string date)
  2025. {
  2026. time_t rawtime;
  2027. time(&rawtime);
  2028. if(startsWith(date, "left="))
  2029. {
  2030. time_t seconds_left = 0;
  2031. date.erase(0, 5);
  2032. if(endsWith(date, "d"))
  2033. {
  2034. date.erase(date.size() - 1);
  2035. seconds_left = to_number<double>(date, 0.0) * 86400.0;
  2036. }
  2037. return rawtime + seconds_left;
  2038. }
  2039. else
  2040. {
  2041. struct tm *expire_time;
  2042. std::vector<std::string> date_array = split(date, ":");
  2043. if(date_array.size() != 6)
  2044. return 0;
  2045. expire_time = localtime(&rawtime);
  2046. expire_time->tm_year = to_int(date_array[0], 1900) - 1900;
  2047. expire_time->tm_mon = to_int(date_array[1], 1) - 1;
  2048. expire_time->tm_mday = to_int(date_array[2]);
  2049. expire_time->tm_hour = to_int(date_array[3]);
  2050. expire_time->tm_min = to_int(date_array[4]);
  2051. expire_time->tm_sec = to_int(date_array[5]);
  2052. return mktime(expire_time);
  2053. }
  2054. }
  2055. bool getSubInfoFromHeader(std::string &header, std::string &result)
  2056. {
  2057. std::string pattern = R"(^(?i:Subscription-UserInfo): (.*?)\s*?$)", retStr;
  2058. if(regFind(header, pattern))
  2059. {
  2060. regGetMatch(header, pattern, 2, NULL, &retStr);
  2061. if(retStr.size())
  2062. {
  2063. result = retStr;
  2064. return true;
  2065. }
  2066. }
  2067. return false;
  2068. }
  2069. bool getSubInfoFromNodes(std::vector<nodeInfo> &nodes, string_array &stream_rules, string_array &time_rules, std::string &result)
  2070. {
  2071. std::string remarks, pattern, target, stream_info, time_info, retStr;
  2072. string_size spos;
  2073. for(nodeInfo &x : nodes)
  2074. {
  2075. remarks = x.remarks;
  2076. if(!stream_info.size())
  2077. {
  2078. for(std::string &y : stream_rules)
  2079. {
  2080. spos = y.rfind("|");
  2081. if(spos == y.npos)
  2082. continue;
  2083. pattern = y.substr(0, spos);
  2084. target = y.substr(spos + 1);
  2085. if(regMatch(remarks, pattern))
  2086. {
  2087. retStr = regReplace(remarks, pattern, target);
  2088. if(retStr != remarks)
  2089. {
  2090. stream_info = retStr;
  2091. break;
  2092. }
  2093. }
  2094. else
  2095. continue;
  2096. }
  2097. }
  2098. remarks = x.remarks;
  2099. if(!time_info.size())
  2100. {
  2101. for(std::string &y : time_rules)
  2102. {
  2103. spos = y.rfind("|");
  2104. if(spos == y.npos)
  2105. continue;
  2106. pattern = y.substr(0, spos);
  2107. target = y.substr(spos + 1);
  2108. if(regMatch(remarks, pattern))
  2109. {
  2110. retStr = regReplace(remarks, pattern, target);
  2111. if(retStr != remarks)
  2112. {
  2113. time_info = retStr;
  2114. break;
  2115. }
  2116. }
  2117. else
  2118. continue;
  2119. }
  2120. }
  2121. if(stream_info.size() && time_info.size())
  2122. break;
  2123. }
  2124. if(!stream_info.size() && !time_info.size())
  2125. return false;
  2126. //calculate how much stream left
  2127. unsigned long long total = 0, left = 0, used = 0, expire = 0;
  2128. std::string total_str = getUrlArg(stream_info, "total"), left_str = getUrlArg(stream_info, "left"), used_str = getUrlArg(stream_info, "used");
  2129. if(strFind(total_str, "%"))
  2130. {
  2131. if(used_str.size())
  2132. {
  2133. used = streamToInt(used_str);
  2134. total = used / (1 - percentToDouble(total_str));
  2135. }
  2136. else if(left_str.size())
  2137. {
  2138. left = streamToInt(left_str);
  2139. total = left / percentToDouble(total_str);
  2140. used = total - left;
  2141. }
  2142. }
  2143. else
  2144. {
  2145. total = streamToInt(total_str);
  2146. if(used_str.size())
  2147. {
  2148. used = streamToInt(used_str);
  2149. }
  2150. else if(left_str.size())
  2151. {
  2152. left = streamToInt(left_str);
  2153. used = total - left;
  2154. }
  2155. }
  2156. result = "upload=0; download=" + std::to_string(used) + "; total=" + std::to_string(total) + ";";
  2157. //calculate expire time
  2158. expire = dateStringToTimestamp(time_info);
  2159. if(expire)
  2160. result += " expire=" + std::to_string(expire) + ";";
  2161. return true;
  2162. }
  2163. bool getSubInfoFromSSD(std::string &sub, std::string &result)
  2164. {
  2165. rapidjson::Document json;
  2166. json.Parse(urlsafe_base64_decode(sub.substr(6)).data());
  2167. if(json.HasParseError())
  2168. return false;
  2169. std::string used_str = GetMember(json, "traffic_used"), total_str = GetMember(json, "traffic_total"), expire_str = GetMember(json, "expiry");
  2170. if(!used_str.size() || !total_str.size())
  2171. return false;
  2172. unsigned long long used = stod(used_str) * std::pow(1024, 3), total = stod(total_str) * std::pow(1024, 3), expire;
  2173. result = "upload=0; download=" + std::to_string(used) + "; total=" + std::to_string(total) + ";";
  2174. expire = dateStringToTimestamp(regReplace(expire_str, "(\\d+)-(\\d+)-(\\d+) (.*)", "$1:$2:$3:$4"));
  2175. if(expire)
  2176. result += " expire=" + std::to_string(expire) + ";";
  2177. return true;
  2178. }