CManageNetWork.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. DataToFile();
  197. EventLogin* pEvt = new EventLogin(nullptr);
  198. pEvt->status = code;
  199. pEvt->msg = msg;
  200. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  201. pEvt->Release();
  202. }
  203. void CManageNetWork::ThreadFun_Dowlon_Config(LPARAM lParam)
  204. {
  205. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  206. int code = 200;
  207. SStringW msg = L"";
  208. std::string data;
  209. if (!m_base_curl->Download(CApp::getSingletonPtr()->GetUserinfo()->clash_config)) {
  210. code = 0;
  211. msg = L"下载文件失败";
  212. }
  213. EventDoWNload* pEvt = new EventDoWNload(nullptr);
  214. pEvt->status = code;
  215. pEvt->msg = msg;
  216. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  217. pEvt->Release();
  218. }
  219. void CManageNetWork::ThreadFun_Sys_Config(LPARAM lParam)
  220. {
  221. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  222. int code = 200;
  223. SStringW msg = L"";
  224. std::string data;
  225. //if (m_base_curl->Version(data) == HTTPRET::http_f) {
  226. // code = 0;
  227. // msg = m_base_curl->GetLastErrorW();
  228. //}
  229. HTTPRET httpstatus = m_base_curl->GetSysConfig(data);
  230. if (httpstatus == HTTPRET::http_user_expired_at)
  231. {
  232. code = -2;
  233. msg = m_base_curl->GetLastErrorW();
  234. }
  235. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  236. code = -3;
  237. msg = m_base_curl->GetLastErrorW();
  238. }
  239. else if (httpstatus == HTTPRET::http_f) {
  240. code = 0;
  241. msg = m_base_curl->GetLastErrorW();
  242. }
  243. if (!data.empty())
  244. {
  245. if (!CApp::getSingletonPtr()->GetSysconfig()->Inti(data))
  246. {
  247. code = -1;
  248. msg = CApp::getSingletonPtr()->GetSysconfig()->GetLastErrorW();
  249. }
  250. }
  251. EventSysconfig* pEvt = new EventSysconfig(nullptr);
  252. pEvt->status = code;
  253. pEvt->msg = msg;
  254. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  255. pEvt->Release();
  256. }
  257. void CManageNetWork::ThreadFun_Version_Config(LPARAM lParam)
  258. {
  259. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  260. int code = 200;
  261. SStringW msg = L"有新版本,点我升级";
  262. std::string data;
  263. if (m_base_curl->Version(data) == HTTPRET::http_f) {
  264. code = 0;
  265. msg = m_base_curl->GetLastErrorW();
  266. }
  267. if (!data.empty())
  268. {
  269. if (!CApp::getSingletonPtr()->GetVerinfo()->Inti(data))
  270. {
  271. code = -1;
  272. msg = CApp::getSingletonPtr()->GetVerinfo()->GetLastErrorW();
  273. }
  274. }
  275. EventVerions* pEvt = new EventVerions(nullptr);
  276. pEvt->status = code;
  277. pEvt->msg = msg;
  278. pEvt->versionupdate = CApp::getSingletonPtr()->GetVerinfo()->versionupdate;
  279. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  280. pEvt->Release();
  281. }
  282. void CManageNetWork::ThreadFun_Node_Config(LPARAM lParam)
  283. {
  284. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  285. int code = 200;
  286. SStringW msg = L"";
  287. std::string data;
  288. int count = 0;
  289. do
  290. {
  291. data = "";
  292. HTTPRET httpstatus = m_base_curl->GetServerNode(data);
  293. if (httpstatus == HTTPRET::http_f) {
  294. code = 0;
  295. msg = m_base_curl->GetLastErrorW();
  296. }
  297. if (m_base_curl->GetHttpStatus() == 401)
  298. {
  299. if (count >= 3)
  300. {
  301. code = m_base_curl->GetHttpStatus();
  302. msg = L"鉴权失败,需要重新登录";
  303. break;
  304. }
  305. httpstatus = m_base_curl->PostLogin(this->m_username, this->m_password, data);
  306. if (httpstatus == HTTPRET::http_f) {
  307. code = m_base_curl->GetHttpStatus();
  308. msg = m_base_curl->GetLastErrorW();
  309. count++;
  310. }
  311. else {
  312. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  313. {
  314. code = -1;
  315. msg = CApp::getSingletonPtr()->GetServerList()->GetLastErrorW();
  316. }
  317. continue;
  318. }
  319. }
  320. else {
  321. if (!data.empty())
  322. {
  323. if (!CApp::getSingletonPtr()->GetServerList()->Init(data))
  324. {
  325. code = -1;
  326. msg = CApp::getSingletonPtr()->GetServerList()->GetLastErrorW();
  327. }
  328. break;
  329. }
  330. }
  331. } while (m_base_curl);
  332. EventNodeList* pEvt = new EventNodeList(nullptr);
  333. pEvt->status = code;
  334. pEvt->msg = msg;
  335. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  336. pEvt->Release();
  337. }
  338. void CManageNetWork::ThreadFun_Auth(LPARAM lParam)
  339. {
  340. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  341. int code = 200;
  342. SStringW msg = L"";
  343. std::string data;
  344. int count = 0;
  345. do
  346. {
  347. HTTPRET http_status = m_base_curl->Auth(data);
  348. if (http_status == HTTPRET::http_f) {
  349. code = 0;
  350. msg = m_base_curl->GetLastErrorW();
  351. break;
  352. }
  353. auto status = CApp::getSingletonPtr()->GetUserinfo()->authInit(data);
  354. if (status != -1) {
  355. code = status;
  356. msg = CApp::getSingletonPtr()->GetUserinfo()->GetLastErrorW();
  357. break;
  358. }
  359. /*else if (http_status == HTTPRET::http_user_expired_at) {
  360. }
  361. else if (http_status == HTTPRET::http_user_transfer_enable)
  362. {
  363. code = 0;
  364. msg = m_base_curl->GetLastErrorW();
  365. break;
  366. }*/
  367. //重新鉴权
  368. if (m_base_curl->GetHttpStatus() == 401)
  369. {
  370. if (count >= 3)
  371. {
  372. code = m_base_curl->GetHttpStatus();
  373. msg = L"鉴权失败,需要重新登录";
  374. break;
  375. }
  376. http_status = m_base_curl->PostLogin(this->m_username, this->m_password, data);
  377. if (http_status == HTTPRET::http_f) {
  378. code = m_base_curl->GetHttpStatus();
  379. msg = m_base_curl->GetLastErrorW();
  380. count++;
  381. }
  382. else {
  383. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  384. {
  385. code = -1;
  386. msg = CApp::getSingletonPtr()->GetServerList()->GetLastErrorW();
  387. }
  388. continue;
  389. }
  390. }
  391. else {
  392. code == 200;
  393. break;
  394. }
  395. } while (m_base_curl);
  396. EventAUTH* pEvt = new EventAUTH(nullptr);
  397. pEvt->status = code;
  398. pEvt->msg = msg;
  399. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  400. pEvt->Release();
  401. }
  402. void CManageNetWork::ThreadFun_Reg(LPARAM lParam)
  403. {
  404. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  405. int code = 200;
  406. SStringW msg = L"";
  407. std::string data;
  408. HTTPRET httpstatus = m_base_curl->GetSysConfigFromUser(this->m_username, this->m_password, data);
  409. if (httpstatus == HTTPRET::http_user_expired_at)
  410. {
  411. code = -2;
  412. msg = m_base_curl->GetLastErrorW();
  413. }
  414. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  415. code = -3;
  416. msg = m_base_curl->GetLastErrorW();
  417. }
  418. else if (httpstatus == HTTPRET::http_f) {
  419. code = 0;
  420. msg = m_base_curl->GetLastErrorW();
  421. }
  422. if (!data.empty())
  423. {
  424. if (!CApp::getSingletonPtr()->GetSysconfig()->Inti(data)) {
  425. }
  426. }
  427. data.empty();
  428. httpstatus = m_base_curl->PostReg(this->m_username, this->m_password, data);
  429. if (httpstatus == HTTPRET::http_user_expired_at)
  430. {
  431. code = -2;
  432. msg = m_base_curl->GetLastErrorW();
  433. }
  434. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  435. code = -3;
  436. msg = m_base_curl->GetLastErrorW();
  437. }
  438. else if (httpstatus == HTTPRET::http_f) {
  439. code = 0;
  440. msg = m_base_curl->GetLastErrorW();
  441. }
  442. if (!data.empty())
  443. {
  444. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  445. {
  446. code = -1;
  447. msg = CApp::getSingletonPtr()->GetUserinfo()->GetLastErrorW();
  448. }
  449. }
  450. DataToFile();
  451. EventLogin* pEvt = new EventLogin(nullptr);
  452. pEvt->status = code;
  453. pEvt->msg = msg;
  454. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  455. pEvt->Release();
  456. }
  457. void CManageNetWork::LoadFileToData() {
  458. try
  459. {
  460. FileOperate f;
  461. SStringA paht;
  462. paht.Format("%s\\%s", CApp::getSingletonPtr()->GetDataPath().string().c_str(), S_CW2A(DSPROXY_CONFIG_NAME, CP_UTF8));
  463. if (f.open(paht.GetBuffer(0), std::fstream::in | std::fstream::out | std::fstream::app)) {
  464. std::string data = f.readAll();
  465. if (data.empty())
  466. {
  467. return;
  468. }
  469. nlohmann::json j3 = nlohmann::json::parse(data.begin(), data.end());
  470. this->m_username = j3["username"].get<std::string>().c_str();
  471. this->m_password = j3["password"].get<std::string>().c_str();
  472. }
  473. f.close();
  474. }
  475. CATCH_LOG();
  476. }
  477. void CManageNetWork::DataToFile()
  478. {
  479. try
  480. {
  481. nlohmann::json j3;
  482. j3["username"] = this->m_username;
  483. j3["password"] = this->m_password;
  484. auto str = j3.dump(4);
  485. FileOperate f;
  486. SStringA paht;
  487. paht.Format("%s\\%s", CApp::getSingletonPtr()->GetDataPath().string().c_str(), S_CW2A(DSPROXY_CONFIG_NAME, CP_UTF8));
  488. f.create(paht.GetBuffer(0));
  489. if (f.open(paht.GetBuffer(0), std::fstream::in | std::fstream::out | std::fstream::app)) {
  490. f.write(str);
  491. }
  492. f.close();
  493. }
  494. CATCH_LOG();
  495. }