CNetWork.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. #include "stdafx.h"
  2. #include "CNetWork.h"
  3. #include <nlohmann/json.hpp>
  4. #include <fmt/format.h>
  5. #include "Logger.h"
  6. CNetWork::CNetWork() : m_http_ret(HTTPRET::http_f)
  7. {
  8. initSessionPool(100);
  9. }
  10. CNetWork::~CNetWork(void)
  11. {
  12. DestoryOneConnSessionPool();
  13. }
  14. int CNetWork::GetHttpStatus() {
  15. return m_http_status;
  16. }
  17. HTTPRET CNetWork::GetMySub(std::string& data)
  18. {
  19. std::vector<cpr::Parameter> p;
  20. p.push_back({ "token", CApp::getSingletonPtr()->GetLoginInfo()->token.c_str() });
  21. p.push_back({ "auth_data", CApp::getSingletonPtr()->GetLoginInfo()->auth_data.c_str() });
  22. std::string text = GetUrl("/api/v1/manager/getSubscribe", p, "");
  23. if (text.empty()) {
  24. return HTTPRET::http_f;
  25. }
  26. data = text.c_str();
  27. return HTTPRET::http_yes;
  28. return HTTPRET();
  29. }
  30. void CNetWork::SetUrlArray(std::vector<ServerListUrl> data) {
  31. m_url_list = data;
  32. }
  33. HTTPRET CNetWork::Version(std::string& data) {
  34. std::vector<cpr::Parameter> p;
  35. p.push_back({ "tag","win" });
  36. p.push_back({ "appverion",S_CW2A(VERSION).GetBuffer(0)});
  37. std::string text = GetUrl("/api/client/v3/version", p, CApp::getSingletonPtr()->GetLoginInfo()->token);
  38. if (text.empty()) {
  39. return HTTPRET::http_f;
  40. }
  41. data = text.c_str();
  42. return HTTPRET::http_yes;
  43. }
  44. HTTPRET CNetWork::Refresh(std::string& data) {
  45. std::vector<cpr::Parameter> p;
  46. std::string text = GetUrl("/api/client/v3/refresh", p, CApp::getSingletonPtr()->GetLoginInfo()->token);
  47. if (text.empty()) {
  48. return HTTPRET::http_f;
  49. }
  50. data = text.c_str();
  51. return HTTPRET::http_yes;
  52. }
  53. HTTPRET CNetWork::Auth(std::string& data){
  54. std::vector<cpr::Parameter> p;
  55. std::string text = GetUrl("/api/client/v3/authUser",p,CApp::getSingletonPtr()->GetLoginInfo()->token);
  56. if (text.empty()) {
  57. return HTTPRET::http_f;
  58. }
  59. data = text.c_str();
  60. return HTTPRET::http_yes;
  61. }
  62. HTTPRET CNetWork::GetServerNode(std::string& data) {
  63. std::vector<cpr::Parameter> p;
  64. p.push_back({ "token", CApp::getSingletonPtr()->GetLoginInfo()->token.c_str() });
  65. p.push_back({ "auth_data", CApp::getSingletonPtr()->GetLoginInfo()->auth_data.c_str() });
  66. std::string text = GetUrl("/api/v1/manager/server/fetch",p,"");
  67. if (text.empty()) {
  68. return HTTPRET::http_f;
  69. }
  70. data = text.c_str();
  71. return HTTPRET::http_yes;
  72. }
  73. HTTPRET CNetWork::GetSysConfig(std::string& data)
  74. {
  75. AutoToggleNode();
  76. std::vector<cpr::Parameter> p;
  77. std::string text = GetUrl("/api/v1/manage/getUrl", p);
  78. if (text.empty()) {
  79. if (m_http_status == 445)
  80. {
  81. return HTTPRET::http_user_expired_at;
  82. }
  83. else if (m_http_status == 446)
  84. {
  85. return HTTPRET::http_user_transfer_enable;
  86. }
  87. else {
  88. return HTTPRET::http_f;
  89. }
  90. }
  91. data = text.c_str();
  92. return HTTPRET::http_yes;
  93. }
  94. HTTPRET CNetWork::PostReg(LPCSTR username, LPCSTR password, std::string& data) {
  95. std::vector<cpr::Pair> p;
  96. p.push_back({ "username",""});
  97. p.push_back({ "email",username });
  98. p.push_back({ "password",password });
  99. std::string text = PostUrl("/api/client/v2/register", p);
  100. if (text.empty()) {
  101. if (m_http_status == 445)
  102. {
  103. return HTTPRET::http_user_expired_at;
  104. }
  105. else if (m_http_status == 446)
  106. {
  107. return HTTPRET::http_user_transfer_enable;
  108. }
  109. else if (m_http_status == 447) {
  110. return HTTPRET::http_user_enable;
  111. }
  112. else {
  113. return HTTPRET::http_f;
  114. }
  115. }
  116. data = text.c_str();
  117. return HTTPRET::http_yes;
  118. }
  119. HTTPRET CNetWork::PostLogin(LPCSTR username, LPCSTR password, std::string& data)
  120. {
  121. std::vector<cpr::Pair> p;
  122. p.push_back({ "email",username });
  123. p.push_back({ "password",password });
  124. std::string text = PostUrl("/api/v1/manage/auth/login/", p);
  125. if (text.empty()) {
  126. if (m_http_status == 445)
  127. {
  128. return HTTPRET::http_user_expired_at;
  129. } else if (m_http_status == 446)
  130. {
  131. return HTTPRET::http_user_transfer_enable;
  132. }
  133. else if (m_http_status == 447) {
  134. return HTTPRET::http_user_enable;
  135. }
  136. else {
  137. return HTTPRET::http_f;
  138. }
  139. }
  140. data = text.c_str();
  141. return HTTPRET::http_yes;
  142. }
  143. HTTPRET CNetWork::GetSysConfigFromUser(LPCSTR username, LPCSTR password, std::string& data) {
  144. std::vector<cpr::Parameter> p;
  145. p.push_back({ "email",username });
  146. p.push_back({ "password",password });
  147. std::string text = GetUrl("/api/client/v3/getconfig", p);
  148. if (text.empty()) {
  149. if (m_http_status == 445)
  150. {
  151. return HTTPRET::http_user_expired_at;
  152. }
  153. else if (m_http_status == 446)
  154. {
  155. return HTTPRET::http_user_transfer_enable;
  156. }
  157. else {
  158. return HTTPRET::http_f;
  159. }
  160. }
  161. data = text.c_str();
  162. return HTTPRET::http_yes;
  163. }
  164. void CNetWork::SetUrl(LPCSTR url)
  165. {
  166. m_url = url;
  167. }
  168. void CNetWork::initSessionPool(int szie) {
  169. for (int i = 0 ; i < szie ; i++)
  170. {
  171. std::shared_ptr<cpr::Session> pp = std::make_shared<cpr::Session>();
  172. m_session_vect.push_back(std::move(pp));
  173. }
  174. }
  175. void CNetWork::DestoryOneConnSessionPool() {
  176. for (auto &conn : m_session_vect)
  177. {
  178. std::move(m_session_vect.front());
  179. }
  180. }
  181. std::shared_ptr<cpr::Session> CNetWork::GetSession() {
  182. std::shared_ptr<cpr::Session> pp = nullptr;
  183. m_mutx.lock();
  184. if (m_session_vect.size() > 0)
  185. {
  186. pp = m_session_vect.front();
  187. m_session_vect.pop_back();
  188. }
  189. m_mutx.unlock();
  190. /*if (pp == nullptr)
  191. {
  192. pp = std::make_shared<cpr::Session>();
  193. }*/
  194. return pp;
  195. }
  196. void CNetWork::pullSession(std::shared_ptr<cpr::Session> &p) {
  197. m_mutx.lock();
  198. m_session_vect.push_back(p);
  199. m_mutx.unlock();
  200. }
  201. SStringA CNetWork::GetLastErrorA()
  202. {
  203. return SStringA().Format("%s", m_error_msg.c_str()).GetBuffer(0);
  204. }
  205. SStringW CNetWork::GetLastErrorW()
  206. {
  207. return S_CA2W(m_error_msg.c_str(),CP_UTF8).GetBuffer(0);
  208. }
  209. std::string CNetWork::GetUrl(std::string path, std::vector<cpr::Parameter> parame,std::string token)
  210. {
  211. cpr::Parameters ps;
  212. std::vector<cpr::Parameter>::iterator it_i;
  213. for (it_i = parame.begin(); it_i != parame.end(); ++it_i)
  214. {
  215. ps.Add(*it_i);
  216. //ps(cpr::Parameter({ it->first, it->second }));
  217. }
  218. cpr::Header hander;
  219. if (!token.empty())
  220. {
  221. hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
  222. }
  223. else {
  224. hander = cpr::Header{ {"accept", "application/json"} };
  225. }
  226. cpr::Response r;
  227. std::string res_test = "";
  228. //auto session = GetSession();
  229. int count = 3;
  230. /*int serverurlid = 0;*/
  231. do
  232. {
  233. if (count <= 0)
  234. {
  235. break;
  236. }
  237. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  238. if (parame.empty())
  239. {
  240. m_session.SetHeader(hander);
  241. m_session.SetVerifySsl(false);
  242. m_session.SetTimeout(cpr::Timeout{TIMEOUTE});
  243. m_session.SetUrl(s.c_str());
  244. //session->SetDebugCallback()
  245. r = m_session.Get();
  246. //r = cpr::Get(cpr::Url{ s.c_str() }, cpr::VerifySsl{ false }, hander, cpr::Timeout{ 60 * 1000 });
  247. }
  248. else {
  249. m_session.SetHeader(hander);
  250. m_session.SetVerifySsl(false);
  251. m_session.SetTimeout(cpr::Timeout{ TIMEOUTE });
  252. m_session.SetUrl(s.c_str());
  253. m_session.SetParameters(ps);
  254. r = m_session.Get();
  255. //r = cpr::Get(cpr::Url{ s.c_str() }, cpr::VerifySsl{ false }, ps, hander, cpr::Timeout{ 60 * 1000 });
  256. }
  257. if (r.status_code == 200 || r.status_code == 201)
  258. {
  259. res_test = r.text;
  260. break;
  261. }
  262. else {
  263. m_http_status = r.status_code;
  264. if (r.error.message.empty())
  265. {
  266. m_error_msg = r.status_line;
  267. }
  268. else {
  269. m_error_msg = UpdateError(r.error.code, r.error.message);
  270. }
  271. }
  272. if (!res_test.empty())
  273. {
  274. break;
  275. }
  276. Sleep(3000);
  277. count--;
  278. Logger::getSingletonPtr()->INFO("get 重试" + std::to_string(count) + "次数");
  279. } while (res_test.empty());
  280. //pullSession(session);
  281. return res_test;
  282. }
  283. std::string CNetWork::PostUrl(std::string path, std::vector<cpr::Pair> parame, std::string token)
  284. {
  285. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  286. cpr::Header hander;
  287. if (!token.empty())
  288. {
  289. hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
  290. }
  291. else {
  292. hander = cpr::Header{ {"accept", "application/json"} };
  293. }
  294. cpr::Response r;
  295. std::string res_test = "";
  296. /**
  297. * \brief
  298. * \param
  299. * \param
  300. * \param
  301. * \return
  302. */
  303. int count = 3;
  304. do
  305. {
  306. if (count <= 0)
  307. {
  308. break;
  309. }
  310. if (parame.empty())
  311. {
  312. //r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::VerifySsl{false}, cpr::Timeout{ 60 * 1000 });
  313. m_session.SetHeader(hander);
  314. m_session.SetVerifySsl(false);
  315. m_session.SetTimeout(cpr::Timeout{ TIMEOUTE });
  316. m_session.SetUrl(s.c_str());
  317. r = m_session.Get();
  318. }
  319. else {
  320. m_session.SetHeader(hander);
  321. m_session.SetVerifySsl(false);
  322. m_session.SetTimeout(cpr::Timeout{ TIMEOUTE });
  323. m_session.SetUrl(s.c_str());
  324. m_session.SetPayload(cpr::Payload{ parame.begin(),parame.end() });
  325. r = m_session.Post();
  326. //r = cpr::Post(cpr::Url{ s.c_str() }, cpr::VerifySsl{ false }, cpr::Payload{ parame.begin(),parame.end() }, hander, cpr::Timeout{ 60 * 1000 });
  327. }
  328. //Logger::getSingletonPtr()->DEBUG(r.url.c_str());
  329. if (r.status_code == 200 || r.status_code == 201)
  330. {
  331. res_test = std::move(r.text);
  332. break;
  333. }
  334. else {
  335. m_http_status = r.status_code;
  336. if (r.error.message.empty())
  337. {
  338. m_error_msg = r.status_line;
  339. }
  340. else {
  341. m_error_msg = UpdateError(r.error.code, r.error.message);
  342. }
  343. }
  344. if (!res_test.empty())
  345. {
  346. break;
  347. }
  348. Logger::getSingletonPtr()->INFO("重试" + std::to_string(count) + "次数");
  349. count--;
  350. } while (res_test.empty());
  351. //pullSession(session);
  352. return res_test;
  353. }
  354. void CNetWork::AutoToggleNode() {
  355. cpr::Header hander;
  356. hander = cpr::Header{ {"accept", "application/json"} };
  357. std::vector<cpr::Parameter> p;
  358. p.push_back({ "tag","win" });
  359. p.push_back({ "appverion",S_CW2A(VERSION).GetBuffer(0) });
  360. cpr::Parameters ps;
  361. std::vector<cpr::Parameter>::iterator it_i;
  362. for (it_i = p.begin(); it_i != p.end(); ++it_i)
  363. {
  364. ps.Add(*it_i);
  365. //ps(cpr::Parameter({ it->first, it->second }));
  366. }
  367. //m_url_list.reserve(m_url.size());
  368. //切换节点,
  369. for each (ServerListUrl itme in m_url_list)
  370. {
  371. cpr::Session m_session;
  372. auto s = fmt::format("{0}{1}", itme.url, "/api/v1/manage/getUrl");
  373. m_session.SetHeader(hander);
  374. m_session.SetVerifySsl(false);
  375. m_session.SetTimeout(cpr::Timeout{ TIMEOUTE });
  376. m_session.SetUrl(s.c_str());
  377. m_session.SetParameters(ps);
  378. auto r = m_session.Get();
  379. if (r.status_code != 200) {
  380. continue;
  381. }
  382. else
  383. {
  384. if (m_url != itme.url)
  385. {
  386. m_url = itme.url;
  387. Logger::getSingletonPtr()->INFO(S_CW2A(itme.name).GetBuffer(0));
  388. }
  389. break;
  390. }
  391. }
  392. }
  393. std::string CNetWork::Retrying(std::string path, std::vector<cpr::Parameter> parame, std::string token)
  394. {
  395. std::string res_test = "";
  396. int count = 3;
  397. do
  398. {
  399. if (count <= 0)
  400. {
  401. break;
  402. }
  403. res_test = GetUrl(path, parame, token);
  404. if (!res_test.empty())
  405. {
  406. break;
  407. }
  408. count--;
  409. } while (res_test.empty());
  410. return res_test;
  411. }
  412. bool write_data(std::string /*data*/, intptr_t /*userdata*/)
  413. {
  414. return true;
  415. }
  416. bool CNetWork::Download(std::string path)
  417. {
  418. //return false;
  419. std::string configdir = std::filesystem::current_path().string() + "\\" + S_CW2A(CLASH_ASSETS_DIR_NAME).GetBuffer(0);
  420. std::filesystem::path p(configdir);
  421. if (!std::filesystem::exists(p)) {
  422. std::filesystem::create_directory(p);
  423. }
  424. std::string configname = configdir + "\\" + CLASHCONFIGDIR;
  425. std::ofstream ofs(configname);
  426. cpr::Url url{ path };
  427. cpr::Session session;
  428. session.SetUrl(url);
  429. cpr::Response response = session.Download(ofs);
  430. return response.status_code == 200;
  431. }
  432. void CNetWork::Init()
  433. {
  434. }
  435. void CNetWork::UnInit()
  436. {
  437. }
  438. std::string CNetWork::UpdateError(cpr::ErrorCode code, std::string msg)
  439. {
  440. if (code == cpr::ErrorCode::HOST_RESOLUTION_FAILURE)
  441. {
  442. m_error_msg = "解析域名失败";
  443. }
  444. else if (code == cpr::ErrorCode::OPERATION_TIMEDOUT)
  445. {
  446. m_error_msg = "请求数据超时,请重新请求";
  447. }
  448. else if (code == cpr::ErrorCode::CONNECTION_FAILURE) {
  449. m_error_msg = "发送服务器失败";
  450. }
  451. else if (code == cpr::ErrorCode::INVALID_URL_FORMAT) {
  452. m_error_msg = "未知URL错误";
  453. } else if (code == cpr::ErrorCode::SSL_CONNECT_ERROR)
  454. {
  455. m_error_msg = "SSL连接错误";
  456. }
  457. else {
  458. m_error_msg = msg;
  459. }
  460. return m_error_msg;
  461. }