123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #include "stdafx.h"
- #include "CNetWork.h"
- #include <nlohmann/json.hpp>
- #include <fmt/format.h>
- CNetWork::CNetWork() : m_http_ret(http_f)
- {
- }
- CNetWork::~CNetWork(void)
- {
- }
- int CNetWork::GetHttpStatus() {
- return m_http_status;
- }
- HTTPRET CNetWork::Version(std::string& data) {
- std::vector<cpr::Parameter> p;
- p.push_back({ "tag","win" });
- p.push_back({ "appverion",S_CW2A(VERSION).GetBuffer(0)});
- std::string text = GetUrl("/api/client/v3/version", p, CApp::getSingletonPtr()->GetUserinfo()->access_token);
- if (text.empty()) {
- return http_f;
- }
- data = text.c_str();
- return http_yes;
- }
- HTTPRET CNetWork::Refresh(std::string& data) {
- std::vector<cpr::Parameter> p;
- std::string text = GetUrl("/api/client/v3/refresh", p, CApp::getSingletonPtr()->GetUserinfo()->access_token);
- if (text.empty()) {
- return http_f;
- }
- data = text.c_str();
- return http_yes;
- }
- HTTPRET CNetWork::Auth(std::string& data) {
- return http_yes;
- }
- HTTPRET CNetWork::GetServerNode(std::string& data) {
- std::vector<cpr::Parameter> p;
- std::string text = GetUrl("/api/client/v3/nodes",p,CApp::getSingletonPtr()->GetUserinfo()->access_token);
- if (text.empty()) {
- return http_f;
- }
- data = text.c_str();
- return http_yes;
- }
- HTTPRET CNetWork::PostLogin(LPCSTR username, LPCSTR password, std::string& data)
- {
- std::vector<cpr::Pair> p;
- p.push_back({ "email",username });
- p.push_back({ "password",password });
- std::string text = PostUrl("/api/client/v3/login", p);
- if (text.empty()) {
- return http_f;
- }
- data = text.c_str();
- return http_yes;
- }
- void CNetWork::SetUrl(LPCSTR url)
- {
- m_url = url;
- }
- SStringA CNetWork::GetLastErrorA()
- {
- return SStringA().Format("%s", m_error_msg.c_str()).GetBuffer(0);
- }
- SStringW CNetWork::GetLastErrorW()
- {
- return S_CA2W(m_error_msg.c_str(),CP_UTF8).GetBuffer(0);
- }
- std::string CNetWork::GetUrl(std::string path, std::vector<cpr::Parameter> parame,std::string token)
- {
- cpr::Parameters ps;
- std::vector<cpr::Parameter>::iterator it_i;
- for (it_i = parame.begin(); it_i != parame.end(); ++it_i)
- {
- ps.Add(*it_i);
- //ps(cpr::Parameter({ it->first, it->second }));
- }
- cpr::Header hander;
- if (!token.empty())
- {
- hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
- }
- else {
- hander = cpr::Header{ {"accept", "application/json"} };
- }
- auto s = fmt::format("{0}{1}", m_url, path.c_str());
- cpr::Response r;
- if (parame.empty())
- {
- r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::Timeout{ 60 * 100});
- }
- else {
- r = cpr::Get(cpr::Url{ s.c_str() }, ps, hander, cpr::Timeout{ 60 * 100 });
- }
- if (r.status_code == 200 || r.status_code == 201)
- {
- return std::move(r.text);
- }
- else {
- m_http_status = r.status_code;
- if (r.error.message.empty())
- {
- m_error_msg = r.status_line;
- }
- else {
- m_error_msg = r.error.message;
- }
-
- return "";
- }
- return std::string("");
- }
- std::string CNetWork::PostUrl(std::string path, std::vector<cpr::Pair> parame, std::string token)
- {
- auto s = fmt::format("{0}{1}", m_url, path.c_str());
- cpr::Header hander;
- if (!token.empty())
- {
- hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
- }
- else {
- hander = cpr::Header{ {"accept", "application/json"} };
- }
- cpr::Response r;
- if (parame.empty())
- {
- r = cpr::Get(cpr::Url{ s.c_str() },hander , cpr::Timeout{ 60 * 100 });
- }
- else {
- r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ parame.begin(),parame.end() }, hander, cpr::Timeout{ 60 * 100 });
- }
- if (r.status_code == 200 || r.status_code == 201)
- {
- return std::move(r.text);
- }
- else {
- m_http_status = r.status_code;
- if (r.error.message.empty())
- {
- m_error_msg = r.status_line;
- }
- else {
- m_error_msg = r.error.message;
- }
- return "";
- }
- return std::string("");
- }
- bool write_data(std::string /*data*/, intptr_t /*userdata*/)
- {
- return true;
- }
- bool CNetWork::Download(std::string path)
- {
- return false;
- /*std::string configdir = std::filesystem::current_path().string() + "\\" + CLASHCONFIGDIR;
- std::filesystem::path p(configdir);
- if (!std::filesystem::exists(p)) {
- std::filesystem::create_directory(p);
- }
- std::string configname = configdir + "\\" CLASHCONFIGNAME;
- std::ofstream ofs(configname);
- cpr::Url url{ path };
- cpr::Session session;
- session.SetUrl(url);
- cpr::Response response = session.Download(ofs);
- return response.status_code == 200;*/
- }
- void CNetWork::Init()
- {
- }
- void CNetWork::UnInit()
- {
- }
|