CNetWork.cpp 11 KB

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