CNetWork.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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::GetSysConfig(std::string& data)
  48. {
  49. std::vector<cpr::Parameter> p;
  50. std::string text = GetUrl("/api/client/v3/getconfig", p);
  51. if (text.empty()) {
  52. if (m_http_status == 445)
  53. {
  54. return HTTPRET::http_user_expired_at;
  55. }
  56. else if (m_http_status == 446)
  57. {
  58. return HTTPRET::http_user_transfer_enable;
  59. }
  60. else {
  61. return HTTPRET::http_f;
  62. }
  63. }
  64. data = text.c_str();
  65. return HTTPRET::http_yes;
  66. }
  67. HTTPRET CNetWork::PostLogin(LPCSTR username, LPCSTR password, std::string& data)
  68. {
  69. std::vector<cpr::Pair> p;
  70. p.push_back({ "email",username });
  71. p.push_back({ "password",password });
  72. std::string text = PostUrl("/api/client/v3/login", p);
  73. if (text.empty()) {
  74. if (m_http_status == 445)
  75. {
  76. return HTTPRET::http_user_expired_at;
  77. } else if (m_http_status == 446)
  78. {
  79. return HTTPRET::http_user_transfer_enable;
  80. }
  81. else {
  82. return HTTPRET::http_f;
  83. }
  84. }
  85. data = text.c_str();
  86. return HTTPRET::http_yes;
  87. }
  88. HTTPRET CNetWork::GetSysConfigFromUser(LPCSTR username, LPCSTR password, std::string& data) {
  89. std::vector<cpr::Parameter> p;
  90. p.push_back({ "email",username });
  91. p.push_back({ "password",password });
  92. std::string text = GetUrl("/api/client/v3/getconfig", p);
  93. if (text.empty()) {
  94. if (m_http_status == 445)
  95. {
  96. return HTTPRET::http_user_expired_at;
  97. }
  98. else if (m_http_status == 446)
  99. {
  100. return HTTPRET::http_user_transfer_enable;
  101. }
  102. else {
  103. return HTTPRET::http_f;
  104. }
  105. }
  106. data = text.c_str();
  107. return HTTPRET::http_yes;
  108. }
  109. void CNetWork::SetUrl(LPCSTR url)
  110. {
  111. m_url = url;
  112. }
  113. SStringA CNetWork::GetLastErrorA()
  114. {
  115. return SStringA().Format("%s", m_error_msg.c_str()).GetBuffer(0);
  116. }
  117. SStringW CNetWork::GetLastErrorW()
  118. {
  119. return S_CA2W(m_error_msg.c_str(),CP_UTF8).GetBuffer(0);
  120. }
  121. std::string CNetWork::GetUrl(std::string path, std::vector<cpr::Parameter> parame,std::string token)
  122. {
  123. cpr::Parameters ps;
  124. std::vector<cpr::Parameter>::iterator it_i;
  125. for (it_i = parame.begin(); it_i != parame.end(); ++it_i)
  126. {
  127. ps.Add(*it_i);
  128. //ps(cpr::Parameter({ it->first, it->second }));
  129. }
  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. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  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() }, cpr::VerifySsl{ false }, hander, cpr::Timeout{ 60 * 1000 });
  151. }
  152. else {
  153. r = cpr::Get(cpr::Url{ s.c_str() }, cpr::VerifySsl{ false }, ps, hander, cpr::Timeout{ 60 * 1000 });
  154. }
  155. if (r.status_code == 200 || r.status_code == 201)
  156. {
  157. res_test = 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. count--;
  175. } while (res_test.empty());
  176. return res_test;
  177. }
  178. std::string CNetWork::PostUrl(std::string path, std::vector<cpr::Pair> parame, std::string token)
  179. {
  180. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  181. cpr::Header hander;
  182. if (!token.empty())
  183. {
  184. hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
  185. }
  186. else {
  187. hander = cpr::Header{ {"accept", "application/json"} };
  188. }
  189. cpr::Response r;
  190. std::string res_test = "";
  191. int count = 3;
  192. do
  193. {
  194. if (count <= 0)
  195. {
  196. break;
  197. }
  198. if (parame.empty())
  199. {
  200. r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::VerifySsl{false}, cpr::Timeout{ 60 * 1000 });
  201. }
  202. else {
  203. r = cpr::Post(cpr::Url{ s.c_str() }, cpr::VerifySsl{ false }, cpr::Payload{ parame.begin(),parame.end() }, hander, cpr::Timeout{ 60 * 1000 });
  204. }
  205. Logger::getSingletonPtr()->DEBUG(r.url.c_str());
  206. if (r.status_code == 200 || r.status_code == 201)
  207. {
  208. res_test = std::move(r.text);
  209. break;
  210. }
  211. else {
  212. m_http_status = r.status_code;
  213. if (r.error.message.empty())
  214. {
  215. m_error_msg = r.status_line;
  216. }
  217. else {
  218. m_error_msg = UpdateError(r.error.code, r.error.message);
  219. }
  220. }
  221. if (!res_test.empty())
  222. {
  223. break;
  224. }
  225. Logger::getSingletonPtr()->INFO("重试" + std::to_string(count) + "次数");
  226. count--;
  227. } while (res_test.empty());
  228. return res_test;
  229. }
  230. std::string CNetWork::Retrying(std::string path, std::vector<cpr::Parameter> parame, std::string token)
  231. {
  232. std::string res_test = "";
  233. int count = 3;
  234. do
  235. {
  236. if (count <= 0)
  237. {
  238. break;
  239. }
  240. res_test = GetUrl(path, parame, token);
  241. if (!res_test.empty())
  242. {
  243. break;
  244. }
  245. count--;
  246. } while (res_test.empty());
  247. return res_test;
  248. }
  249. bool write_data(std::string /*data*/, intptr_t /*userdata*/)
  250. {
  251. return true;
  252. }
  253. bool CNetWork::Download(std::string path)
  254. {
  255. return false;
  256. /*std::string configdir = std::filesystem::current_path().string() + "\\" + CLASHCONFIGDIR;
  257. std::filesystem::path p(configdir);
  258. if (!std::filesystem::exists(p)) {
  259. std::filesystem::create_directory(p);
  260. }
  261. std::string configname = configdir + "\\" CLASHCONFIGNAME;
  262. std::ofstream ofs(configname);
  263. cpr::Url url{ path };
  264. cpr::Session session;
  265. session.SetUrl(url);
  266. cpr::Response response = session.Download(ofs);
  267. return response.status_code == 200;*/
  268. }
  269. void CNetWork::Init()
  270. {
  271. }
  272. void CNetWork::UnInit()
  273. {
  274. }
  275. std::string CNetWork::UpdateError(cpr::ErrorCode code, std::string msg)
  276. {
  277. if (code == cpr::ErrorCode::HOST_RESOLUTION_FAILURE)
  278. {
  279. m_error_msg = "解析域名失败";
  280. }
  281. else if (code == cpr::ErrorCode::OPERATION_TIMEDOUT)
  282. {
  283. m_error_msg = "请求数据超时,请重新请求";
  284. }
  285. else if (code == cpr::ErrorCode::CONNECTION_FAILURE) {
  286. m_error_msg = "发送服务器失败";
  287. }
  288. else if (code == cpr::ErrorCode::INVALID_URL_FORMAT) {
  289. m_error_msg = "未知URL错误";
  290. } else if (code == cpr::ErrorCode::SSL_CONNECT_ERROR)
  291. {
  292. m_error_msg = "SSL连接错误";
  293. }
  294. else {
  295. m_error_msg = msg;
  296. }
  297. return m_error_msg;
  298. }