acl/app/wizard_demo/httpd_proxy/master_service.cpp

234 lines
6.0 KiB
C++
Raw Normal View History

#include "stdafx.h"
2019-03-14 15:24:11 +08:00
#include "http_servlet.h"
#include "master_service.h"
2022-08-26 00:12:26 +08:00
char *var_cfg_libcrypto_path; // For OpenSSL, MbedTLS
char *var_cfg_libx509_path; // For MbedTLS
char *var_cfg_libssl_path; // For OpenSSL, MbedTLS, and PolarSSL
char *var_cfg_crt_file; // For OpenSSL, MbedTLS, and PolarSSL
char *var_cfg_key_file; // For OpenSSL, MbedTLS, and PolarSSL
char *var_cfg_key_pass; // For OpenSSL, MbedTLS, and PolarSSL
char *var_cfg_https_port;
2019-03-14 15:24:11 +08:00
acl::master_str_tbl var_conf_str_tab[] = {
2022-08-26 00:12:26 +08:00
{ "libcrypto_path", "", &var_cfg_libcrypto_path },
{ "libx509_path", "", &var_cfg_libx509_path },
{ "libssl_path", "", &var_cfg_libssl_path },
{ "crt_file", "", &var_cfg_crt_file },
{ "key_file", "", &var_cfg_key_file },
{ "key_pass", "", &var_cfg_key_pass },
{ "https_port", "443", &var_cfg_https_port },
2019-03-14 15:24:11 +08:00
{ 0, 0, 0 }
};
static int var_cfg_debug_enable;
acl::master_bool_tbl var_conf_bool_tab[] = {
{ "debug_enable", 1, &var_cfg_debug_enable },
{ 0, 0, 0 }
};
static int var_cfg_io_timeout;
acl::master_int_tbl var_conf_int_tab[] = {
{ "io_timeout", 120, &var_cfg_io_timeout, 0, 0 },
{ 0, 0 , 0 , 0, 0 }
};
acl::master_int64_tbl var_conf_int64_tab[] = {
{ 0, 0 , 0 , 0, 0 }
};
//////////////////////////////////////////////////////////////////////////
master_service::master_service(void)
2022-08-26 17:51:41 +08:00
: server_conf_(NULL)
, client_conf_(NULL)
2019-03-14 15:24:11 +08:00
{
}
master_service::~master_service(void)
{
2022-08-26 17:51:41 +08:00
delete server_conf_;
delete client_conf_;
2019-03-14 15:24:11 +08:00
}
2022-08-26 00:12:26 +08:00
acl::sslbase_io* master_service::setup_ssl(acl::socket_stream& conn,
2022-08-26 17:51:41 +08:00
acl::sslbase_conf& server_conf)
2022-08-26 00:12:26 +08:00
{
acl::sslbase_io* hook = (acl::sslbase_io*) conn.get_hook();
if (hook != NULL) {
return hook;
}
2022-09-06 09:48:25 +08:00
// <20><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> SSL <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA> SSL IO <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD> ssl io <20>滻 stream <20><>Ĭ<EFBFBD>ϵĵײ<C4B5> IO <20><><EFBFBD><EFBFBD>
2022-08-26 00:12:26 +08:00
2022-08-29 17:09:02 +08:00
logger_debug(DEBUG_SSL, 2, "begin setup ssl hook...");
2022-08-26 00:12:26 +08:00
2022-09-06 09:48:25 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SSL <20><><EFBFBD>ַ<EFBFBD>ʽ
2022-08-26 17:51:41 +08:00
acl::sslbase_io* ssl = server_conf.create(false);
2022-08-26 00:12:26 +08:00
if (conn.setup_hook(ssl) == ssl) {
logger_error("setup_hook error!");
ssl->destroy();
return NULL;
}
if (!ssl->handshake()) {
logger_error("ssl handshake failed");
2022-08-26 21:42:14 +08:00
ssl->destroy();
2022-08-26 00:12:26 +08:00
return NULL;
}
if (!ssl->handshake_ok()) {
2022-08-26 21:42:14 +08:00
logger_error("handshake trying again...");
ssl->destroy();
2022-08-26 00:12:26 +08:00
return NULL;
}
2022-08-29 17:09:02 +08:00
logger_debug(DEBUG_SSL, 2, "handshake_ok");
2022-08-26 00:12:26 +08:00
return ssl;
}
2019-03-14 15:24:11 +08:00
void master_service::on_accept(acl::socket_stream& conn)
{
2022-08-29 17:09:02 +08:00
logger_debug(DEBUG_CONN, 2, "connect from %s, fd %d",
conn.get_peer(), conn.sock_handle());
2019-03-14 15:24:11 +08:00
2022-08-26 00:12:26 +08:00
const char* local = conn.get_local(true);
if (local == NULL || *local == 0) {
logger_error("get_local null from fd=%d", conn.sock_handle());
return;
}
2022-08-26 17:51:41 +08:00
bool use_ssl = false;
2022-08-26 00:12:26 +08:00
acl::string buf(local);
2022-08-26 17:51:41 +08:00
if (buf.end_with(var_cfg_https_port) && server_conf_) {
if (setup_ssl(conn, *server_conf_) == NULL) {
2022-08-26 00:12:26 +08:00
logger_error("setup ssl error");
return;
}
2022-08-26 17:51:41 +08:00
use_ssl = true;
} else {
2022-08-29 17:09:02 +08:00
//printf("local=%s, https=%s, server_conf_=%p\n",
// local, var_cfg_https_port, server_conf_);
2022-08-26 00:12:26 +08:00
}
2022-08-26 21:42:14 +08:00
conn.set_rw_timeout(5);
2019-03-14 15:24:11 +08:00
acl::memcache_session* session = new acl::memcache_session("127.0.0.1:11211");
2022-08-26 17:51:41 +08:00
http_servlet* servlet = new http_servlet(&conn, session,
use_ssl ? client_conf_ : NULL);
2019-03-14 15:24:11 +08:00
// charset: big5, gb2312, gb18030, gbk, utf-8
servlet->setLocalCharset("utf-8");
servlet->setParseBody(false);
2019-03-14 15:24:11 +08:00
while(servlet->doRun()) {}
2019-03-14 15:24:11 +08:00
2022-08-29 17:09:02 +08:00
logger_debug(DEBUG_CONN, 2, "disconnect from %s, fd %d",
conn.get_peer(), conn.sock_handle());
delete session;
delete servlet;
2019-03-14 15:24:11 +08:00
}
void master_service::proc_pre_jail(void)
{
logger(">>>proc_pre_jail<<<");
}
void master_service::proc_on_listen(acl::server_socket& ss)
{
logger(">>>listen %s ok<<<", ss.get_addr());
}
void master_service::proc_on_init(void)
{
2022-04-02 16:31:12 +08:00
logger(">>>proc_on_init: shared stack size=%zd<<<",
acl::fiber::get_shared_stack_size());
2022-08-26 00:12:26 +08:00
2022-09-06 09:48:25 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC> SSL <20><><EFBFBD><EFBFBD>
2022-08-26 00:12:26 +08:00
if (var_cfg_crt_file == NULL || *var_cfg_crt_file == 0
|| var_cfg_key_file == NULL || *var_cfg_key_file == 0) {
logger("not use SSL mode");
return;
}
if (strstr(var_cfg_libssl_path, "mbedtls")) {
acl::mbedtls_conf::set_libpath(var_cfg_libcrypto_path,
var_cfg_libx509_path, var_cfg_libssl_path);
if (!acl::mbedtls_conf::load()) {
logger_error("load %s error", var_cfg_libssl_path);
return;
}
logger("MbedTLS loaded, crypto=%s, x509=%s, ssl=%s",
var_cfg_libcrypto_path, var_cfg_libx509_path,
var_cfg_libssl_path);
2022-08-26 17:51:41 +08:00
server_conf_ = new acl::mbedtls_conf(true);
client_conf_ = new acl::mbedtls_conf(false);
2022-08-26 00:12:26 +08:00
} else if (strstr(var_cfg_libssl_path, "polarssl")) {
acl::polarssl_conf::set_libpath(var_cfg_libssl_path);
if (!acl::polarssl_conf::load()) {
logger_error("load %s error", var_cfg_libssl_path);
return;
}
logger("PolarSSL loaded, ssl=%s", var_cfg_libssl_path);
2022-08-26 17:51:41 +08:00
server_conf_ = new acl::polarssl_conf();
client_conf_ = new acl::polarssl_conf();
2022-08-26 00:12:26 +08:00
} else if (strstr(var_cfg_libssl_path, "libssl")) {
acl::openssl_conf::set_libpath(var_cfg_libcrypto_path,
var_cfg_libssl_path);
if (!acl::openssl_conf::load()) {
logger_error("load %s error", var_cfg_libssl_path);
return;
}
logger("OpenSSL loaded, crypto=%s, ssl=%s",
var_cfg_libcrypto_path, var_cfg_libssl_path);
2022-08-29 18:40:15 +08:00
server_conf_ = new acl::openssl_conf(true, 5);
client_conf_ = new acl::openssl_conf(false, 5);
2022-08-26 00:12:26 +08:00
} else {
logger("unsupported ssl=%s", var_cfg_libssl_path);
return;
}
2022-09-06 09:48:25 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD> SSL <20><EFBFBD><E1BBB0><EFBFBD><EFBFBD><E6B9A6>
2022-08-26 17:51:41 +08:00
//server_conf_->enable_cache(var_cfg_ssl_session_cache);
2022-08-26 00:12:26 +08:00
2022-09-06 09:48:25 +08:00
// <20><><EFBFBD>ӱ<EFBFBD><D3B1>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD><E9BCB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ
2022-08-26 17:51:41 +08:00
if (!server_conf_->add_cert(var_cfg_crt_file, var_cfg_key_file,
2022-08-26 00:12:26 +08:00
var_cfg_key_pass)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
2022-08-26 17:51:41 +08:00
delete server_conf_;
server_conf_ = NULL;
2022-08-26 00:12:26 +08:00
return;
}
logger("load cert ok, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
2019-03-14 15:24:11 +08:00
}
void master_service::proc_on_exit(void)
{
logger(">>>proc_on_exit<<<");
}
bool master_service::proc_on_sighup(acl::string&)
{
logger(">>>proc_on_sighup<<<");
return true;
}