CNetWork.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. std::vector<cpr::Parameter> p;
  61. std::string text = GetUrl("/api/client/v3/getconfig", p);
  62. if (text.empty()) {
  63. if (m_http_status == 445)
  64. {
  65. return HTTPRET::http_user_expired_at;
  66. }
  67. else if (m_http_status == 446)
  68. {
  69. return HTTPRET::http_user_transfer_enable;
  70. }
  71. else {
  72. return HTTPRET::http_f;
  73. }
  74. }
  75. data = text.c_str();
  76. return HTTPRET::http_yes;
  77. }
  78. HTTPRET CNetWork::PostReg(LPCSTR username, LPCSTR password, std::string& data) {
  79. std::vector<cpr::Pair> p;
  80. p.push_back({ "username",""});
  81. p.push_back({ "email",username });
  82. p.push_back({ "password",password });
  83. std::string text = PostUrl("/api/client/v2/register", p);
  84. if (text.empty()) {
  85. if (m_http_status == 445)
  86. {
  87. return HTTPRET::http_user_expired_at;
  88. }
  89. else if (m_http_status == 446)
  90. {
  91. return HTTPRET::http_user_transfer_enable;
  92. }
  93. else {
  94. return HTTPRET::http_f;
  95. }
  96. }
  97. data = text.c_str();
  98. return HTTPRET::http_yes;
  99. }
  100. HTTPRET CNetWork::PostLogin(LPCSTR username, LPCSTR password, std::string& data)
  101. {
  102. std::vector<cpr::Pair> p;
  103. p.push_back({ "email",username });
  104. p.push_back({ "password",password });
  105. std::string text = PostUrl("/api/client/v3/login", p);
  106. if (text.empty()) {
  107. if (m_http_status == 445)
  108. {
  109. return HTTPRET::http_user_expired_at;
  110. } else if (m_http_status == 446)
  111. {
  112. return HTTPRET::http_user_transfer_enable;
  113. }
  114. else {
  115. return HTTPRET::http_f;
  116. }
  117. }
  118. data = text.c_str();
  119. return HTTPRET::http_yes;
  120. }
  121. HTTPRET CNetWork::GetSysConfigFromUser(LPCSTR username, LPCSTR password, std::string& data) {
  122. std::vector<cpr::Parameter> p;
  123. p.push_back({ "email",username });
  124. p.push_back({ "password",password });
  125. std::string text = GetUrl("/api/client/v3/getconfig", p);
  126. if (text.empty()) {
  127. if (m_http_status == 445)
  128. {
  129. return HTTPRET::http_user_expired_at;
  130. }
  131. else if (m_http_status == 446)
  132. {
  133. return HTTPRET::http_user_transfer_enable;
  134. }
  135. else {
  136. return HTTPRET::http_f;
  137. }
  138. }
  139. data = text.c_str();
  140. return HTTPRET::http_yes;
  141. }
  142. void CNetWork::SetUrl(LPCSTR url)
  143. {
  144. m_url = url;
  145. }
  146. void CNetWork::initSessionPool(int szie) {
  147. for (int i = 0 ; i < szie ; i++)
  148. {
  149. std::shared_ptr<cpr::Session> pp = std::make_shared<cpr::Session>();
  150. m_session_vect.push_back(std::move(pp));
  151. }
  152. }
  153. void CNetWork::DestoryOneConnSessionPool() {
  154. for (auto &conn : m_session_vect)
  155. {
  156. std::move(m_session_vect.front());
  157. }
  158. }
  159. std::shared_ptr<cpr::Session> CNetWork::GetSession() {
  160. std::shared_ptr<cpr::Session> pp = nullptr;
  161. m_mutx.lock();
  162. if (m_session_vect.size() > 0)
  163. {
  164. pp = m_session_vect.front();
  165. m_session_vect.pop_back();
  166. }
  167. m_mutx.unlock();
  168. /*if (pp == nullptr)
  169. {
  170. pp = std::make_shared<cpr::Session>();
  171. }*/
  172. return pp;
  173. }
  174. void CNetWork::pullSession(std::shared_ptr<cpr::Session> &p) {
  175. m_mutx.lock();
  176. m_session_vect.push_back(p);
  177. m_mutx.unlock();
  178. }
  179. SStringA CNetWork::GetLastErrorA()
  180. {
  181. return SStringA().Format("%s", m_error_msg.c_str()).GetBuffer(0);
  182. }
  183. SStringW CNetWork::GetLastErrorW()
  184. {
  185. return S_CA2W(m_error_msg.c_str(),CP_UTF8).GetBuffer(0);
  186. }
  187. std::string CNetWork::GetUrl(std::string path, std::vector<cpr::Parameter> parame,std::string token)
  188. {
  189. AutoToggleNode();
  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. count--;
  256. Logger::getSingletonPtr()->INFO("get 重试" + std::to_string(count) + "次数");
  257. } while (res_test.empty());
  258. //pullSession(session);
  259. return res_test;
  260. }
  261. std::string CNetWork::PostUrl(std::string path, std::vector<cpr::Pair> parame, std::string token)
  262. {
  263. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  264. cpr::Header hander;
  265. if (!token.empty())
  266. {
  267. hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
  268. }
  269. else {
  270. hander = cpr::Header{ {"accept", "application/json"} };
  271. }
  272. cpr::Response r;
  273. std::string res_test = "";
  274. /**
  275. * \brief
  276. * \param
  277. * \param
  278. * \param
  279. * \return
  280. */
  281. int count = 3;
  282. do
  283. {
  284. if (count <= 0)
  285. {
  286. break;
  287. }
  288. if (parame.empty())
  289. {
  290. //r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::VerifySsl{false}, cpr::Timeout{ 60 * 1000 });
  291. m_session.SetHeader(hander);
  292. m_session.SetVerifySsl(false);
  293. m_session.SetTimeout(cpr::Timeout{ TIMEOUTE });
  294. m_session.SetUrl(s.c_str());
  295. r = m_session.Get();
  296. }
  297. else {
  298. m_session.SetHeader(hander);
  299. m_session.SetVerifySsl(false);
  300. m_session.SetTimeout(cpr::Timeout{ TIMEOUTE });
  301. m_session.SetUrl(s.c_str());
  302. m_session.SetPayload(cpr::Payload{ parame.begin(),parame.end() });
  303. r = m_session.Post();
  304. //r = cpr::Post(cpr::Url{ s.c_str() }, cpr::VerifySsl{ false }, cpr::Payload{ parame.begin(),parame.end() }, hander, cpr::Timeout{ 60 * 1000 });
  305. }
  306. //Logger::getSingletonPtr()->DEBUG(r.url.c_str());
  307. if (r.status_code == 200 || r.status_code == 201)
  308. {
  309. res_test = std::move(r.text);
  310. break;
  311. }
  312. else {
  313. m_http_status = r.status_code;
  314. if (r.error.message.empty())
  315. {
  316. m_error_msg = r.status_line;
  317. }
  318. else {
  319. m_error_msg = UpdateError(r.error.code, r.error.message);
  320. }
  321. }
  322. if (!res_test.empty())
  323. {
  324. break;
  325. }
  326. Logger::getSingletonPtr()->INFO("重试" + std::to_string(count) + "次数");
  327. count--;
  328. } while (res_test.empty());
  329. //pullSession(session);
  330. return res_test;
  331. }
  332. void CNetWork::AutoToggleNode() {
  333. cpr::Header hander;
  334. hander = cpr::Header{ {"accept", "application/json"} };
  335. std::vector<cpr::Parameter> p;
  336. p.push_back({ "tag","win" });
  337. p.push_back({ "appverion",S_CW2A(VERSION).GetBuffer(0) });
  338. cpr::Parameters ps;
  339. std::vector<cpr::Parameter>::iterator it_i;
  340. for (it_i = p.begin(); it_i != p.end(); ++it_i)
  341. {
  342. ps.Add(*it_i);
  343. //ps(cpr::Parameter({ it->first, it->second }));
  344. }
  345. //切换节点,
  346. for each (ServerListUrl itme in m_url_list)
  347. {
  348. auto s = fmt::format("{0}{1}", itme.url, "/api/client/v3/version");
  349. m_session.SetHeader(hander);
  350. m_session.SetVerifySsl(false);
  351. m_session.SetTimeout(cpr::Timeout{ TIMEOUTE });
  352. m_session.SetUrl(s.c_str());
  353. m_session.SetParameters(ps);
  354. auto r = m_session.Get();
  355. if (r.status_code == 200) {
  356. m_url = itme.url;
  357. Logger::getSingletonPtr()->INFO(S_CW2A(itme.name).GetBuffer(0));
  358. }
  359. }
  360. }
  361. std::string CNetWork::Retrying(std::string path, std::vector<cpr::Parameter> parame, std::string token)
  362. {
  363. std::string res_test = "";
  364. int count = 3;
  365. do
  366. {
  367. if (count <= 0)
  368. {
  369. break;
  370. }
  371. res_test = GetUrl(path, parame, token);
  372. if (!res_test.empty())
  373. {
  374. break;
  375. }
  376. count--;
  377. } while (res_test.empty());
  378. return res_test;
  379. }
  380. bool write_data(std::string /*data*/, intptr_t /*userdata*/)
  381. {
  382. return true;
  383. }
  384. bool CNetWork::Download(std::string path)
  385. {
  386. return false;
  387. /*std::string configdir = std::filesystem::current_path().string() + "\\" + CLASHCONFIGDIR;
  388. std::filesystem::path p(configdir);
  389. if (!std::filesystem::exists(p)) {
  390. std::filesystem::create_directory(p);
  391. }
  392. std::string configname = configdir + "\\" CLASHCONFIGNAME;
  393. std::ofstream ofs(configname);
  394. cpr::Url url{ path };
  395. cpr::Session session;
  396. session.SetUrl(url);
  397. cpr::Response response = session.Download(ofs);
  398. return response.status_code == 200;*/
  399. }
  400. void CNetWork::Init()
  401. {
  402. }
  403. void CNetWork::UnInit()
  404. {
  405. }
  406. std::string CNetWork::UpdateError(cpr::ErrorCode code, std::string msg)
  407. {
  408. if (code == cpr::ErrorCode::HOST_RESOLUTION_FAILURE)
  409. {
  410. m_error_msg = "解析域名失败";
  411. }
  412. else if (code == cpr::ErrorCode::OPERATION_TIMEDOUT)
  413. {
  414. m_error_msg = "请求数据超时,请重新请求";
  415. }
  416. else if (code == cpr::ErrorCode::CONNECTION_FAILURE) {
  417. m_error_msg = "发送服务器失败";
  418. }
  419. else if (code == cpr::ErrorCode::INVALID_URL_FORMAT) {
  420. m_error_msg = "未知URL错误";
  421. } else if (code == cpr::ErrorCode::SSL_CONNECT_ERROR)
  422. {
  423. m_error_msg = "SSL连接错误";
  424. }
  425. else {
  426. m_error_msg = msg;
  427. }
  428. return m_error_msg;
  429. }