speedtestutil.cpp 88 KB

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