【问题标题】:c++ boost socket proxy-server add SSL时间:2018-10-10 标签:c++boostsocketproxy-server add SSL
【发布时间】:2019-04-16 17:24:17
【问题描述】:

我正在使用带有 boost 库的 http 代理服务器。喜欢: WebBrowser Socket-ProxyServer http 服务器。它工作正常。但我有一个 SSL 证书,需要将其添加到我的代理服务器中。我的代码是:

代理服务器.cpp:

    server::server(const ios_deque& io_services, int port, std::string interface_address, int num)
    : io_services_(io_services),
      endpoint_(interface_address.empty()?  
                (ba::ip::tcp::endpoint(ba::ip::tcp::v4(), port)): 
                ba::ip::tcp::endpoint(ba::ip::address().from_string(interface_address), port) ),    
      acceptor_(*io_services.front(), endpoint_)    
{
    start_accept(num);
}

void server::start_accept(int num) {
    io_services_.push_back(io_services_.front());
    io_services_.pop_front();
    connection::pointer new_connection = connection::create(*io_services_.front());
        acceptor_.async_accept(new_connection->socket(),
                           boost::bind(&server::handle_accept, this, new_connection,
                                       ba::placeholders::error, num));

        }

void server::handle_accept(connection::pointer new_connection, const bs::error_code& error, int num) {
    if (!error) {
            new_connection->start(GlobalSett.CheckIp(inet_addr(new_connection->socket().remote_endpoint().address().to_string().c_str())), num);

            start_accept(num); 
    }

}

代理服务器.hpp:

typedef std::deque<io_service_ptr> ios_deque;

class server {
public:
    server(const ios_deque& io_services, int port, std::string interface_address, int);

private:
    void start_accept(int);
    void handle_accept(connection::pointer new_connection, const bs::error_code& error, int);

    ios_deque io_services_;
    const ba::ip::tcp::endpoint endpoint_;   
    ba::ip::tcp::acceptor acceptor_;         
    function FUNC;
};

并启动服务器:

try {
        int thread_num = atoi(GlobalSett.GetSetConfig("PROXY_THREAD[" + std::to_string(num) + "]")), port = atoi(GlobalSett.GetSetConfig("PROXY_LISTNER_PORT["+std::to_string(num)+"]"));
        std::string interface_address = GlobalSett.GetSetConfig("PROXY_LISTNER_IP["+ std::to_string(num)+"]");

        ios_deque io_services;
        std::deque<ba::io_service::work> io_service_work;

        boost::thread_group thr_grp;

        for (int i = 0; i < thread_num; ++i) {
            io_service_ptr ios(new ba::io_service);
            io_services.push_back(ios);
            io_service_work.push_back(ba::io_service::work(*ios));
            thr_grp.create_thread(boost::bind(&ba::io_service::run, ios));
        }
        server server(io_services, port, interface_address, num);
        thr_grp.join_all();
    }
    catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
    }

需要将 ssl 添加到服务器套接字。任何人都可以帮助解决这个问题?谢谢!

【问题讨论】:

    标签: c++ sockets boost


    【解决方案1】:

    您需要使用boost::asio::ssl:stream。查看 boost asio SSL 示例:https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/example/cpp03/ssl/server.cpp

    如果您需要在 boost::asio::ssl::stream 中未公开的 SSL 功能,您可以随时使用 native_handle():

    boost::asio::ssl::stream<asio:ip::tcp::socket> sock(io_context, ctx);
    ...
    SSL_use_certificate(sock.native_handle(), x509_certificate);
    ...
    

    【讨论】:

    • 请阅读help pages,请阅读SO tour,阅读有关how to ask good questions的信息。我们可以帮助您解决具体问题,但不能编写您的代码。如果您发现它们有用,也可以为答案投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2013-02-18
    • 2023-03-11
    • 1970-01-01
    • 2014-11-13
    • 2017-09-02
    相关资源
    最近更新 更多