Kylin/HttpProxy/NetworkUtility.h

120 lines
3.9 KiB
C
Raw Normal View History

2024-01-14 17:54:26 +08:00
#ifndef __NETWORKUTILITY_H__
2023-07-21 11:53:19 +08:00
#define __NETWORKUTILITY_H__
#include "Singleton.h"
#include <boost/asio/io_context.hpp>
2024-01-14 17:54:26 +08:00
#include <boost/beast/http/string_body.hpp>
2023-07-21 11:53:19 +08:00
#include <boost/system/error_code.hpp>
#include <string>
#include <string_view>
2024-01-14 17:54:26 +08:00
namespace boost {
namespace asio {
namespace ssl {
class context;
}
} // namespace asio
} // namespace boost
2023-07-21 11:53:19 +08:00
class Network {
friend class Amass::Singleton<Network, Amass::GlobalInstance>;
public:
static bool isConnected(const std::string_view &host, size_t size);
static uint64_t htonll(uint64_t val);
static uint64_t ntohll(uint64_t val);
protected:
Network(int argc, char *argv[]);
~Network();
};
2024-01-14 17:54:26 +08:00
namespace Http {
using Request = boost::beast::http::request<boost::beast::http::string_body>;
enum Type {
Transparent,
SSL,
};
enum CertificateLocation {
None,
Default,
SystemDependent,
};
enum Version : unsigned {
Version_1_1 = 11,
};
struct ParseItem {
std::string filename;
// std::string::const_iterator begin;
// std::string::const_iterator end;
size_t begin;
size_t end;
};
2023-07-21 11:53:19 +08:00
2024-01-17 11:56:24 +08:00
/**
* @brief
*
* @param buffer
* @return std::vector<ParseItem>
* @example auto items = parseFormData(request.body());
* for (auto &item : items) {
* std::string filename("E:/");
* filename += item.filename;
* std::ofstream ofs(filename.c_str(), std::ios::binary);
* ofs.write(buffer.data() + item.begin, item.end - item.begin);
* ofs.close();
* }
*/
static std::vector<ParseItem> parseFormData(const std::string_view &buffer);
class ClientPrivate;
class Client {
public:
Client(boost::asio::io_context &ioContext, Type type, Version version = Version_1_1);
~Client();
void loadRootCertificates(boost::system::error_code &error);
std::string get(const std::string &host, const std::string &port, const std::string &url,
boost::system::error_code &error);
std::string post(const std::string &host, const std::string &port, const std::string &url, const std::string &body,
boost::system::error_code &error);
std::string put(const std::string &host, const std::string &port, const std::string &url, const std::string &body,
boost::system::error_code &error);
void addRequestField(boost::beast::http::field name, const std::string &value);
2024-01-14 17:54:26 +08:00
2024-01-17 11:56:24 +08:00
protected:
std::string execute(const std::string &host, const std::string &port, const Request &request,
boost::system::error_code &error);
void connect(const std::string &host, const std::string &port, boost::system::error_code &error);
void shutdown(boost::system::error_code &error);
private:
ClientPrivate *m_d = nullptr;
boost::asio::io_context &m_ioContext;
std::shared_ptr<boost::asio::ssl::context> m_sslContext;
CertificateLocation m_certificateLocation = CertificateLocation::None;
std::string m_host;
std::string m_port;
Request m_request;
};
2024-01-14 17:54:26 +08:00
2024-01-17 11:56:24 +08:00
} // namespace Http
2023-07-21 11:53:19 +08:00
class Https {
public:
2024-01-14 17:54:26 +08:00
static std::string get(boost::asio::io_context &ioContext, const std::string &host, const std::string &port,
const std::string &url, boost::system::error_code &error,
2023-07-21 11:53:19 +08:00
Http::Version version = Http::Version_1_1);
2024-01-14 17:54:26 +08:00
static std::string post(boost::asio::io_context &ioContext, const std::string &host, const std::string &port,
const std::string &url, const std::string &body, boost::system::error_code &error,
Http::Version version = Http::Version_1_1);
2023-07-21 11:53:19 +08:00
static std::string put(boost::asio::io_context &ioContext, const std::string &host, const std::string &port,
const std::string &url, const std::string &body, boost::system::error_code &error,
Http::Version version = Http::Version_1_1);
};
2024-01-14 17:54:26 +08:00
#endif // __NETWORKUTILITY_H__