speedtestutil.cpp 80 KB

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