CLashConfig.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_is_qut(false), m_log(nullptr)
  7. {
  8. }
  9. CLashConfig::~CLashConfig(void)
  10. {
  11. if (m_process)
  12. {
  13. delete m_process;
  14. m_process = nullptr;
  15. }
  16. if (m_log)
  17. {
  18. delete m_log;
  19. m_log = nullptr;
  20. }
  21. }
  22. BOOL CLashConfig::MakeClash()
  23. {
  24. return 0;
  25. }
  26. BOOL CLashConfig::InitClash()
  27. {
  28. if (!m_process)
  29. {
  30. m_process = new CProcess();
  31. }
  32. m_http_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9300,9500);
  33. m_socks_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9600,9800);
  34. m_c_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9900);
  35. /*std::string dir = CLASHCONFIGDIR;
  36. auto name_file = dir + "\\" + CLASHCONFIINIT;
  37. std::string logfile = path + "\\config\\log.log";
  38. if (m_log == nullptr)
  39. {
  40. m_log = new Logger(Logger::file_and_terminal, Logger::info, logfile);
  41. }*/
  42. auto name_file = DSPROXY_CONFIG_INIT_ClASH_NAME;
  43. if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode) {
  44. /*name_file = dir + "\\" + CLASHCONFIINIT;*/
  45. name_file = DSPROXY_CONFIG_TUN_ClASH_NAME;
  46. }
  47. YAML::Node root;
  48. root["port"] = m_http_port;
  49. root["socks-port"] = m_socks_port;
  50. root["allow-lan"] = true;
  51. root["external-controller"] = "127.0.0.1:" + std::to_string(m_c_port);
  52. try
  53. {
  54. auto config_path = CApp::getSingletonPtr()->GetConfigPath().wstring() + L"\\" + name_file;
  55. std::ofstream ofstream(config_path);
  56. ofstream << root << std::endl;
  57. ofstream.close();
  58. return TRUE;
  59. }
  60. catch (const std::exception&)
  61. {
  62. return FALSE;
  63. }
  64. return TRUE;
  65. }
  66. BOOL CLashConfig::StartClash()
  67. {
  68. //std::wstring config = path + L"\\" + WCLASHCONFIGDIR;
  69. ////args.push_back("-d");
  70. //////args.push_back(std::filesystem::current_path().string() + "\\route");
  71. ////args.push_back(path + "\\config");
  72. ////args.push_back("-f");
  73. ////args.push_back(path + "\\config\\configinit.yaml");
  74. //std::wstring run_config = config + L"\\" + S_CA2W(CLASHCONFIINIT).GetBuffer(0);
  75. //SStringW path_config;
  76. //path_config.Format(L"%s\\%s -d %s -f %s", config.c_str(),CLASHEXE,config.c_str(), run_config.c_str());
  77. auto assetsDir = std::filesystem::current_path() / CLASH_ASSETS_DIR_NAME;
  78. auto confg_path = CApp::getSingletonPtr()->GetConfigPath() / DSPROXY_CONFIG_INIT_ClASH_NAME;
  79. CProcessManager::getSingletonPtr()->SetArgs(assetsDir / DSPROXY_EXE_NAME, assetsDir, std::move(confg_path));
  80. /*ProcessManager::SetConfigFile(confg_path / DSPROXY_CONFIG_INIT_ClASH_NAME);*/
  81. //m_Asyntask.AddTask(&CLashConfig::ThreadFun_process_Config, this, (LPARAM)NULL);
  82. if (CProcessManager::getSingletonPtr()->Start())
  83. {
  84. CApp::getSingletonPtr()->SetCLashRuning(true);
  85. }
  86. /*if (m_process)
  87. {
  88. m_process->Create(CProcess::ASYNC);
  89. if (m_process->Execute(path_config.GetBuffer(0)))
  90. {
  91. }
  92. }*/
  93. return 0;
  94. }
  95. BOOL CLashConfig::StopClash()
  96. {
  97. m_is_qut = true;
  98. CProcessManager::getSingletonPtr()->Stop();
  99. //char ch[MAX_PATH];
  100. //memset(ch, 0, MAX_PATH);
  101. //sprintf_s(ch, "ok\n");
  102. //m_process->WriteSome(ch, sizeof(ch));
  103. return 0;
  104. }
  105. YAML::Node CLashConfig::buildShadowsocks()
  106. {
  107. return YAML::Node();
  108. }
  109. YAML::Node CLashConfig::buildtrojan()
  110. {
  111. return YAML::Node();
  112. }
  113. YAML::Node CLashConfig::buildv2ray()
  114. {
  115. return YAML::Node();
  116. }
  117. std::vector<YAML::Node> CLashConfig::buildv2rayHost()
  118. {
  119. return std::vector<YAML::Node>();
  120. }
  121. YAML::Node CLashConfig::builTunConfig()
  122. {
  123. return YAML::Node();
  124. }
  125. YAML::Node CLashConfig::buildDnsConfig()
  126. {
  127. return YAML::Node();
  128. }
  129. std::vector<YAML::Node> CLashConfig::buildProxyGroups()
  130. {
  131. return std::vector<YAML::Node>();
  132. }
  133. std::vector<YAML::Node> CLashConfig::buildRules()
  134. {
  135. return std::vector<YAML::Node>();
  136. }
  137. void CLashConfig::ThreadFun_process_Config(LPARAM lParam)
  138. {
  139. if (CProcessManager::getSingletonPtr()->Start())
  140. {
  141. CApp::getSingletonPtr()->SetCLashRuning(true);
  142. HANDLE hClashProcess = CProcessManager::getSingletonPtr()->GetClashProcessInfo().hProcess;
  143. for (size_t i = 0; i < 5; ++i) {
  144. }
  145. }
  146. //if (m_process)
  147. //{
  148. // //char ch[MAX_PATH];
  149. // //memset(ch, 0, MAX_PATH);
  150. // //do
  151. // //{
  152. // // size_t bytesRead = m_process->ReadLine(ch, sizeof(ch));
  153. // //
  154. // // if (bytesRead == 0)
  155. // // {
  156. // //
  157. // // break;
  158. // // }
  159. // // if (m_is_qut)
  160. // // {
  161. // //
  162. // // break;
  163. // // }
  164. // // if (m_log)
  165. // // {
  166. // // std::string log(ch);
  167. // // m_log->INFO(log);
  168. // // }
  169. // // /*if (ch[bytesRead - 1] == '\n') break;*/
  170. // //} while (true);
  171. //
  172. //}
  173. }