CManageNetWork.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. #include "stdafx.h"
  2. #include "CManageNetWork.h"
  3. #include "CNetWork.h"
  4. #include "CApp.h"
  5. #include "FileOperate.h"
  6. #include <nlohmann/json.hpp>
  7. CManageNetWork* SSingleton<CManageNetWork>::ms_Singleton = NULL;
  8. CManageNetWork::CManageNetWork() : m_base_curl(nullptr), m_Asyntask(4)
  9. {
  10. }
  11. CManageNetWork::~CManageNetWork()
  12. {
  13. if (m_base_curl)
  14. {
  15. delete m_base_curl;
  16. m_base_curl = nullptr;
  17. }
  18. }
  19. void CManageNetWork::init()
  20. {
  21. SNotifyCenter::getSingleton().addEvent(EVENTID(EventLogin));
  22. SNotifyCenter::getSingleton().addEvent(EVENTID(EventDoWNload));
  23. SNotifyCenter::getSingleton().addEvent(EVENTID(EventNodeList));
  24. SNotifyCenter::getSingleton().addEvent(EVENTID(EventVerions));
  25. SNotifyCenter::getSingleton().addEvent(EVENTID(EventSysconfig));
  26. SNotifyCenter::getSingleton().addEvent(EVENTID(EventAUTH));
  27. if (!m_base_curl)
  28. {
  29. m_base_curl = new CNetWork();
  30. //m_base_curl.Attach(new CNetWork());
  31. }
  32. LoadFileToData();
  33. }
  34. void CManageNetWork::Login(LPCSTR username, LPCSTR password)
  35. {
  36. if (!m_base_curl)
  37. {
  38. return;
  39. }
  40. this->m_username = username;
  41. this->m_password = password;
  42. m_Asyntask.AddTask(&CManageNetWork::ThreadFun_login, this, (LPARAM)m_base_curl);
  43. }
  44. void CManageNetWork::SetUrl(LPCSTR url)
  45. {
  46. if (!m_base_curl)
  47. {
  48. return;
  49. }
  50. m_base_curl->SetUrl(url);
  51. }
  52. SStringA CManageNetWork::GetUsername()
  53. {
  54. return m_username;
  55. }
  56. SStringA CManageNetWork::GetPassWord()
  57. {
  58. return m_password;
  59. }
  60. void CManageNetWork::DonloadConfg()
  61. {
  62. if (!m_base_curl)
  63. {
  64. return;
  65. }
  66. m_Asyntask.AddTask(&CManageNetWork::ThreadFun_Dowlon_Config, this, (LPARAM)m_base_curl);
  67. }
  68. void CManageNetWork::GetNodeList()
  69. {
  70. if (!m_base_curl)
  71. {
  72. return;
  73. }
  74. m_Asyntask.AddTask(&CManageNetWork::ThreadFun_Node_Config, this, (LPARAM)m_base_curl);
  75. }
  76. void CManageNetWork::GetVersion()
  77. {
  78. if (!m_base_curl)
  79. {
  80. return;
  81. }
  82. m_Asyntask.AddTask(&CManageNetWork::ThreadFun_Version_Config, this, (LPARAM)m_base_curl);
  83. }
  84. void CManageNetWork::GetSysConfig()
  85. {
  86. if (!m_base_curl)
  87. {
  88. return;
  89. }
  90. m_Asyntask.AddTask(&CManageNetWork::ThreadFun_Sys_Config, this, (LPARAM)m_base_curl);
  91. }
  92. void CManageNetWork::Auth()
  93. {
  94. if (!m_base_curl)
  95. {
  96. return;
  97. }
  98. m_Asyntask.AddTask(&CManageNetWork::ThreadFun_Auth, this, (LPARAM)m_base_curl);
  99. }
  100. void CManageNetWork::Reg(LPCSTR username, LPCSTR password)
  101. {
  102. if (!m_base_curl)
  103. {
  104. return;
  105. }
  106. this->m_username = username;
  107. this->m_password = password;
  108. m_Asyntask.AddTask(&CManageNetWork::ThreadFun_Reg, this, (LPARAM)m_base_curl);
  109. }
  110. void CManageNetWork::SetUrlList(std::vector<ServerListUrl> data)
  111. {
  112. if (!m_base_curl)
  113. {
  114. return;
  115. }
  116. m_base_curl->SetUrlArray(data);
  117. }
  118. void CManageNetWork::ThreadFun_login(LPARAM lParam)
  119. {
  120. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  121. int code = 200;
  122. SStringW msg = L"";
  123. std::string data;
  124. HTTPRET httpstatus = m_base_curl->GetSysConfig(data);
  125. if (httpstatus == HTTPRET::http_user_expired_at)
  126. {
  127. code = -2;
  128. msg = m_base_curl->GetLastErrorW();
  129. }
  130. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  131. code = -3;
  132. msg = m_base_curl->GetLastErrorW();
  133. }
  134. else if (httpstatus == HTTPRET::http_f) {
  135. code = 0;
  136. msg = m_base_curl->GetLastErrorW();
  137. }
  138. if (!data.empty())
  139. {
  140. if (!CApp::getSingletonPtr()->GetSysconfig()->Inti(data)) {
  141. EventLogin* pEvt = new EventLogin(nullptr);
  142. pEvt->status = code;
  143. pEvt->msg = CApp::getSingletonPtr()->GetSysconfig()->GetLastErrorW();
  144. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  145. pEvt->Release();
  146. return;
  147. }
  148. }
  149. data.clear();
  150. httpstatus = m_base_curl->PostLogin(this->m_username, this->m_password, data);
  151. if (httpstatus == HTTPRET::http_user_expired_at)
  152. {
  153. code = -2;
  154. msg = m_base_curl->GetLastErrorW();
  155. }
  156. else if(httpstatus == HTTPRET::http_user_transfer_enable){
  157. code = -3;
  158. msg = m_base_curl->GetLastErrorW();
  159. }
  160. else if (httpstatus == HTTPRET::http_f) {
  161. code = 0;
  162. msg = m_base_curl->GetLastErrorW();
  163. }
  164. if (!data.empty())
  165. {
  166. if (!CApp::getSingletonPtr()->GetLoginInfo()->Init(data))
  167. {
  168. code = -1;
  169. msg = CApp::getSingletonPtr()->GetLoginInfo()->GetLastErrorW();
  170. }
  171. }
  172. data.clear();
  173. if (!msg.IsEmpty())
  174. {
  175. DataToFile();
  176. EventLogin* pEvt = new EventLogin(nullptr);
  177. pEvt->status = code;
  178. pEvt->msg = msg;
  179. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  180. pEvt->Release();
  181. return;
  182. }
  183. httpstatus = m_base_curl->GetMySub(data);
  184. if (httpstatus == HTTPRET::http_f) {
  185. code = 0;
  186. msg = m_base_curl->GetLastErrorW();
  187. }
  188. if (!data.empty())
  189. {
  190. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  191. {
  192. code = -1;
  193. msg = CApp::getSingletonPtr()->GetUserinfo()->GetLastErrorW();
  194. }
  195. }
  196. data.clear();
  197. httpstatus = m_base_curl->GetloginByUrl("",data);
  198. if (httpstatus == HTTPRET::http_f) {
  199. code = 0;
  200. msg = m_base_curl->GetLastErrorW();
  201. }
  202. if (!data.empty())
  203. {
  204. if (!CApp::getSingletonPtr()->GetSysconfig()->IntiLogin(data))
  205. {
  206. code = -1;
  207. msg = CApp::getSingletonPtr()->GetUserinfo()->GetLastErrorW();
  208. }
  209. }
  210. DataToFile();
  211. EventLogin* pEvt = new EventLogin(nullptr);
  212. pEvt->status = code;
  213. pEvt->msg = msg;
  214. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  215. pEvt->Release();
  216. }
  217. void CManageNetWork::ThreadFun_Dowlon_Config(LPARAM lParam)
  218. {
  219. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  220. int code = 200;
  221. SStringW msg = L"";
  222. std::string data;
  223. if (!m_base_curl->Download(CApp::getSingletonPtr()->GetUserinfo()->clash_config)) {
  224. code = 0;
  225. msg = L"下载文件失败";
  226. }
  227. EventDoWNload* pEvt = new EventDoWNload(nullptr);
  228. pEvt->status = code;
  229. pEvt->msg = msg;
  230. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  231. pEvt->Release();
  232. }
  233. void CManageNetWork::ThreadFun_Sys_Config(LPARAM lParam)
  234. {
  235. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  236. int code = 200;
  237. SStringW msg = L"";
  238. std::string data;
  239. //if (m_base_curl->Version(data) == HTTPRET::http_f) {
  240. // code = 0;
  241. // msg = m_base_curl->GetLastErrorW();
  242. //}
  243. HTTPRET httpstatus = m_base_curl->GetSysConfig(data);
  244. if (httpstatus == HTTPRET::http_user_expired_at)
  245. {
  246. code = -2;
  247. msg = m_base_curl->GetLastErrorW();
  248. }
  249. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  250. code = -3;
  251. msg = m_base_curl->GetLastErrorW();
  252. }
  253. else if (httpstatus == HTTPRET::http_f) {
  254. code = 0;
  255. msg = m_base_curl->GetLastErrorW();
  256. }
  257. if (!data.empty())
  258. {
  259. if (!CApp::getSingletonPtr()->GetSysconfig()->Inti(data))
  260. {
  261. code = -1;
  262. msg = CApp::getSingletonPtr()->GetSysconfig()->GetLastErrorW();
  263. }
  264. }
  265. EventSysconfig* pEvt = new EventSysconfig(nullptr);
  266. pEvt->status = code;
  267. pEvt->msg = msg;
  268. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  269. pEvt->Release();
  270. }
  271. void CManageNetWork::ThreadFun_Version_Config(LPARAM lParam)
  272. {
  273. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  274. int code = 200;
  275. SStringW msg = L"";
  276. std::string data;
  277. if (m_base_curl->Version(data) == HTTPRET::http_f) {
  278. code = 0;
  279. msg = m_base_curl->GetLastErrorW();
  280. }
  281. if (!data.empty())
  282. {
  283. if (!CApp::getSingletonPtr()->GetVerinfo()->Inti(data))
  284. {
  285. code = -1;
  286. msg = CApp::getSingletonPtr()->GetVerinfo()->GetLastErrorW();
  287. }
  288. }
  289. EventVerions* pEvt = new EventVerions(nullptr);
  290. pEvt->status = code;
  291. pEvt->msg = msg;
  292. pEvt->versionupdate = CApp::getSingletonPtr()->GetVerinfo()->versionupdate;
  293. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  294. pEvt->Release();
  295. }
  296. void CManageNetWork::ThreadFun_Node_Config(LPARAM lParam)
  297. {
  298. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  299. int code = 200;
  300. SStringW msg = L"";
  301. std::string data;
  302. int count = 0;
  303. do
  304. {
  305. data = "";
  306. HTTPRET httpstatus = m_base_curl->GetServerNode(data);
  307. if (httpstatus == HTTPRET::http_f) {
  308. code = 0;
  309. msg = m_base_curl->GetLastErrorW();
  310. }
  311. if (m_base_curl->GetHttpStatus() == 401)
  312. {
  313. if (count >= 3)
  314. {
  315. code = m_base_curl->GetHttpStatus();
  316. msg = L"鉴权失败,需要重新登录";
  317. break;
  318. }
  319. httpstatus = m_base_curl->PostLogin(this->m_username, this->m_password, data);
  320. if (httpstatus == HTTPRET::http_f) {
  321. code = m_base_curl->GetHttpStatus();
  322. msg = m_base_curl->GetLastErrorW();
  323. count++;
  324. }
  325. else {
  326. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  327. {
  328. code = -1;
  329. msg = CApp::getSingletonPtr()->GetServerList()->GetLastErrorW();
  330. }
  331. continue;
  332. }
  333. }
  334. else {
  335. if (!data.empty())
  336. {
  337. if (!CApp::getSingletonPtr()->GetServerList()->Init(data))
  338. {
  339. code = -1;
  340. msg = CApp::getSingletonPtr()->GetServerList()->GetLastErrorW();
  341. }
  342. break;
  343. }
  344. }
  345. } while (m_base_curl);
  346. EventNodeList* pEvt = new EventNodeList(nullptr);
  347. pEvt->status = code;
  348. pEvt->msg = msg;
  349. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  350. pEvt->Release();
  351. }
  352. void CManageNetWork::ThreadFun_Auth(LPARAM lParam)
  353. {
  354. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  355. int code = 200;
  356. SStringW msg = L"";
  357. std::string data;
  358. int count = 0;
  359. do
  360. {
  361. HTTPRET http_status = m_base_curl->Auth(data);
  362. if (http_status == HTTPRET::http_f) {
  363. code = 0;
  364. msg = m_base_curl->GetLastErrorW();
  365. break;
  366. }
  367. auto status = CApp::getSingletonPtr()->GetUserinfo()->authInit(data);
  368. if (status != -1) {
  369. code = status;
  370. msg = CApp::getSingletonPtr()->GetUserinfo()->GetLastErrorW();
  371. break;
  372. }
  373. /*else if (http_status == HTTPRET::http_user_expired_at) {
  374. }
  375. else if (http_status == HTTPRET::http_user_transfer_enable)
  376. {
  377. code = 0;
  378. msg = m_base_curl->GetLastErrorW();
  379. break;
  380. }*/
  381. //重新鉴权
  382. if (m_base_curl->GetHttpStatus() == 401)
  383. {
  384. if (count >= 3)
  385. {
  386. code = m_base_curl->GetHttpStatus();
  387. msg = L"鉴权失败,需要重新登录";
  388. break;
  389. }
  390. http_status = m_base_curl->PostLogin(this->m_username, this->m_password, data);
  391. if (http_status == HTTPRET::http_f) {
  392. code = m_base_curl->GetHttpStatus();
  393. msg = m_base_curl->GetLastErrorW();
  394. count++;
  395. }
  396. else {
  397. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  398. {
  399. code = -1;
  400. msg = CApp::getSingletonPtr()->GetServerList()->GetLastErrorW();
  401. }
  402. continue;
  403. }
  404. }
  405. else {
  406. code == 200;
  407. break;
  408. }
  409. } while (m_base_curl);
  410. EventAUTH* pEvt = new EventAUTH(nullptr);
  411. pEvt->status = code;
  412. pEvt->msg = msg;
  413. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  414. pEvt->Release();
  415. }
  416. void CManageNetWork::ThreadFun_Reg(LPARAM lParam)
  417. {
  418. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  419. int code = 200;
  420. SStringW msg = L"";
  421. std::string data;
  422. HTTPRET httpstatus = m_base_curl->GetSysConfigFromUser(this->m_username, this->m_password, data);
  423. if (httpstatus == HTTPRET::http_user_expired_at)
  424. {
  425. code = -2;
  426. msg = m_base_curl->GetLastErrorW();
  427. }
  428. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  429. code = -3;
  430. msg = m_base_curl->GetLastErrorW();
  431. }
  432. else if (httpstatus == HTTPRET::http_f) {
  433. code = 0;
  434. msg = m_base_curl->GetLastErrorW();
  435. }
  436. if (!data.empty())
  437. {
  438. if (!CApp::getSingletonPtr()->GetSysconfig()->Inti(data)) {
  439. }
  440. }
  441. data.empty();
  442. httpstatus = m_base_curl->PostReg(this->m_username, this->m_password, data);
  443. if (httpstatus == HTTPRET::http_user_expired_at)
  444. {
  445. code = -2;
  446. msg = m_base_curl->GetLastErrorW();
  447. }
  448. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  449. code = -3;
  450. msg = m_base_curl->GetLastErrorW();
  451. }
  452. else if (httpstatus == HTTPRET::http_f) {
  453. code = 0;
  454. msg = m_base_curl->GetLastErrorW();
  455. }
  456. if (!data.empty())
  457. {
  458. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  459. {
  460. code = -1;
  461. msg = CApp::getSingletonPtr()->GetUserinfo()->GetLastErrorW();
  462. }
  463. }
  464. DataToFile();
  465. EventLogin* pEvt = new EventLogin(nullptr);
  466. pEvt->status = code;
  467. pEvt->msg = msg;
  468. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  469. pEvt->Release();
  470. }
  471. void CManageNetWork::LoadFileToData() {
  472. try
  473. {
  474. FileOperate f;
  475. SStringA paht;
  476. paht.Format("%s\\%s", CApp::getSingletonPtr()->GetDataPath().string().c_str(), S_CW2A(DSPROXY_CONFIG_NAME, CP_UTF8));
  477. if (f.open(paht.GetBuffer(0), std::fstream::in | std::fstream::out | std::fstream::app)) {
  478. std::string data = f.readAll();
  479. if (data.empty())
  480. {
  481. return;
  482. }
  483. nlohmann::json j3 = nlohmann::json::parse(data.begin(), data.end());
  484. this->m_username = j3["username"].get<std::string>().c_str();
  485. this->m_password = j3["password"].get<std::string>().c_str();
  486. }
  487. f.close();
  488. }
  489. CATCH_LOG();
  490. }
  491. void CManageNetWork::DataToFile()
  492. {
  493. try
  494. {
  495. nlohmann::json j3;
  496. j3["username"] = this->m_username;
  497. j3["password"] = this->m_password;
  498. auto str = j3.dump(4);
  499. FileOperate f;
  500. SStringA paht;
  501. paht.Format("%s\\%s", CApp::getSingletonPtr()->GetDataPath().string().c_str(), S_CW2A(DSPROXY_CONFIG_NAME, CP_UTF8));
  502. f.create(paht.GetBuffer(0));
  503. if (f.open(paht.GetBuffer(0), std::fstream::in | std::fstream::out | std::fstream::app)) {
  504. f.write(str);
  505. }
  506. f.close();
  507. }
  508. CATCH_LOG();
  509. }