CLashConfig.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 path = std::filesystem::current_path().string();
  36. std::string dir = CLASHCONFIGDIR;
  37. auto name_file = dir + "\\" + CLASHCONFIINIT;
  38. std::string logfile = path + "\\config\\log.log";
  39. if (m_log == nullptr)
  40. {
  41. m_log = new Logger(Logger::file_and_terminal, Logger::info, logfile);
  42. }
  43. if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode) {
  44. name_file = dir + "\\" + CLASHCONFIINIT;
  45. }
  46. YAML::Node root;
  47. root["port"] = m_http_port;
  48. root["socks-port"] = m_socks_port;
  49. root["allow-lan"] = true;
  50. root["external-controller"] = "127.0.0.1:" + std::to_string(m_c_port);
  51. try
  52. {
  53. std::ofstream ofstream(name_file);
  54. ofstream << root << std::endl;
  55. ofstream.close();
  56. return TRUE;
  57. }
  58. catch (const std::exception&)
  59. {
  60. return FALSE;
  61. }
  62. return TRUE;
  63. }
  64. BOOL CLashConfig::StartClash()
  65. {
  66. std::wstring path = std::filesystem::current_path().wstring();
  67. std::wstring config = path + L"\\" + WCLASHCONFIGDIR;
  68. //args.push_back("-d");
  69. ////args.push_back(std::filesystem::current_path().string() + "\\route");
  70. //args.push_back(path + "\\config");
  71. //args.push_back("-f");
  72. //args.push_back(path + "\\config\\configinit.yaml");
  73. std::wstring run_config = config + L"\\" + S_CA2W(CLASHCONFIINIT).GetBuffer(0);
  74. SStringW path_config;
  75. path_config.Format(L"%s\\%s -d %s -f %s", config.c_str(),CLASHEXE,config.c_str(), run_config.c_str());
  76. if (m_process)
  77. {
  78. m_process->Create(CProcess::ASYNC);
  79. if (m_process->Execute(path_config.GetBuffer(0)))
  80. {
  81. m_Asyntask.AddTask(&CLashConfig::ThreadFun_process_Config, this, (LPARAM)NULL);
  82. }
  83. }
  84. return 0;
  85. }
  86. BOOL CLashConfig::StopClash()
  87. {
  88. m_is_qut = true;
  89. m_Asyntask.Close();
  90. char ch[MAX_PATH];
  91. memset(ch, 0, MAX_PATH);
  92. sprintf_s(ch, "ok\n");
  93. m_process->WriteSome(ch, sizeof(ch));
  94. return 0;
  95. }
  96. YAML::Node CLashConfig::buildShadowsocks()
  97. {
  98. return YAML::Node();
  99. }
  100. YAML::Node CLashConfig::buildtrojan()
  101. {
  102. return YAML::Node();
  103. }
  104. YAML::Node CLashConfig::buildv2ray()
  105. {
  106. return YAML::Node();
  107. }
  108. std::vector<YAML::Node> CLashConfig::buildv2rayHost()
  109. {
  110. return std::vector<YAML::Node>();
  111. }
  112. YAML::Node CLashConfig::builTunConfig()
  113. {
  114. return YAML::Node();
  115. }
  116. YAML::Node CLashConfig::buildDnsConfig()
  117. {
  118. return YAML::Node();
  119. }
  120. std::vector<YAML::Node> CLashConfig::buildProxyGroups()
  121. {
  122. return std::vector<YAML::Node>();
  123. }
  124. std::vector<YAML::Node> CLashConfig::buildRules()
  125. {
  126. return std::vector<YAML::Node>();
  127. }
  128. void CLashConfig::ThreadFun_process_Config(LPARAM lParam)
  129. {
  130. if (m_process)
  131. {
  132. char ch[MAX_PATH];
  133. memset(ch, 0, MAX_PATH);
  134. do
  135. {
  136. size_t bytesRead = m_process->ReadLine(ch, sizeof(ch));
  137. if (bytesRead == 0)
  138. {
  139. break;
  140. }
  141. if (m_is_qut)
  142. {
  143. break;
  144. }
  145. if (m_log)
  146. {
  147. std::string log(ch);
  148. m_log->INFO(log);
  149. }
  150. /*if (ch[bytesRead - 1] == '\n') break;*/
  151. } while (true);
  152. }
  153. }