CConnectMage.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include "stdafx.h"
  2. #include "CConnectMage.h"
  3. CConnectMage::CConnectMage() : m_clash_Api(nullptr), m_clash_config(nullptr), m_connect_status(ConnectState::Stop), m_Asyntask(4)
  4. {
  5. SNotifyCenter::getSingleton().addEvent(EVENTID(EventConnect));
  6. m_isclash_config = false;
  7. }
  8. CConnectMage::~CConnectMage()
  9. {
  10. if (m_clash_Api)
  11. {
  12. delete m_clash_Api;
  13. m_clash_Api = nullptr;
  14. }
  15. }
  16. BOOL CConnectMage::setProxy()
  17. {
  18. BOOL isok = FALSE;
  19. if (m_clash_config)
  20. {
  21. isok = m_clash_config->SetProxy();
  22. }
  23. return isok;
  24. }
  25. void CConnectMage::Stop()
  26. {
  27. m_connect_status = ConnectState::Stop;
  28. m_Asyntask.AddTask(&CConnectMage::ThreadFun_ChangeClash_Stop, this, (LPARAM)nullptr);
  29. }
  30. void CConnectMage::ReqConfig()
  31. {
  32. m_Asyntask.AddTask(&CConnectMage::ThreadFun_ChangeClash_Config, this, (LPARAM)nullptr);
  33. }
  34. void CConnectMage::ChangeClashNode()
  35. {
  36. m_Asyntask.AddTask(&CConnectMage::ThreadFun_ChangeClash_Node, this, (LPARAM)nullptr);
  37. }
  38. void CConnectMage::ChangeClashProxyMode()
  39. {
  40. m_Asyntask.AddTask(&CConnectMage::ThreadFun_ChangeClash_ProxyMode, this, (LPARAM)nullptr);
  41. }
  42. void CConnectMage::ChangeClashInit()
  43. {
  44. }
  45. void CConnectMage::Init()
  46. {
  47. if (!m_clash_Api)
  48. {
  49. m_clash_Api = new ClashApi();
  50. m_clash_Api->SetProt(m_clash_config->GetClasApiPort());
  51. }
  52. }
  53. void CConnectMage::SetNodeName(std::string name)
  54. {
  55. m_name = name;
  56. }
  57. void CConnectMage::SetClashConfig(CLashConfig* m)
  58. {
  59. m_clash_config = m;
  60. }
  61. ConnectState CConnectMage::GetConnectStatus()
  62. {
  63. return m_connect_status;
  64. }
  65. void CConnectMage::ThreadFun_ChangeClash_Config(LPARAM lParam)
  66. {
  67. if (!CApp::getSingletonPtr()->GetClashRuning())
  68. {
  69. m_connect_status = ConnectState::Stop;
  70. m_error = L"内核没有启动,重启电脑或者软件";
  71. EventConnect* pEvt = new EventConnect(nullptr);
  72. pEvt->status = m_connect_status;
  73. pEvt->msg = m_error;
  74. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  75. pEvt->Release();
  76. return;
  77. }
  78. if (m_clash_config == nullptr || m_clash_Api == nullptr)
  79. {
  80. m_connect_status = ConnectState::Stop;
  81. m_error = L"错误,重启电脑";
  82. EventConnect* pEvt = new EventConnect(nullptr);
  83. pEvt->status = m_connect_status;
  84. pEvt->msg = m_error;
  85. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  86. pEvt->Release();
  87. return;
  88. }
  89. //创建配置
  90. if (!m_clash_config->MakeClash())
  91. {
  92. m_connect_status = ConnectState::Stop;
  93. m_error = L"没有生成配置,检查系统是否有管理员";
  94. EventConnect* pEvt = new EventConnect(nullptr);
  95. pEvt->status = m_connect_status;
  96. pEvt->msg = m_error;
  97. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  98. pEvt->Release();
  99. return;
  100. }
  101. /*std::filesystem::path config;
  102. if (m_connect_status == ConnectState::Stop && CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode)
  103. {
  104. config = CApp::getSingletonPtr()->GetConfigPath() / DSPROXY_CONFIG_INIT_ClASH_NAME;
  105. }*/
  106. //切换请求
  107. if (m_clash_Api->RequestConfigUpdate(m_clash_config->GetRunConfig()))
  108. {
  109. m_isclash_config = true;
  110. m_connect_status = ConnectState::SwitchNodeSuccEss;
  111. m_error = L"加载配置成功";
  112. //if (m_name.empty())
  113. //{
  114. // m_connect_status = ConnectState::SwitchNodeSuccEss;
  115. // m_error = L"加载配置成功";
  116. // //m_clash_config->SetProxy();
  117. // //CServerList* serverlist = CApp::getSingletonPtr()->GetServerList();
  118. // //if (serverlist)
  119. // //{
  120. // // serverlist->MinimumSelectNode();
  121. // // if (!serverlist->node_name.empty())
  122. // // {
  123. // // if (m_clash_Api->UpdateProxyGroup("Proxy", serverlist->node_name))
  124. // // {
  125. // // m_connect_status = ConnectState::SwitchNodeSuccEss;
  126. // // m_error = L"切换成功";
  127. // // //m_clash_config->SetProxy();
  128. // // }
  129. // // else {
  130. // // m_connect_status = ConnectState::Stop;
  131. // // m_error = L"切换失败,可以重新尝试";
  132. // // }
  133. // // }
  134. // //}
  135. //}
  136. //else {
  137. // //if (CApp::getSingletonPtr()->GetRouteMode() == ROUT_MODE::cn_mode)
  138. // //{
  139. // // if (m_clash_Api->UpdateProxyGroup("Rule", m_name))
  140. // // {
  141. // // m_connect_status = ConnectState::SwitchNodeSuccEss;
  142. // // m_error = L"切换节点成功";
  143. // // //m_clash_config->SetProxy();
  144. // // }
  145. // // else {
  146. // // m_connect_status = ConnectState::Stop;
  147. // // m_error = L"切换节点失败,可以重新尝试";
  148. // // }
  149. // //}
  150. // //else {
  151. // // if (m_clash_Api->UpdateProxyGroup("Global", m_name))
  152. // // {
  153. // // m_connect_status = ConnectState::SwitchNodeSuccEss;
  154. // // m_error = L"切换节点成功";
  155. // // //m_clash_config->SetProxy();
  156. // // }
  157. // // else {
  158. // // m_connect_status = ConnectState::Stop;
  159. // // m_error = L"切换节点失败,可以重新尝试";
  160. // // }
  161. // //}
  162. // /*if (m_clash_Api->UpdateProxyGroup("Proxy", m_name))
  163. // {
  164. // m_connect_status = ConnectState::SwitchNodeSuccEss;
  165. // m_error = L"切换成功";
  166. // m_clash_config->SetProxy();
  167. // }
  168. // else {
  169. // m_connect_status = ConnectState::Stop;
  170. // m_error = L"切换失败,可以重新尝试";
  171. // }*/
  172. // m_connect_status = ConnectState::SwitchNodeSuccEss;
  173. // m_error = L"加载配置失败";
  174. //}
  175. }
  176. else {
  177. m_connect_status = ConnectState::Stop;
  178. m_error = L"切换失败,可以重新尝试";
  179. }
  180. EventConnect* pEvt = new EventConnect(nullptr);
  181. pEvt->status = m_connect_status;
  182. pEvt->msg = m_error;
  183. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  184. pEvt->Release();
  185. }
  186. void CConnectMage::ThreadFun_ChangeClash_ProxyMode(LPARAM lParam)
  187. {
  188. if (m_clash_config == nullptr || m_clash_Api == nullptr)
  189. {
  190. m_connect_status = ConnectState::Stop;
  191. m_error = L"错误,重启电脑";
  192. EventConnect* pEvt = new EventConnect(nullptr);
  193. pEvt->status = m_connect_status;
  194. pEvt->msg = m_error;
  195. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  196. pEvt->Release();
  197. return;
  198. }
  199. ClashProxyMode modes;
  200. if (CApp::getSingletonPtr()->GetRouteMode() == ROUT_MODE::cn_mode)
  201. {
  202. modes = ClashProxyMode::Rule;
  203. }
  204. else {
  205. modes = ClashProxyMode::Global;
  206. }
  207. if (m_clash_Api->UpdateProxyMode(modes))
  208. {
  209. m_connect_status = ConnectState::SwitchNodeSuccEss;
  210. if (CApp::getSingletonPtr()->GetRouteMode() == ROUT_MODE::cn_mode)
  211. {
  212. if (m_clash_Api->UpdateProxyGroup("Proxy", m_name))
  213. {
  214. m_connect_status = ConnectState::SwitchNodeSuccEss;
  215. m_error = L"切换成功..模式成功";
  216. //m_clash_config->SetProxy();
  217. }
  218. else {
  219. m_connect_status = ConnectState::Stop;
  220. m_error = L"切换节点失败,可以重新尝试";
  221. }
  222. }
  223. else {
  224. if (m_clash_Api->UpdateProxyGroup("GLOBAL", m_name))
  225. {
  226. m_connect_status = ConnectState::SwitchNodeSuccEss;
  227. m_error = L"切换成功..模式成功";
  228. //m_clash_config->SetProxy();
  229. }
  230. else {
  231. m_connect_status = ConnectState::Stop;
  232. m_error = L"切换节点失败,可以重新尝试";
  233. }
  234. }
  235. //m_clash_config->SetProxy();
  236. //ClashConfig clash_config = m_clash_Api->GetConfig();
  237. //m_error = L"获取配置";
  238. }
  239. else {
  240. m_connect_status = ConnectState::Stop;
  241. m_error = L"切换成功..模式失败";
  242. }
  243. EventConnect* pEvt = new EventConnect(nullptr);
  244. pEvt->status = m_connect_status;
  245. pEvt->msg = m_error;
  246. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  247. pEvt->Release();
  248. }
  249. void CConnectMage::ThreadFun_ChangeClash_Node(LPARAM lParam)
  250. {
  251. if (m_clash_config == nullptr || m_clash_Api == nullptr)
  252. {
  253. m_connect_status = ConnectState::Stop;
  254. m_error = L"错误,重启电脑";
  255. EventConnect* pEvt = new EventConnect(nullptr);
  256. pEvt->status = m_connect_status;
  257. pEvt->msg = m_error;
  258. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  259. pEvt->Release();
  260. return;
  261. }
  262. if (m_clash_Api->UpdateProxyGroup("Proxy", m_name))
  263. {
  264. m_connect_status = ConnectState::SwitchNodeSuccEss;
  265. m_error = L"切换成功..点击断开";
  266. //m_clash_config->SetProxy();
  267. }
  268. else {
  269. m_connect_status = ConnectState::Stop;
  270. m_error = L"切换失败,可以重新尝试";
  271. }
  272. EventConnect* pEvt = new EventConnect(nullptr);
  273. pEvt->status = m_connect_status;
  274. pEvt->msg = m_error;
  275. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  276. pEvt->Release();
  277. }
  278. void CConnectMage::ThreadFun_ChangeClash_Stop(LPARAM lParam)
  279. {
  280. if (!CApp::getSingletonPtr()->GetClashRuning())
  281. {
  282. m_connect_status = ConnectState::Stop;
  283. m_error = L"内核没有启动,重启电脑或者软件";
  284. EventConnect* pEvt = new EventConnect(nullptr);
  285. pEvt->status = m_connect_status;
  286. pEvt->msg = m_error;
  287. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  288. pEvt->Release();
  289. return;
  290. }
  291. if (m_clash_config == nullptr || m_clash_Api == nullptr)
  292. {
  293. m_connect_status = ConnectState::Stop;
  294. m_error = L"错误,重启电脑";
  295. EventConnect* pEvt = new EventConnect(nullptr);
  296. pEvt->status = m_connect_status;
  297. pEvt->msg = m_error;
  298. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  299. pEvt->Release();
  300. return;
  301. }
  302. //创建配置
  303. if (!m_clash_config->MakeClash())
  304. {
  305. m_connect_status = ConnectState::Stop;
  306. m_error = L"没有生成配置,检查系统是否有管理员";
  307. EventConnect* pEvt = new EventConnect(nullptr);
  308. pEvt->status = m_connect_status;
  309. pEvt->msg = m_error;
  310. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  311. pEvt->Release();
  312. return;
  313. }
  314. /*std::filesystem::path config;
  315. if (m_connect_status == ConnectState::Stop && CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode)
  316. {
  317. config = CApp::getSingletonPtr()->GetConfigPath() / DSPROXY_CONFIG_INIT_ClASH_NAME;
  318. }*/
  319. //切换请求
  320. if (m_clash_Api->RequestConfigUpdate(CApp::getSingletonPtr()->GetConfigPath() / DSPROXY_CONFIG_INIT_ClASH_NAME))
  321. {
  322. m_isclash_config = true;
  323. if (m_name.empty())
  324. {
  325. CServerList* serverlist = CApp::getSingletonPtr()->GetServerList();
  326. if (serverlist)
  327. {
  328. serverlist->MinimumSelectNode();
  329. if (!serverlist->node_name.empty())
  330. {
  331. if (m_clash_Api->UpdateProxyGroup("Proxy", serverlist->node_name))
  332. {
  333. m_connect_status = ConnectState::SwitchNodeSuccEss;
  334. m_error = L"切换成功";
  335. //m_clash_config->SetProxy();
  336. }
  337. else {
  338. m_connect_status = ConnectState::Stop;
  339. m_error = L"切换失败,可以重新尝试";
  340. }
  341. }
  342. }
  343. }
  344. else {
  345. if (m_clash_Api->UpdateProxyGroup("Proxy", m_name))
  346. {
  347. m_connect_status = ConnectState::SwitchNodeSuccEss;
  348. m_error = L"切换成功";
  349. //m_clash_config->SetProxy();
  350. }
  351. else {
  352. m_connect_status = ConnectState::Stop;
  353. m_error = L"切换失败,可以重新尝试";
  354. }
  355. }
  356. }
  357. else {
  358. m_connect_status = ConnectState::Stop;
  359. m_error = L"切换失败,可以重新尝试";
  360. }
  361. }