CNetWork.cpp 5.7 KB

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