CManageNetWork.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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::ThreadFun_login(LPARAM lParam)
  111. {
  112. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  113. int code = 200;
  114. SStringW msg = L"";
  115. std::string data;
  116. HTTPRET httpstatus = m_base_curl->GetSysConfigFromUser(this->m_username, this->m_password, data);
  117. if (httpstatus == HTTPRET::http_user_expired_at)
  118. {
  119. code = -2;
  120. msg = m_base_curl->GetLastErrorW();
  121. }
  122. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  123. code = -3;
  124. msg = m_base_curl->GetLastErrorW();
  125. }
  126. else if (httpstatus == HTTPRET::http_f) {
  127. code = 0;
  128. msg = m_base_curl->GetLastErrorW();
  129. }
  130. if (!data.empty())
  131. {
  132. if (!CApp::getSingletonPtr()->GetSysconfig()->Inti(data)) {
  133. }
  134. }
  135. data.empty();
  136. httpstatus = m_base_curl->PostLogin(this->m_username, this->m_password, data);
  137. if (httpstatus == HTTPRET::http_user_expired_at)
  138. {
  139. code = -2;
  140. msg = m_base_curl->GetLastErrorW();
  141. }
  142. else if(httpstatus == HTTPRET::http_user_transfer_enable){
  143. code = -3;
  144. msg = m_base_curl->GetLastErrorW();
  145. }
  146. else if (httpstatus == HTTPRET::http_f) {
  147. code = 0;
  148. msg = m_base_curl->GetLastErrorW();
  149. }
  150. if (!data.empty())
  151. {
  152. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  153. {
  154. code = -1;
  155. msg = CApp::getSingletonPtr()->GetUserinfo()->GetLastErrorW();
  156. }
  157. }
  158. DataToFile();
  159. EventLogin* pEvt = new EventLogin(nullptr);
  160. pEvt->status = code;
  161. pEvt->msg = msg;
  162. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  163. pEvt->Release();
  164. }
  165. void CManageNetWork::ThreadFun_Dowlon_Config(LPARAM lParam)
  166. {
  167. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  168. int code = 200;
  169. SStringW msg = L"";
  170. std::string data;
  171. if (!m_base_curl->Download(CApp::getSingletonPtr()->GetUserinfo()->clash_config)) {
  172. code = 0;
  173. msg = L"下载文件失败";
  174. }
  175. EventDoWNload* pEvt = new EventDoWNload(nullptr);
  176. pEvt->status = code;
  177. pEvt->msg = msg;
  178. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  179. pEvt->Release();
  180. }
  181. void CManageNetWork::ThreadFun_Sys_Config(LPARAM lParam)
  182. {
  183. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  184. int code = 200;
  185. SStringW msg = L"";
  186. std::string data;
  187. //if (m_base_curl->Version(data) == HTTPRET::http_f) {
  188. // code = 0;
  189. // msg = m_base_curl->GetLastErrorW();
  190. //}
  191. HTTPRET httpstatus = m_base_curl->GetSysConfig(data);
  192. if (httpstatus == HTTPRET::http_user_expired_at)
  193. {
  194. code = -2;
  195. msg = m_base_curl->GetLastErrorW();
  196. }
  197. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  198. code = -3;
  199. msg = m_base_curl->GetLastErrorW();
  200. }
  201. else if (httpstatus == HTTPRET::http_f) {
  202. code = 0;
  203. msg = m_base_curl->GetLastErrorW();
  204. }
  205. if (!data.empty())
  206. {
  207. if (!CApp::getSingletonPtr()->GetSysconfig()->Inti(data))
  208. {
  209. code = -1;
  210. msg = CApp::getSingletonPtr()->GetSysconfig()->GetLastErrorW();
  211. }
  212. }
  213. EventSysconfig* pEvt = new EventSysconfig(nullptr);
  214. pEvt->status = code;
  215. pEvt->msg = msg;
  216. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  217. pEvt->Release();
  218. }
  219. void CManageNetWork::ThreadFun_Version_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. if (!data.empty())
  230. {
  231. if (!CApp::getSingletonPtr()->GetVerinfo()->Inti(data))
  232. {
  233. code = -1;
  234. msg = CApp::getSingletonPtr()->GetVerinfo()->GetLastErrorW();
  235. }
  236. }
  237. EventVerions* pEvt = new EventVerions(nullptr);
  238. pEvt->status = code;
  239. pEvt->msg = msg;
  240. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  241. pEvt->Release();
  242. }
  243. void CManageNetWork::ThreadFun_Node_Config(LPARAM lParam)
  244. {
  245. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  246. int code = 200;
  247. SStringW msg = L"";
  248. std::string data;
  249. int count = 0;
  250. do
  251. {
  252. data = "";
  253. HTTPRET httpstatus = m_base_curl->GetServerNode(data);
  254. if (httpstatus == HTTPRET::http_f) {
  255. code = 0;
  256. msg = m_base_curl->GetLastErrorW();
  257. }
  258. if (m_base_curl->GetHttpStatus() == 401)
  259. {
  260. if (count >= 3)
  261. {
  262. code = m_base_curl->GetHttpStatus();
  263. msg = L"鉴权失败,需要重新登录";
  264. break;
  265. }
  266. httpstatus = m_base_curl->PostLogin(this->m_username, this->m_password, data);
  267. if (httpstatus == HTTPRET::http_f) {
  268. code = m_base_curl->GetHttpStatus();
  269. msg = m_base_curl->GetLastErrorW();
  270. count++;
  271. }
  272. else {
  273. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  274. {
  275. code = -1;
  276. msg = CApp::getSingletonPtr()->GetServerList()->GetLastErrorW();
  277. }
  278. continue;
  279. }
  280. }
  281. else {
  282. if (!data.empty())
  283. {
  284. if (!CApp::getSingletonPtr()->GetServerList()->Init(data))
  285. {
  286. code = -1;
  287. msg = CApp::getSingletonPtr()->GetServerList()->GetLastErrorW();
  288. }
  289. break;
  290. }
  291. }
  292. } while (m_base_curl);
  293. EventNodeList* pEvt = new EventNodeList(nullptr);
  294. pEvt->status = code;
  295. pEvt->msg = msg;
  296. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  297. pEvt->Release();
  298. }
  299. void CManageNetWork::ThreadFun_Auth(LPARAM lParam)
  300. {
  301. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  302. int code = 200;
  303. SStringW msg = L"";
  304. std::string data;
  305. int count = 0;
  306. do
  307. {
  308. HTTPRET http_status = m_base_curl->Auth(data);
  309. if (http_status == HTTPRET::http_f) {
  310. code = 0;
  311. msg = m_base_curl->GetLastErrorW();
  312. break;
  313. }
  314. auto status = CApp::getSingletonPtr()->GetUserinfo()->authInit(data);
  315. if (status != -1) {
  316. code = status;
  317. msg = CApp::getSingletonPtr()->GetUserinfo()->GetLastErrorW();
  318. break;
  319. }
  320. /*else if (http_status == HTTPRET::http_user_expired_at) {
  321. }
  322. else if (http_status == HTTPRET::http_user_transfer_enable)
  323. {
  324. code = 0;
  325. msg = m_base_curl->GetLastErrorW();
  326. break;
  327. }*/
  328. //重新鉴权
  329. if (m_base_curl->GetHttpStatus() == 401)
  330. {
  331. if (count >= 3)
  332. {
  333. code = m_base_curl->GetHttpStatus();
  334. msg = L"鉴权失败,需要重新登录";
  335. break;
  336. }
  337. http_status = m_base_curl->PostLogin(this->m_username, this->m_password, data);
  338. if (http_status == HTTPRET::http_f) {
  339. code = m_base_curl->GetHttpStatus();
  340. msg = m_base_curl->GetLastErrorW();
  341. count++;
  342. }
  343. else {
  344. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  345. {
  346. code = -1;
  347. msg = CApp::getSingletonPtr()->GetServerList()->GetLastErrorW();
  348. }
  349. continue;
  350. }
  351. }
  352. else {
  353. code == 200;
  354. break;
  355. }
  356. } while (m_base_curl);
  357. EventAUTH* pEvt = new EventAUTH(nullptr);
  358. pEvt->status = code;
  359. pEvt->msg = msg;
  360. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  361. pEvt->Release();
  362. }
  363. void CManageNetWork::ThreadFun_Reg(LPARAM lParam)
  364. {
  365. IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
  366. int code = 200;
  367. SStringW msg = L"";
  368. std::string data;
  369. HTTPRET httpstatus = m_base_curl->GetSysConfigFromUser(this->m_username, this->m_password, data);
  370. if (httpstatus == HTTPRET::http_user_expired_at)
  371. {
  372. code = -2;
  373. msg = m_base_curl->GetLastErrorW();
  374. }
  375. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  376. code = -3;
  377. msg = m_base_curl->GetLastErrorW();
  378. }
  379. else if (httpstatus == HTTPRET::http_f) {
  380. code = 0;
  381. msg = m_base_curl->GetLastErrorW();
  382. }
  383. if (!data.empty())
  384. {
  385. if (!CApp::getSingletonPtr()->GetSysconfig()->Inti(data)) {
  386. }
  387. }
  388. data.empty();
  389. httpstatus = m_base_curl->PostReg(this->m_username, this->m_password, data);
  390. if (httpstatus == HTTPRET::http_user_expired_at)
  391. {
  392. code = -2;
  393. msg = m_base_curl->GetLastErrorW();
  394. }
  395. else if (httpstatus == HTTPRET::http_user_transfer_enable) {
  396. code = -3;
  397. msg = m_base_curl->GetLastErrorW();
  398. }
  399. else if (httpstatus == HTTPRET::http_f) {
  400. code = 0;
  401. msg = m_base_curl->GetLastErrorW();
  402. }
  403. if (!data.empty())
  404. {
  405. if (!CApp::getSingletonPtr()->GetUserinfo()->Init(data))
  406. {
  407. code = -1;
  408. msg = CApp::getSingletonPtr()->GetUserinfo()->GetLastErrorW();
  409. }
  410. }
  411. DataToFile();
  412. EventLogin* pEvt = new EventLogin(nullptr);
  413. pEvt->status = code;
  414. pEvt->msg = msg;
  415. SNotifyCenter::getSingleton().FireEventAsync(pEvt);
  416. pEvt->Release();
  417. }
  418. void CManageNetWork::LoadFileToData() {
  419. try
  420. {
  421. FileOperate f;
  422. SStringA paht;
  423. paht.Format("%s\\%s", CApp::getSingletonPtr()->GetDataPath().string().c_str(), S_CW2A(DSPROXY_CONFIG_NAME, CP_UTF8));
  424. if (f.open(paht.GetBuffer(0), std::fstream::in | std::fstream::out | std::fstream::app)) {
  425. std::string data = f.readAll();
  426. if (data.empty())
  427. {
  428. return;
  429. }
  430. nlohmann::json j3 = nlohmann::json::parse(data.begin(), data.end());
  431. this->m_username = j3["username"].get<std::string>().c_str();
  432. this->m_password = j3["password"].get<std::string>().c_str();
  433. }
  434. f.close();
  435. }
  436. CATCH_LOG();
  437. }
  438. void CManageNetWork::DataToFile()
  439. {
  440. try
  441. {
  442. nlohmann::json j3;
  443. j3["username"] = this->m_username;
  444. j3["password"] = this->m_password;
  445. auto str = j3.dump(4);
  446. FileOperate f;
  447. SStringA paht;
  448. paht.Format("%s\\%s", CApp::getSingletonPtr()->GetDataPath().string().c_str(), S_CW2A(DSPROXY_CONFIG_NAME, CP_UTF8));
  449. f.create(paht.GetBuffer(0));
  450. if (f.open(paht.GetBuffer(0), std::fstream::in | std::fstream::out | std::fstream::app)) {
  451. f.write(str);
  452. }
  453. f.close();
  454. }
  455. CATCH_LOG();
  456. }