CLashConfig.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #include "stdafx.h"
  2. #include "CLashConfig.h"
  3. #include "CApp.h"
  4. #include "comm.h"
  5. #include "FileOperate.h"
  6. CLashConfig::CLashConfig() : m_socks_port(9200), m_http_port(9300), m_c_port(9090), m_process(nullptr), m_Asyntask(1), m_AsyntaskProcessMonitor(1), m_is_qut(false), m_log(nullptr)
  7. {
  8. _hEvent = nullptr;
  9. }
  10. CLashConfig::~CLashConfig(void)
  11. {
  12. if (m_process)
  13. {
  14. delete m_process;
  15. m_process = nullptr;
  16. }
  17. if (m_log)
  18. {
  19. delete m_log;
  20. m_log = nullptr;
  21. }
  22. }
  23. BOOL CLashConfig::MakeClash()
  24. {
  25. buildRules();
  26. YAML::Node root;
  27. root["port"] = m_http_port;
  28. root["socks-port"] = m_socks_port;
  29. root["allow-lan"] = true;
  30. root["external-controller"] = "127.0.0.1:" + std::to_string(m_c_port);
  31. if (CApp::getSingletonPtr()->GetRouteMode() == ROUT_MODE::cn_mode)
  32. {
  33. root["mode"] = "rule";
  34. }
  35. else {
  36. root["mode"] = "rule";
  37. }
  38. #ifdef _DEBUG
  39. root["log-level"] = "debug";
  40. #else
  41. root["log-level"] = "info";
  42. #endif
  43. root["dns"] = buildDnsConfig();
  44. if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode)
  45. {
  46. root["tun"] = builTunConfig();
  47. }
  48. //proxys.reserve(0);
  49. //CList_node::getSingletonPtr()->listnode;
  50. YAML::Node proxies;
  51. //for each (LISTNODE node in CList_node::getSingletonPtr()->listnode)
  52. //{
  53. // if (node.type == "shadowsocks") {
  54. // proxies.push_back(buildShadowsocks(&node));
  55. // }
  56. // else if (node.type == "trojan") {
  57. // proxies.push_back(buildtrojan(&node));
  58. // }
  59. // else if (node.type == "v2ray") {
  60. // proxies.push_back(buildv2ray(&node));
  61. // }
  62. //}
  63. root["proxies"] = proxies;
  64. root["proxy-groups"] = buildProxyGroups();
  65. YAML::Node reject;
  66. YAML::Node google;
  67. YAML::Node gfw;
  68. YAML::Node proxy;
  69. YAML::Node cncidr;
  70. reject["type"] = "http";
  71. reject["behavior"] = "domain";
  72. reject["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt";
  73. reject["path"] = "./ruleset/reject.yaml";
  74. reject["interval"] = 86400;
  75. google["type"] = "http";
  76. google["behavior"] = "domain";
  77. google["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/google.txt";
  78. google["path"] = "./ruleset/google.yaml";
  79. google["interval"] = 86400;
  80. gfw["type"] = "http";
  81. gfw["behavior"] = "domain";
  82. gfw["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/gfw.txt";
  83. gfw["path"] = "./ruleset/gfw.yaml";
  84. gfw["interval"] = 86400;
  85. proxy["type"] = "http";
  86. proxy["behavior"] = "domain";
  87. proxy["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/proxy.txt";
  88. proxy["path"] = "./ruleset/proxy.yaml";
  89. proxy["interval"] = 86400;
  90. cncidr["type"] = "http";
  91. cncidr["behavior"] = "domain";
  92. cncidr["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/cncidr.txt";
  93. cncidr["path"] = "./ruleset/cncidr.yaml";
  94. cncidr["interval"] = 86400;
  95. if (CApp::getSingletonPtr()->GetRouteMode() == ROUT_MODE::cn_mode)
  96. {
  97. root["rule-providers"]["reject"] = reject;
  98. root["rule-providers"]["cncidr"] = cncidr;
  99. root["rule-providers"]["proxy"] = proxy;
  100. root["rule-providers"]["gfw"] = gfw;
  101. root["rule-providers"]["google"] = google;
  102. }
  103. root["rules"] = m_rules;
  104. auto name_file = "config.yaml";
  105. if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode) {
  106. name_file = "tun_config.yaml";
  107. }
  108. //std::ofstream ofstream(path + "\\config\\" + name_file);
  109. //// ÉèÖÃÅäÖÃÎļþnodeÊý
  110. //ofstream << root << std::endl;
  111. //ofstream.close();
  112. return TRUE;
  113. }
  114. BOOL CLashConfig::InitClash()
  115. {
  116. if (!m_process)
  117. {
  118. m_process = new CProcess();
  119. }
  120. std::string logfile = std::filesystem::current_path().string() + "\\log\\log.log";
  121. if (m_log == nullptr)
  122. {
  123. m_log = new Logger(Logger::file_and_terminal, Logger::info, logfile);
  124. }
  125. m_http_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9300,9500);
  126. m_socks_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9600,9800);
  127. m_c_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9900);
  128. ;
  129. /*std::string dir = CLASHCONFIGDIR;
  130. auto name_file = dir + "\\" + CLASHCONFIINIT;
  131. */
  132. auto name_file = DSPROXY_CONFIG_INIT_ClASH_NAME;
  133. if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode) {
  134. /*name_file = dir + "\\" + CLASHCONFIINIT;*/
  135. name_file = DSPROXY_CONFIG_TUN_ClASH_NAME;
  136. }
  137. YAML::Node root;
  138. root["port"] = m_http_port;
  139. root["socks-port"] = m_socks_port;
  140. root["allow-lan"] = true;
  141. root["external-controller"] = "127.0.0.1:" + std::to_string(m_c_port);
  142. try
  143. {
  144. auto config_path = CApp::getSingletonPtr()->GetConfigPath().wstring() + L"\\" + name_file;
  145. std::ofstream ofstream(config_path);
  146. ofstream << root << std::endl;
  147. ofstream.close();
  148. return TRUE;
  149. }
  150. catch (const std::exception&)
  151. {
  152. return FALSE;
  153. }
  154. return TRUE;
  155. }
  156. BOOL CLashConfig::StartClash()
  157. {
  158. /*wil::unique_handle hEvent(CreateEventW(nullptr, FALSE, FALSE, nullptr));
  159. THROW_LAST_ERROR_IF_NULL(hEvent);*/
  160. //std::wstring config = path + L"\\" + WCLASHCONFIGDIR;
  161. ////args.push_back("-d");
  162. //////args.push_back(std::filesystem::current_path().string() + "\\route");
  163. ////args.push_back(path + "\\config");
  164. ////args.push_back("-f");
  165. ////args.push_back(path + "\\config\\configinit.yaml");
  166. //std::wstring run_config = config + L"\\" + S_CA2W(CLASHCONFIINIT).GetBuffer(0);
  167. //SStringW path_config;
  168. //path_config.Format(L"%s\\%s -d %s -f %s", config.c_str(),CLASHEXE,config.c_str(), run_config.c_str());
  169. auto assetsDir = std::filesystem::current_path() / CLASH_ASSETS_DIR_NAME;
  170. auto confg_path = CApp::getSingletonPtr()->GetConfigPath() / DSPROXY_CONFIG_INIT_ClASH_NAME;
  171. CProcessManager::getSingletonPtr()->SetArgs(assetsDir / DSPROXY_EXE_NAME, assetsDir, std::move(confg_path));
  172. /*ProcessManager::SetConfigFile(confg_path / DSPROXY_CONFIG_INIT_ClASH_NAME);*/
  173. /* m_Asyntask.AddTask(&CLashConfig::ThreadFun_process_Config, this, (LPARAM)CProcessManager::getSingletonPtr());*/
  174. if (CProcessManager::getSingletonPtr()->Start())
  175. {
  176. CApp::getSingletonPtr()->SetCLashRuning(true);
  177. m_AsyntaskProcessMonitor.AddTask(&CLashConfig::ThreadFun_ProcessMonitor_Config, this, (LPARAM)CProcessManager::getSingletonPtr());
  178. }
  179. /*if (m_process)
  180. {
  181. m_process->Create(CProcess::ASYNC);
  182. if (m_process->Execute(path_config.GetBuffer(0)))
  183. {
  184. }
  185. }*/
  186. return 0;
  187. }
  188. BOOL CLashConfig::StopClash()
  189. {
  190. m_is_qut = true;
  191. /*SetEvent(_hEvent.get());*/
  192. //char ch[MAX_PATH];
  193. //memset(ch, 0, MAX_PATH);
  194. //sprintf_s(ch, "ok\n");
  195. //m_process->WriteSome(ch, sizeof(ch));
  196. return 0;
  197. }
  198. YAML::Node CLashConfig::buildShadowsocks()
  199. {
  200. return YAML::Node();
  201. }
  202. YAML::Node CLashConfig::buildtrojan()
  203. {
  204. return YAML::Node();
  205. }
  206. YAML::Node CLashConfig::buildv2ray()
  207. {
  208. return YAML::Node();
  209. }
  210. std::vector<YAML::Node> CLashConfig::buildv2rayHost()
  211. {
  212. std::vector<YAML::Node> p;
  213. /*if (node) {
  214. YAML::Node t_map;
  215. t_map["host"] = node->v2_host;
  216. p.push_back(t_map);
  217. }*/
  218. return p;
  219. }
  220. YAML::Node CLashConfig::builTunConfig()
  221. {
  222. std::vector<std::string> dns;
  223. dns.push_back("198.18.0.2:53");
  224. YAML::Node node;
  225. node["enable"] = true;
  226. node["stack"] = "gvisor";
  227. node["dns-hijack"] = dns;
  228. node["auto-route"] = true;
  229. node["auto-detect-interface"] = true;
  230. return node;
  231. }
  232. YAML::Node CLashConfig::buildDnsConfig()
  233. {
  234. YAML::Node dns;
  235. std::vector<std::string> dns_arr;
  236. dns_arr.push_back("114.114.114.114");
  237. dns_arr.push_back("8.8.8.8");
  238. dns_arr.push_back("tls://dns.rubyfish.cn:853");
  239. dns_arr.push_back("https://1.1.1.1/dns-query");
  240. dns["enable"] = true;
  241. if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode)
  242. {
  243. dns["enhanced-mode"] = "redir-host";
  244. }
  245. else
  246. {
  247. //dns["enable"] = false;
  248. }
  249. dns["nameserver"] = dns_arr;
  250. //dns["use-hosts"] = true;
  251. return dns;
  252. }
  253. std::vector<YAML::Node> CLashConfig::buildProxyGroups()
  254. {
  255. std::vector<YAML::Node> dns_arr;
  256. std::vector<std::string> proxy;
  257. YAML::Node proxies;
  258. /*for each (LISTNODE node in CList_node::getSingletonPtr()->listnode)
  259. {
  260. proxy.push_back(node.name.c_str());
  261. }*/
  262. YAML::Node li;
  263. li["name"] = "Proxy";
  264. li["type"] = "select";
  265. li["proxies"] = proxy;
  266. dns_arr.push_back(li);
  267. return dns_arr;
  268. }
  269. std::vector<YAML::Node> CLashConfig::buildRules()
  270. {
  271. std::vector<YAML::Node> p;
  272. m_rules.clear();
  273. if (CApp::getSingletonPtr()->GetRouteMode() == ROUT_MODE::cn_mode)
  274. {
  275. m_rules.push_back("RULE-SET,proxy,Proxy");
  276. m_rules.push_back("RULE-SET,google,Proxy");
  277. m_rules.push_back("RULE-SET,gfw,Proxy");
  278. m_rules.push_back("RULE-SET,cncidr,DIRECT");
  279. m_rules.push_back("GEOIP,CN,DIRECT");
  280. m_rules.push_back("IP-CIDR,127.0.0.0/8,DIRECT");
  281. m_rules.push_back("IP-CIDR,172.16.0.0/12,DIRECT");
  282. m_rules.push_back("IP-CIDR,192.168.0.0/16,DIRECT");
  283. m_rules.push_back("IP-CIDR,10.0.0.0/8,DIRECT");
  284. m_rules.push_back("IP-CIDR,100.64.0.0/10,DIRECT");
  285. }
  286. else
  287. {
  288. // m_rules.push_back("RULE-SET,proxy,Proxy");
  289. // m_rules.push_back("RULE-SET,google,Proxy");
  290. // m_rules.push_back("RULE-SET,gfw,Proxy");
  291. // m_rules.push_back("RULE-SET,cncidr,Proxy");
  292. m_rules.push_back("GEOIP,CN,Proxy");
  293. m_rules.push_back("IP-CIDR,127.0.0.0/8,DIRECT");
  294. m_rules.push_back("IP-CIDR,172.16.0.0/12,DIRECT");
  295. m_rules.push_back("IP-CIDR,192.168.0.0/16,DIRECT");
  296. m_rules.push_back("IP-CIDR,10.0.0.0/8,DIRECT");
  297. m_rules.push_back("IP-CIDR,100.64.0.0/10,DIRECT");
  298. }
  299. m_rules.push_back("MATCH,Proxy");
  300. return p;
  301. }
  302. std::vector<YAML::Node> CLashConfig::buildruleproviders()
  303. {
  304. std::vector<YAML::Node> pp;
  305. YAML::Node node;
  306. YAML::Node reject;
  307. YAML::Node google;
  308. YAML::Node gfw;
  309. YAML::Node proxy;
  310. YAML::Node cncidr;
  311. reject["type"] = "http";
  312. reject["behavior"] = "domain";
  313. reject["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt";
  314. reject["path"] = "./ruleset/reject.yaml";
  315. reject["interval"] = 86400;
  316. node["reject"] = reject;
  317. pp.push_back(node);
  318. google["type"] = "http";
  319. google["behavior"] = "domain";
  320. google["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/google.txt";
  321. google["path"] = "./ruleset/google.yaml";
  322. google["interval"] = 86400;
  323. node["google"] = google;
  324. pp.push_back(node);
  325. gfw["type"] = "http";
  326. gfw["behavior"] = "domain";
  327. gfw["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/gfw.txt";
  328. gfw["path"] = "./ruleset/gfw.yaml";
  329. gfw["interval"] = 86400;
  330. node["gfw"] = gfw;
  331. pp.push_back(node);
  332. proxy["type"] = "http";
  333. proxy["behavior"] = "domain";
  334. proxy["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/proxy.txt";
  335. proxy["path"] = "./ruleset/proxy.yaml";
  336. proxy["interval"] = 86400;
  337. node["proxy"] = proxy;
  338. pp.push_back(node);
  339. cncidr["type"] = "http";
  340. cncidr["behavior"] = "domain";
  341. cncidr["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/cncidr.txt";
  342. cncidr["path"] = "./ruleset/cncidr.yaml";
  343. cncidr["interval"] = 86400;
  344. node["cncidr"] = proxy;
  345. pp.push_back(node);
  346. return pp;
  347. }
  348. void CLashConfig::ThreadFun_process_Config(LPARAM lParam)
  349. {
  350. CProcessManager* p = (CProcessManager*)lParam;
  351. if (p->Start())
  352. {
  353. }
  354. WaitForSingleObject(_hEvent.get(),INFINITE);
  355. }
  356. void CLashConfig::ThreadFun_ProcessMonitor_Config(LPARAM lParam)
  357. {
  358. CProcessManager* p = (CProcessManager*)lParam;
  359. if (p)
  360. {
  361. HANDLE hClashProcess = nullptr;
  362. HANDLE hSubProcess = nullptr;
  363. do
  364. {
  365. if (p->IsRunning() != State::Running)
  366. {
  367. break;
  368. }
  369. } while (hClashProcess = p->GetClashProcessInfo().hProcess);
  370. }
  371. }
  372. //winrt::fire_and_forget CLashConfig::_StartClash()
  373. //{
  374. // if (CProcessManager::getSingletonPtr()->Start())
  375. // {
  376. // CApp::getSingletonPtr()->SetCLashRuning(true);
  377. //
  378. //
  379. //
  380. // /*HANDLE hClashProcess = CProcessManager::getSingletonPtr()->GetClashProcessInfo().hProcess;
  381. //
  382. // for (size_t i = 0; i < 5; ++i) {
  383. //
  384. // }*/
  385. // }
  386. //
  387. //
  388. //
  389. //}
  390. //
  391. //winrt::Windows::Foundation::IAsyncAction CLashConfig::ProcessMonitor()
  392. //{
  393. // HANDLE hSubProcess = CProcessManager::getSingletonPtr()->GetClashProcessInfo().hProcess,
  394. // hClashProcess = CProcessManager::getSingletonPtr()->GetClashProcessInfo().hProcess;
  395. //
  396. // winrt::resume_on_signal(hClashProcess);
  397. //
  398. // /*std::future<void> f = std::async(std::launch::async, []() {
  399. // })*/
  400. //
  401. //
  402. //
  403. //
  404. //}