CNetWork.cpp 6.1 KB

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