#include "stdafx.h" #include "CLashConfig.h" #include "CApp.h" #include "comm.h" #include "FileOperate.h" 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) { } CLashConfig::~CLashConfig(void) { if (m_process) { delete m_process; m_process = nullptr; } if (m_log) { delete m_log; m_log = nullptr; } } BOOL CLashConfig::MakeClash() { return 0; } BOOL CLashConfig::InitClash() { if (!m_process) { m_process = new CProcess(); } m_http_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9300,9500); m_socks_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9600,9800); m_c_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9900); /*std::string dir = CLASHCONFIGDIR; auto name_file = dir + "\\" + CLASHCONFIINIT; std::string logfile = path + "\\config\\log.log"; if (m_log == nullptr) { m_log = new Logger(Logger::file_and_terminal, Logger::info, logfile); }*/ auto name_file = DSPROXY_CONFIG_INIT_ClASH_NAME; if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode) { /*name_file = dir + "\\" + CLASHCONFIINIT;*/ name_file = DSPROXY_CONFIG_TUN_ClASH_NAME; } YAML::Node root; root["port"] = m_http_port; root["socks-port"] = m_socks_port; root["allow-lan"] = true; root["external-controller"] = "127.0.0.1:" + std::to_string(m_c_port); try { auto config_path = CApp::getSingletonPtr()->GetConfigPath().wstring() + L"\\" + name_file; std::ofstream ofstream(config_path); ofstream << root << std::endl; ofstream.close(); return TRUE; } catch (const std::exception&) { return FALSE; } return TRUE; } BOOL CLashConfig::StartClash() { //std::wstring config = path + L"\\" + WCLASHCONFIGDIR; ////args.push_back("-d"); //////args.push_back(std::filesystem::current_path().string() + "\\route"); ////args.push_back(path + "\\config"); ////args.push_back("-f"); ////args.push_back(path + "\\config\\configinit.yaml"); //std::wstring run_config = config + L"\\" + S_CA2W(CLASHCONFIINIT).GetBuffer(0); //SStringW path_config; //path_config.Format(L"%s\\%s -d %s -f %s", config.c_str(),CLASHEXE,config.c_str(), run_config.c_str()); auto assetsDir = std::filesystem::current_path() / CLASH_ASSETS_DIR_NAME; auto confg_path = CApp::getSingletonPtr()->GetConfigPath() / DSPROXY_CONFIG_INIT_ClASH_NAME; CProcessManager::getSingletonPtr()->SetArgs(assetsDir / DSPROXY_EXE_NAME, assetsDir, std::move(confg_path)); /*ProcessManager::SetConfigFile(confg_path / DSPROXY_CONFIG_INIT_ClASH_NAME);*/ //m_Asyntask.AddTask(&CLashConfig::ThreadFun_process_Config, this, (LPARAM)NULL); if (CProcessManager::getSingletonPtr()->Start()) { CApp::getSingletonPtr()->SetCLashRuning(true); } /*if (m_process) { m_process->Create(CProcess::ASYNC); if (m_process->Execute(path_config.GetBuffer(0))) { } }*/ return 0; } BOOL CLashConfig::StopClash() { m_is_qut = true; CProcessManager::getSingletonPtr()->Stop(); //char ch[MAX_PATH]; //memset(ch, 0, MAX_PATH); //sprintf_s(ch, "ok\n"); //m_process->WriteSome(ch, sizeof(ch)); return 0; } YAML::Node CLashConfig::buildShadowsocks() { return YAML::Node(); } YAML::Node CLashConfig::buildtrojan() { return YAML::Node(); } YAML::Node CLashConfig::buildv2ray() { return YAML::Node(); } std::vector CLashConfig::buildv2rayHost() { return std::vector(); } YAML::Node CLashConfig::builTunConfig() { return YAML::Node(); } YAML::Node CLashConfig::buildDnsConfig() { return YAML::Node(); } std::vector CLashConfig::buildProxyGroups() { return std::vector(); } std::vector CLashConfig::buildRules() { return std::vector(); } void CLashConfig::ThreadFun_process_Config(LPARAM lParam) { if (CProcessManager::getSingletonPtr()->Start()) { CApp::getSingletonPtr()->SetCLashRuning(true); HANDLE hClashProcess = CProcessManager::getSingletonPtr()->GetClashProcessInfo().hProcess; for (size_t i = 0; i < 5; ++i) { } } //if (m_process) //{ // //char ch[MAX_PATH]; // //memset(ch, 0, MAX_PATH); // //do // //{ // // size_t bytesRead = m_process->ReadLine(ch, sizeof(ch)); // // // // if (bytesRead == 0) // // { // // // // break; // // } // // if (m_is_qut) // // { // // // // break; // // } // // if (m_log) // // { // // std::string log(ch); // // m_log->INFO(log); // // } // // /*if (ch[bytesRead - 1] == '\n') break;*/ // //} while (true); // //} }