CNetWork.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if (parame.empty())
  90. {
  91. r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::Timeout{ 60 * 100});
  92. }
  93. else {
  94. r = cpr::Get(cpr::Url{ s.c_str() }, ps, hander, cpr::Timeout{ 60 * 100 });
  95. }
  96. if (r.status_code == 200 || r.status_code == 201)
  97. {
  98. return std::move(r.text);
  99. }
  100. else {
  101. m_http_status = r.status_code;
  102. if (r.error.message.empty())
  103. {
  104. m_error_msg = r.status_line;
  105. }
  106. else {
  107. m_error_msg = r.error.message;
  108. }
  109. return "";
  110. }
  111. return std::string("");
  112. }
  113. std::string CNetWork::PostUrl(std::string path, std::vector<cpr::Pair> parame, std::string token)
  114. {
  115. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  116. cpr::Header hander;
  117. if (!token.empty())
  118. {
  119. hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
  120. }
  121. else {
  122. hander = cpr::Header{ {"accept", "application/json"} };
  123. }
  124. cpr::Response r;
  125. if (parame.empty())
  126. {
  127. r = cpr::Get(cpr::Url{ s.c_str() },hander , cpr::Timeout{ 60 * 100 });
  128. }
  129. else {
  130. r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ parame.begin(),parame.end() }, hander, cpr::Timeout{ 60 * 100 });
  131. }
  132. if (r.status_code == 200 || r.status_code == 201)
  133. {
  134. return std::move(r.text);
  135. }
  136. else {
  137. m_http_status = r.status_code;
  138. if (r.error.message.empty())
  139. {
  140. m_error_msg = r.status_line;
  141. }
  142. else {
  143. m_error_msg = r.error.message;
  144. }
  145. return "";
  146. }
  147. return std::string("");
  148. }
  149. bool write_data(std::string /*data*/, intptr_t /*userdata*/)
  150. {
  151. return true;
  152. }
  153. bool CNetWork::Download(std::string path)
  154. {
  155. return false;
  156. /*std::string configdir = std::filesystem::current_path().string() + "\\" + CLASHCONFIGDIR;
  157. std::filesystem::path p(configdir);
  158. if (!std::filesystem::exists(p)) {
  159. std::filesystem::create_directory(p);
  160. }
  161. std::string configname = configdir + "\\" CLASHCONFIGNAME;
  162. std::ofstream ofs(configname);
  163. cpr::Url url{ path };
  164. cpr::Session session;
  165. session.SetUrl(url);
  166. cpr::Response response = session.Download(ofs);
  167. return response.status_code == 200;*/
  168. }
  169. void CNetWork::Init()
  170. {
  171. }
  172. void CNetWork::UnInit()
  173. {
  174. }