|
@@ -5,8 +5,11 @@
|
|
|
#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() : 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)
|
|
|
{
|
|
|
+
|
|
|
+ _hEvent = nullptr;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
CLashConfig::~CLashConfig(void)
|
|
@@ -25,7 +28,120 @@ CLashConfig::~CLashConfig(void)
|
|
|
|
|
|
BOOL CLashConfig::MakeClash()
|
|
|
{
|
|
|
- return 0;
|
|
|
+ buildRules();
|
|
|
+ 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);
|
|
|
+ if (CApp::getSingletonPtr()->GetRouteMode() == ROUT_MODE::cn_mode)
|
|
|
+ {
|
|
|
+ root["mode"] = "rule";
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ root["mode"] = "rule";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#ifdef _DEBUG
|
|
|
+ root["log-level"] = "debug";
|
|
|
+#else
|
|
|
+ root["log-level"] = "info";
|
|
|
+#endif
|
|
|
+
|
|
|
+ root["dns"] = buildDnsConfig();
|
|
|
+
|
|
|
+ if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode)
|
|
|
+ {
|
|
|
+ root["tun"] = builTunConfig();
|
|
|
+ }
|
|
|
+
|
|
|
+ //proxys.reserve(0);
|
|
|
+
|
|
|
+ //CList_node::getSingletonPtr()->listnode;
|
|
|
+ YAML::Node proxies;
|
|
|
+ //for each (LISTNODE node in CList_node::getSingletonPtr()->listnode)
|
|
|
+ //{
|
|
|
+ // if (node.type == "shadowsocks") {
|
|
|
+ // proxies.push_back(buildShadowsocks(&node));
|
|
|
+ // }
|
|
|
+ // else if (node.type == "trojan") {
|
|
|
+ // proxies.push_back(buildtrojan(&node));
|
|
|
+ // }
|
|
|
+ // else if (node.type == "v2ray") {
|
|
|
+ // proxies.push_back(buildv2ray(&node));
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+
|
|
|
+
|
|
|
+ root["proxies"] = proxies;
|
|
|
+ root["proxy-groups"] = buildProxyGroups();
|
|
|
+
|
|
|
+ YAML::Node reject;
|
|
|
+ YAML::Node google;
|
|
|
+ YAML::Node gfw;
|
|
|
+ YAML::Node proxy;
|
|
|
+ YAML::Node cncidr;
|
|
|
+
|
|
|
+
|
|
|
+ reject["type"] = "http";
|
|
|
+ reject["behavior"] = "domain";
|
|
|
+ reject["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt";
|
|
|
+ reject["path"] = "./ruleset/reject.yaml";
|
|
|
+ reject["interval"] = 86400;
|
|
|
+
|
|
|
+ google["type"] = "http";
|
|
|
+ google["behavior"] = "domain";
|
|
|
+ google["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/google.txt";
|
|
|
+ google["path"] = "./ruleset/google.yaml";
|
|
|
+ google["interval"] = 86400;
|
|
|
+
|
|
|
+
|
|
|
+ gfw["type"] = "http";
|
|
|
+ gfw["behavior"] = "domain";
|
|
|
+ gfw["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/gfw.txt";
|
|
|
+ gfw["path"] = "./ruleset/gfw.yaml";
|
|
|
+ gfw["interval"] = 86400;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ proxy["type"] = "http";
|
|
|
+ proxy["behavior"] = "domain";
|
|
|
+ proxy["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/proxy.txt";
|
|
|
+ proxy["path"] = "./ruleset/proxy.yaml";
|
|
|
+ proxy["interval"] = 86400;
|
|
|
+
|
|
|
+
|
|
|
+ cncidr["type"] = "http";
|
|
|
+ cncidr["behavior"] = "domain";
|
|
|
+ cncidr["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/cncidr.txt";
|
|
|
+ cncidr["path"] = "./ruleset/cncidr.yaml";
|
|
|
+ cncidr["interval"] = 86400;
|
|
|
+
|
|
|
+ if (CApp::getSingletonPtr()->GetRouteMode() == ROUT_MODE::cn_mode)
|
|
|
+ {
|
|
|
+ root["rule-providers"]["reject"] = reject;
|
|
|
+ root["rule-providers"]["cncidr"] = cncidr;
|
|
|
+ root["rule-providers"]["proxy"] = proxy;
|
|
|
+ root["rule-providers"]["gfw"] = gfw;
|
|
|
+ root["rule-providers"]["google"] = google;
|
|
|
+ }
|
|
|
+
|
|
|
+ root["rules"] = m_rules;
|
|
|
+ auto name_file = "config.yaml";
|
|
|
+
|
|
|
+ if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode) {
|
|
|
+ name_file = "tun_config.yaml";
|
|
|
+ }
|
|
|
+
|
|
|
+ //std::ofstream ofstream(path + "\\config\\" + name_file);
|
|
|
+ //// ÉèÖÃÅäÖÃÎļþnodeÊý
|
|
|
+ //ofstream << root << std::endl;
|
|
|
+ //ofstream.close();
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
}
|
|
|
|
|
|
BOOL CLashConfig::InitClash()
|
|
@@ -34,22 +150,22 @@ BOOL CLashConfig::InitClash()
|
|
|
{
|
|
|
m_process = new CProcess();
|
|
|
}
|
|
|
-
|
|
|
+ std::string logfile = std::filesystem::current_path().string() + "\\log\\log.log";
|
|
|
+ if (m_log == nullptr)
|
|
|
+ {
|
|
|
+ m_log = new Logger(Logger::file_and_terminal, Logger::info, logfile);
|
|
|
+ }
|
|
|
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;
|
|
|
|
|
@@ -83,7 +199,8 @@ BOOL CLashConfig::InitClash()
|
|
|
|
|
|
BOOL CLashConfig::StartClash()
|
|
|
{
|
|
|
-
|
|
|
+ /*wil::unique_handle hEvent(CreateEventW(nullptr, FALSE, FALSE, nullptr));
|
|
|
+ THROW_LAST_ERROR_IF_NULL(hEvent);*/
|
|
|
//std::wstring config = path + L"\\" + WCLASHCONFIGDIR;
|
|
|
////args.push_back("-d");
|
|
|
//////args.push_back(std::filesystem::current_path().string() + "\\route");
|
|
@@ -93,19 +210,24 @@ BOOL CLashConfig::StartClash()
|
|
|
//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);
|
|
|
-
|
|
|
+ /* m_Asyntask.AddTask(&CLashConfig::ThreadFun_process_Config, this, (LPARAM)CProcessManager::getSingletonPtr());*/
|
|
|
|
|
|
if (CProcessManager::getSingletonPtr()->Start())
|
|
|
{
|
|
|
CApp::getSingletonPtr()->SetCLashRuning(true);
|
|
|
- }
|
|
|
+ m_AsyntaskProcessMonitor.AddTask(&CLashConfig::ThreadFun_ProcessMonitor_Config, this, (LPARAM)CProcessManager::getSingletonPtr());
|
|
|
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/*if (m_process)
|
|
|
{
|
|
|
m_process->Create(CProcess::ASYNC);
|
|
@@ -122,7 +244,8 @@ BOOL CLashConfig::StartClash()
|
|
|
BOOL CLashConfig::StopClash()
|
|
|
{
|
|
|
m_is_qut = true;
|
|
|
- CProcessManager::getSingletonPtr()->Stop();
|
|
|
+ /*SetEvent(_hEvent.get());*/
|
|
|
+
|
|
|
//char ch[MAX_PATH];
|
|
|
//memset(ch, 0, MAX_PATH);
|
|
|
//sprintf_s(ch, "ok\n");
|
|
@@ -147,72 +270,231 @@ YAML::Node CLashConfig::buildv2ray()
|
|
|
|
|
|
std::vector<YAML::Node> CLashConfig::buildv2rayHost()
|
|
|
{
|
|
|
- return std::vector<YAML::Node>();
|
|
|
+ std::vector<YAML::Node> p;
|
|
|
+ /*if (node) {
|
|
|
+ YAML::Node t_map;
|
|
|
+ t_map["host"] = node->v2_host;
|
|
|
+ p.push_back(t_map);
|
|
|
+ }*/
|
|
|
+ return p;
|
|
|
}
|
|
|
|
|
|
YAML::Node CLashConfig::builTunConfig()
|
|
|
{
|
|
|
- return YAML::Node();
|
|
|
+ std::vector<std::string> dns;
|
|
|
+ dns.push_back("198.18.0.2:53");
|
|
|
+ YAML::Node node;
|
|
|
+ node["enable"] = true;
|
|
|
+ node["stack"] = "gvisor";
|
|
|
+ node["dns-hijack"] = dns;
|
|
|
+ node["auto-route"] = true;
|
|
|
+ node["auto-detect-interface"] = true;
|
|
|
+ return node;
|
|
|
}
|
|
|
|
|
|
YAML::Node CLashConfig::buildDnsConfig()
|
|
|
{
|
|
|
- return YAML::Node();
|
|
|
+ YAML::Node dns;
|
|
|
+
|
|
|
+ std::vector<std::string> dns_arr;
|
|
|
+ dns_arr.push_back("114.114.114.114");
|
|
|
+ dns_arr.push_back("8.8.8.8");
|
|
|
+ dns_arr.push_back("tls://dns.rubyfish.cn:853");
|
|
|
+ dns_arr.push_back("https://1.1.1.1/dns-query");
|
|
|
+ dns["enable"] = true;
|
|
|
+ if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode)
|
|
|
+ {
|
|
|
+
|
|
|
+ dns["enhanced-mode"] = "redir-host";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //dns["enable"] = false;
|
|
|
+ }
|
|
|
+ dns["nameserver"] = dns_arr;
|
|
|
+ //dns["use-hosts"] = true;
|
|
|
+
|
|
|
+
|
|
|
+ return dns;
|
|
|
}
|
|
|
|
|
|
std::vector<YAML::Node> CLashConfig::buildProxyGroups()
|
|
|
{
|
|
|
- return std::vector<YAML::Node>();
|
|
|
+ std::vector<YAML::Node> dns_arr;
|
|
|
+ std::vector<std::string> proxy;
|
|
|
+ YAML::Node proxies;
|
|
|
+ /*for each (LISTNODE node in CList_node::getSingletonPtr()->listnode)
|
|
|
+ {
|
|
|
+ proxy.push_back(node.name.c_str());
|
|
|
+
|
|
|
+ }*/
|
|
|
+ YAML::Node li;
|
|
|
+ li["name"] = "Proxy";
|
|
|
+ li["type"] = "select";
|
|
|
+ li["proxies"] = proxy;
|
|
|
+ dns_arr.push_back(li);
|
|
|
+
|
|
|
+ return dns_arr;
|
|
|
}
|
|
|
|
|
|
std::vector<YAML::Node> CLashConfig::buildRules()
|
|
|
{
|
|
|
- return std::vector<YAML::Node>();
|
|
|
-}
|
|
|
+ std::vector<YAML::Node> p;
|
|
|
|
|
|
-void CLashConfig::ThreadFun_process_Config(LPARAM lParam)
|
|
|
-{
|
|
|
+ m_rules.clear();
|
|
|
|
|
|
- if (CProcessManager::getSingletonPtr()->Start())
|
|
|
+ if (CApp::getSingletonPtr()->GetRouteMode() == ROUT_MODE::cn_mode)
|
|
|
{
|
|
|
- CApp::getSingletonPtr()->SetCLashRuning(true);
|
|
|
+ m_rules.push_back("RULE-SET,proxy,Proxy");
|
|
|
+ m_rules.push_back("RULE-SET,google,Proxy");
|
|
|
+ m_rules.push_back("RULE-SET,gfw,Proxy");
|
|
|
+ m_rules.push_back("RULE-SET,cncidr,DIRECT");
|
|
|
+
|
|
|
+ m_rules.push_back("GEOIP,CN,DIRECT");
|
|
|
+ m_rules.push_back("IP-CIDR,127.0.0.0/8,DIRECT");
|
|
|
+ m_rules.push_back("IP-CIDR,172.16.0.0/12,DIRECT");
|
|
|
+ m_rules.push_back("IP-CIDR,192.168.0.0/16,DIRECT");
|
|
|
+ m_rules.push_back("IP-CIDR,10.0.0.0/8,DIRECT");
|
|
|
+ m_rules.push_back("IP-CIDR,100.64.0.0/10,DIRECT");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // m_rules.push_back("RULE-SET,proxy,Proxy");
|
|
|
+ // m_rules.push_back("RULE-SET,google,Proxy");
|
|
|
+ // m_rules.push_back("RULE-SET,gfw,Proxy");
|
|
|
+ // m_rules.push_back("RULE-SET,cncidr,Proxy");
|
|
|
+ m_rules.push_back("GEOIP,CN,Proxy");
|
|
|
+ m_rules.push_back("IP-CIDR,127.0.0.0/8,DIRECT");
|
|
|
+ m_rules.push_back("IP-CIDR,172.16.0.0/12,DIRECT");
|
|
|
+ m_rules.push_back("IP-CIDR,192.168.0.0/16,DIRECT");
|
|
|
+ m_rules.push_back("IP-CIDR,10.0.0.0/8,DIRECT");
|
|
|
+ m_rules.push_back("IP-CIDR,100.64.0.0/10,DIRECT");
|
|
|
+ }
|
|
|
|
|
|
- HANDLE hClashProcess = CProcessManager::getSingletonPtr()->GetClashProcessInfo().hProcess;
|
|
|
+ m_rules.push_back("MATCH,Proxy");
|
|
|
|
|
|
- for (size_t i = 0; i < 5; ++i) {
|
|
|
+ return p;
|
|
|
+}
|
|
|
|
|
|
- }
|
|
|
+std::vector<YAML::Node> CLashConfig::buildruleproviders()
|
|
|
+{
|
|
|
+ std::vector<YAML::Node> pp;
|
|
|
+ YAML::Node node;
|
|
|
+ YAML::Node reject;
|
|
|
+ YAML::Node google;
|
|
|
+ YAML::Node gfw;
|
|
|
+ YAML::Node proxy;
|
|
|
+ YAML::Node cncidr;
|
|
|
+ reject["type"] = "http";
|
|
|
+ reject["behavior"] = "domain";
|
|
|
+ reject["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt";
|
|
|
+ reject["path"] = "./ruleset/reject.yaml";
|
|
|
+ reject["interval"] = 86400;
|
|
|
+ node["reject"] = reject;
|
|
|
+ pp.push_back(node);
|
|
|
+
|
|
|
+ google["type"] = "http";
|
|
|
+ google["behavior"] = "domain";
|
|
|
+ google["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/google.txt";
|
|
|
+ google["path"] = "./ruleset/google.yaml";
|
|
|
+ google["interval"] = 86400;
|
|
|
+ node["google"] = google;
|
|
|
+ pp.push_back(node);
|
|
|
+
|
|
|
+
|
|
|
+ gfw["type"] = "http";
|
|
|
+ gfw["behavior"] = "domain";
|
|
|
+ gfw["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/gfw.txt";
|
|
|
+ gfw["path"] = "./ruleset/gfw.yaml";
|
|
|
+ gfw["interval"] = 86400;
|
|
|
+ node["gfw"] = gfw;
|
|
|
+ pp.push_back(node);
|
|
|
+
|
|
|
+
|
|
|
+ proxy["type"] = "http";
|
|
|
+ proxy["behavior"] = "domain";
|
|
|
+ proxy["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/proxy.txt";
|
|
|
+ proxy["path"] = "./ruleset/proxy.yaml";
|
|
|
+ proxy["interval"] = 86400;
|
|
|
+ node["proxy"] = proxy;
|
|
|
+ pp.push_back(node);
|
|
|
+
|
|
|
+ cncidr["type"] = "http";
|
|
|
+ cncidr["behavior"] = "domain";
|
|
|
+ cncidr["url"] = "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/cncidr.txt";
|
|
|
+ cncidr["path"] = "./ruleset/cncidr.yaml";
|
|
|
+ cncidr["interval"] = 86400;
|
|
|
+ node["cncidr"] = proxy;
|
|
|
+ pp.push_back(node);
|
|
|
+
|
|
|
+
|
|
|
+ return pp;
|
|
|
+}
|
|
|
+
|
|
|
+void CLashConfig::ThreadFun_process_Config(LPARAM lParam)
|
|
|
+{
|
|
|
+
|
|
|
+ CProcessManager* p = (CProcessManager*)lParam;
|
|
|
+
|
|
|
+ if (p->Start())
|
|
|
+ {
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ WaitForSingleObject(_hEvent.get(),INFINITE);
|
|
|
+}
|
|
|
|
|
|
- //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);
|
|
|
- //
|
|
|
- //}
|
|
|
+void CLashConfig::ThreadFun_ProcessMonitor_Config(LPARAM lParam)
|
|
|
+{
|
|
|
+ CProcessManager* p = (CProcessManager*)lParam;
|
|
|
+ if (p)
|
|
|
+ {
|
|
|
+ HANDLE hClashProcess = nullptr;
|
|
|
+ HANDLE hSubProcess = nullptr;
|
|
|
+
|
|
|
+ do
|
|
|
+ {
|
|
|
+ if (p->IsRunning() != State::Running)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ } while (hClashProcess = p->GetClashProcessInfo().hProcess);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
+//winrt::fire_and_forget CLashConfig::_StartClash()
|
|
|
+//{
|
|
|
+// if (CProcessManager::getSingletonPtr()->Start())
|
|
|
+// {
|
|
|
+// CApp::getSingletonPtr()->SetCLashRuning(true);
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+// /*HANDLE hClashProcess = CProcessManager::getSingletonPtr()->GetClashProcessInfo().hProcess;
|
|
|
+//
|
|
|
+// for (size_t i = 0; i < 5; ++i) {
|
|
|
+//
|
|
|
+// }*/
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+//}
|
|
|
+//
|
|
|
+//winrt::Windows::Foundation::IAsyncAction CLashConfig::ProcessMonitor()
|
|
|
+//{
|
|
|
+// HANDLE hSubProcess = CProcessManager::getSingletonPtr()->GetClashProcessInfo().hProcess,
|
|
|
+// hClashProcess = CProcessManager::getSingletonPtr()->GetClashProcessInfo().hProcess;
|
|
|
+//
|
|
|
+// winrt::resume_on_signal(hClashProcess);
|
|
|
+//
|
|
|
+// /*std::future<void> f = std::async(std::launch::async, []() {
|
|
|
+// })*/
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+//}
|