CNetWork.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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(http_f)
  7. {
  8. }
  9. CNetWork::~CNetWork(void)
  10. {
  11. }
  12. int CNetWork::GetHttpStatus() {
  13. return m_http_status;
  14. }
  15. HTTPRET CNetWork::Version(std::string& data) {
  16. std::vector<cpr::Parameter> p;
  17. p.push_back({ "tag","win" });
  18. p.push_back({ "appverion",S_CW2A(VERSION).GetBuffer(0)});
  19. std::string text = GetUrl("/api/client/v3/version", p, CApp::getSingletonPtr()->GetUserinfo()->access_token);
  20. if (text.empty()) {
  21. return http_f;
  22. }
  23. data = text.c_str();
  24. return http_yes;
  25. }
  26. HTTPRET CNetWork::Refresh(std::string& data) {
  27. std::vector<cpr::Parameter> p;
  28. std::string text = GetUrl("/api/client/v3/refresh", p, CApp::getSingletonPtr()->GetUserinfo()->access_token);
  29. if (text.empty()) {
  30. return http_f;
  31. }
  32. data = text.c_str();
  33. return http_yes;
  34. }
  35. HTTPRET CNetWork::Auth(std::string& data) {
  36. return http_yes;
  37. }
  38. HTTPRET CNetWork::GetServerNode(std::string& data) {
  39. std::vector<cpr::Parameter> p;
  40. std::string text = GetUrl("/api/client/v3/nodes",p,CApp::getSingletonPtr()->GetUserinfo()->access_token);
  41. if (text.empty()) {
  42. return http_f;
  43. }
  44. data = text.c_str();
  45. return http_yes;
  46. }
  47. HTTPRET CNetWork::PostLogin(LPCSTR username, LPCSTR password, std::string& data)
  48. {
  49. std::vector<cpr::Pair> p;
  50. p.push_back({ "email",username });
  51. p.push_back({ "password",password });
  52. std::string text = PostUrl("/api/client/v3/login", p);
  53. if (text.empty()) {
  54. return http_f;
  55. }
  56. data = text.c_str();
  57. return http_yes;
  58. }
  59. void CNetWork::SetUrl(LPCSTR url)
  60. {
  61. m_url = url;
  62. }
  63. SStringA CNetWork::GetLastErrorA()
  64. {
  65. return SStringA().Format("%s", m_error_msg.c_str()).GetBuffer(0);
  66. }
  67. SStringW CNetWork::GetLastErrorW()
  68. {
  69. return S_CA2W(m_error_msg.c_str(),CP_UTF8).GetBuffer(0);
  70. }
  71. std::string CNetWork::GetUrl(std::string path, std::vector<cpr::Parameter> parame,std::string token)
  72. {
  73. cpr::Parameters ps;
  74. std::vector<cpr::Parameter>::iterator it_i;
  75. for (it_i = parame.begin(); it_i != parame.end(); ++it_i)
  76. {
  77. ps.Add(*it_i);
  78. //ps(cpr::Parameter({ it->first, it->second }));
  79. }
  80. cpr::Header hander;
  81. if (!token.empty())
  82. {
  83. hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
  84. }
  85. else {
  86. hander = cpr::Header{ {"accept", "application/json"} };
  87. }
  88. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  89. cpr::Response r;
  90. std::string res_test = "";
  91. int count = 3;
  92. do
  93. {
  94. if (count <= 0)
  95. {
  96. break;
  97. }
  98. if (parame.empty())
  99. {
  100. r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::Timeout{ 60 * 100 });
  101. }
  102. else {
  103. r = cpr::Get(cpr::Url{ s.c_str() }, ps, hander, cpr::Timeout{ 60 * 100 });
  104. }
  105. if (r.status_code == 200 || r.status_code == 201)
  106. {
  107. res_test = r.text;
  108. break;
  109. }
  110. else {
  111. m_http_status = r.status_code;
  112. if (r.error.message.empty())
  113. {
  114. m_error_msg = r.status_line;
  115. }
  116. else {
  117. m_error_msg = UpdateError(r.error.code, r.error.message);
  118. }
  119. }
  120. if (!res_test.empty())
  121. {
  122. break;
  123. }
  124. count--;
  125. } while (res_test.empty());
  126. return res_test;
  127. }
  128. std::string CNetWork::PostUrl(std::string path, std::vector<cpr::Pair> parame, std::string token)
  129. {
  130. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  131. cpr::Header hander;
  132. if (!token.empty())
  133. {
  134. hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
  135. }
  136. else {
  137. hander = cpr::Header{ {"accept", "application/json"} };
  138. }
  139. cpr::Response r;
  140. std::string res_test = "";
  141. int count = 3;
  142. do
  143. {
  144. if (count <= 0)
  145. {
  146. break;
  147. }
  148. if (parame.empty())
  149. {
  150. r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::Timeout{ 60 * 100 });
  151. }
  152. else {
  153. r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ parame.begin(),parame.end() }, hander, cpr::Timeout{ 60 * 100 });
  154. }
  155. if (r.status_code == 200 || r.status_code == 201)
  156. {
  157. res_test = std::move(r.text);
  158. break;
  159. }
  160. else {
  161. m_http_status = r.status_code;
  162. if (r.error.message.empty())
  163. {
  164. m_error_msg = r.status_line;
  165. }
  166. else {
  167. m_error_msg = UpdateError(r.error.code, r.error.message);
  168. }
  169. }
  170. if (!res_test.empty())
  171. {
  172. break;
  173. }
  174. Logger::getSingletonPtr()->INFO("重试" + std::to_string(count) + "次数");
  175. count--;
  176. } while (res_test.empty());
  177. return res_test;
  178. }
  179. std::string CNetWork::Retrying(std::string path, std::vector<cpr::Parameter> parame, std::string token)
  180. {
  181. std::string res_test = "";
  182. int count = 3;
  183. do
  184. {
  185. if (count <= 0)
  186. {
  187. break;
  188. }
  189. res_test = GetUrl(path, parame, token);
  190. if (!res_test.empty())
  191. {
  192. break;
  193. }
  194. count--;
  195. } while (res_test.empty());
  196. return res_test;
  197. }
  198. bool write_data(std::string /*data*/, intptr_t /*userdata*/)
  199. {
  200. return true;
  201. }
  202. bool CNetWork::Download(std::string path)
  203. {
  204. return false;
  205. /*std::string configdir = std::filesystem::current_path().string() + "\\" + CLASHCONFIGDIR;
  206. std::filesystem::path p(configdir);
  207. if (!std::filesystem::exists(p)) {
  208. std::filesystem::create_directory(p);
  209. }
  210. std::string configname = configdir + "\\" CLASHCONFIGNAME;
  211. std::ofstream ofs(configname);
  212. cpr::Url url{ path };
  213. cpr::Session session;
  214. session.SetUrl(url);
  215. cpr::Response response = session.Download(ofs);
  216. return response.status_code == 200;*/
  217. }
  218. void CNetWork::Init()
  219. {
  220. }
  221. void CNetWork::UnInit()
  222. {
  223. }
  224. std::string CNetWork::UpdateError(cpr::ErrorCode code, std::string msg)
  225. {
  226. if (code == cpr::ErrorCode::HOST_RESOLUTION_FAILURE)
  227. {
  228. m_error_msg = "解析域名失败";
  229. }
  230. else if (code == cpr::ErrorCode::OPERATION_TIMEDOUT)
  231. {
  232. m_error_msg = "请求数据超时,请重新请求";
  233. }
  234. else if (code == cpr::ErrorCode::CONNECTION_FAILURE) {
  235. m_error_msg = "发送服务器失败";
  236. }
  237. else if (code == cpr::ErrorCode::INVALID_URL_FORMAT) {
  238. m_error_msg = "未知URL错误";
  239. } else if (code == cpr::ErrorCode::SSL_CONNECT_ERROR)
  240. {
  241. m_error_msg = "SSL连接错误";
  242. }
  243. else {
  244. m_error_msg = msg;
  245. }
  246. return m_error_msg;
  247. }