我正在将HTTP代理服务器与Boost库一起使用。喜欢: WebBrowser <-> Socket-ProxyServer <-> http服务器。很好但是我有一个SSL证书,需要将其添加到我的代理服务器中。我的代码是:
proxy-server.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);
}
}
proxy-server.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添加到服务器套接字。有人可以帮忙吗?谢谢!
答案 0 :(得分:0)
您需要使用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);
...