CNetWork.cpp 9.8 KB

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