acl/app/wizard_demo/dns-gate/tools/status/http_status.cpp
2022-09-11 16:24:01 +08:00

183 lines
3.8 KiB
C++

#include "stdafx.h"
#include "json/struct.h"
#include "json/struct.gson.h"
#include "acl_cpp/serialize/serialize.hpp"
#include "http_status.h"
http_status::http_status(const char* addr, acl::sslbase_conf& ssl_conf,
const char* user, const char* pass)
: addr_(addr)
, ssl_conf_(ssl_conf)
, user_(user)
, pass_(pass)
{
}
http_status::~http_status(void) {}
void http_status::build_request(acl::string& buf) {
request_t req;
acl::serialize<request_t>(req, buf);
}
bool http_status::parse_response(const acl::string& data, response_t& res) {
acl::json json;
json.update(data);
if (!json.finish()) {
printf("invalid json=%s\r\n", data.c_str());
return false;
}
acl::string err;
if (!acl::deserialize<response_t>(json, res, &err)) {
printf("parse json=%s\r\n\r\nerr=%s\r\n", data.c_str(), err.c_str());
return false;
}
return true;
}
void http_status::show_status(const response_t& res) {
#if 0
if (res.host_management.host_info.empty()) {
printf("host_info empty!\r\n");
return;
}
const res_host_info_t& host_info = res.host_management.host_info[0];
const host_info_t& info = host_info.host_info_1;
printf("hostname=%s, ip=%s, mac=%s, up_limit=%s, down_limit=%s\r\n",
info.hostname.c_str(),
info.ip.c_str(),
info.mac.c_str(),
info.up_limit.c_str(),
info.down_limit.c_str());
#else
if (res.wireless.sta_list.empty()) {
printf("sta_list empty!\r\n");
return;
}
for (auto it : res.wireless.sta_list) {
if (it.sta_list_1.name.empty()) {
continue;
}
logger("name=%s, mac=%s, ip=%s, tx_rate=%s, rx_rate=%s",
it.sta_list_1.name.c_str(),
it.sta_list_1.mac.c_str(),
it.sta_list_1.ip.c_str(),
it.sta_list_1.tx_rate.c_str(),
it.sta_list_1.rx_rate.c_str());
}
#endif
}
bool http_status::get_status(const char* stok) {
acl::http_request conn(addr_);
conn.set_ssl(&ssl_conf_);
acl::string url;
url << "/stok=" << stok << "/ds";
acl::string req;
build_request(req);
conn.request_header().set_url(url)
.set_host(addr_)
.set_content_type("application/json; charset=UTF-8")
.add_entry("Origin", "https://192.168.1.65/")
.add_entry("Referer", "https://192.168.1.65/")
.set_content_length(req.size());
//printf("url=%s\r\n", url.c_str());
//printf("req=%s\r\n", req.c_str());
if (!conn.request(req, req.size())) {
printf("send request error, req=%s\r\n", req.c_str());
return false;
}
acl::string buf;
if (!conn.get_body(buf)) {
printf("get response error\r\n");
return false;
}
//printf("%s\n", buf.c_str());
response_t res;
if (!parse_response(buf, res)) {
printf("parse response error\r\n");
return false;
}
show_status(res);
return true;
}
bool http_status::login(acl::string& out) {
login_req_t req;
req.method = "do";
req.login.username = user_;
req.login.password = pass_;
acl::string buf;
acl::serialize<login_req_t>(req, buf);
acl::string url;
url += "/";
acl::http_request conn(addr_);
conn.set_ssl(&ssl_conf_);
conn.request_header().set_url(url)
.set_host(addr_)
.set_content_type("application/json");
if (!conn.request(buf, buf.size())) {
printf("send login request error, req=%s\r\n", buf.c_str());
return false;
}
acl::json json;
if (!conn.get_body(json)) {
printf("get login res error\r\n");
return false;
}
//////////////////////////////////////////////////////////////////////
acl::http_client* client = conn.get_client();
client->print_header("--response--");
//////////////////////////////////////////////////////////////////////
login_res_t res;
acl::string err;
if (!acl::deserialize<login_res_t>(json, res, &err)) {
printf("parse login res error=%s\r\n", err.c_str());
return false;
}
printf("login ok, stok=%s\r\n", res.stok.c_str());
out = res.stok;
return true;
}
bool http_status::start(void) {
acl::string stok;
if (!login(stok)) {
printf("login error\r\n");
return false;
}
while (true) {
get_status(stok);
sleep(1);
}
return true;
}