2019-07-28 10:31:56 +08:00
|
|
|
|
#include "stdafx.h"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include "http_servlet.h"
|
|
|
|
|
#include "master_service.h"
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2020-01-06 15:29:03 +08:00
|
|
|
|
char *var_cfg_libcrypto_path;
|
|
|
|
|
char *var_cfg_libx509_path;
|
2020-01-03 17:01:51 +08:00
|
|
|
|
char *var_cfg_libssl_path;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
char *var_cfg_crt_file;
|
|
|
|
|
char *var_cfg_key_file;
|
|
|
|
|
acl::master_str_tbl var_conf_str_tab[] = {
|
2020-01-06 15:29:03 +08:00
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
{ "libcrypto_path", "./libmbedcrypto.dylib", &var_cfg_libcrypto_path },
|
|
|
|
|
{ "libx509_path", "./libmbedx509.dylib", &var_cfg_libx509_path },
|
|
|
|
|
{ "libssl_path", "./libmbedtls.dylib", &var_cfg_libssl_path },
|
|
|
|
|
#else
|
|
|
|
|
{ "libcrypto_path", "./libmbedcrypto.so", &var_cfg_libcrypto_path },
|
|
|
|
|
{ "libx509_path", "./libmbedx509.so", &var_cfg_libx509_path },
|
2022-08-20 21:32:08 +08:00
|
|
|
|
{ "libssl_path", "./libssl.so", &var_cfg_libssl_path },
|
2020-01-06 15:29:03 +08:00
|
|
|
|
#endif
|
2015-12-08 22:28:10 +08:00
|
|
|
|
{ "crt_file", "./ssl_crt.pem", &var_cfg_crt_file },
|
|
|
|
|
{ "key_file", "./ssl_key.pem", &var_cfg_key_file },
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int var_cfg_session_cache;
|
|
|
|
|
acl::master_bool_tbl var_conf_bool_tab[] = {
|
|
|
|
|
{ "session_cache", 1, &var_cfg_session_cache },
|
|
|
|
|
|
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int var_cfg_io_timeout;
|
|
|
|
|
acl::master_int_tbl var_conf_int_tab[] = {
|
|
|
|
|
{ "io_timeout", 60, &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()
|
|
|
|
|
: conf_(NULL)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
master_service::~master_service()
|
|
|
|
|
{
|
2020-01-05 10:32:38 +08:00
|
|
|
|
if (conf_) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
delete conf_;
|
2020-01-05 10:32:38 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 17:01:51 +08:00
|
|
|
|
static acl::sslbase_io* setup_ssl(acl::socket_stream& conn, acl::sslbase_conf& conf)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2020-01-03 17:01:51 +08:00
|
|
|
|
acl::sslbase_io* hook = (acl::sslbase_io*) conn.get_hook();
|
|
|
|
|
if (hook != NULL) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
return hook;
|
2020-01-03 17:01:51 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +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>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2015-12-08 22:28:10 +08:00
|
|
|
|
//logger("begin setup ssl hook...");
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SSL <20><><EFBFBD>ַ<EFBFBD>ʽ
|
2020-07-16 09:12:39 +08:00
|
|
|
|
acl::sslbase_io* ssl = conf.create(false);
|
2020-01-03 17:01:51 +08:00
|
|
|
|
if (conn.setup_hook(ssl) == ssl) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
logger_error("setup_hook error!");
|
|
|
|
|
ssl->destroy();
|
2014-11-30 21:15:35 +08:00
|
|
|
|
return NULL;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 17:01:51 +08:00
|
|
|
|
if (!ssl->handshake()) {
|
2015-12-08 22:28:10 +08:00
|
|
|
|
logger_error("ssl handshake failed");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 17:01:51 +08:00
|
|
|
|
if (!ssl->handshake_ok()) {
|
2015-12-08 22:28:10 +08:00
|
|
|
|
logger("handshake trying again...");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger("handshake_ok");
|
|
|
|
|
|
2014-11-30 21:15:35 +08:00
|
|
|
|
return ssl;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool master_service::thread_on_read(acl::socket_stream* conn)
|
|
|
|
|
{
|
|
|
|
|
http_servlet* servlet = (http_servlet*) conn->get_ctx();
|
2020-01-05 10:32:38 +08:00
|
|
|
|
if (servlet == NULL) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
logger_fatal("servlet null!");
|
2020-01-05 10:32:38 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2020-01-05 10:32:38 +08:00
|
|
|
|
if (conf_ == NULL) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return servlet->doRun("127.0.0.1:11211", conn);
|
2020-01-05 10:32:38 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2020-01-03 17:01:51 +08:00
|
|
|
|
acl::sslbase_io* ssl = setup_ssl(*conn, *conf_);
|
2020-01-05 10:32:38 +08:00
|
|
|
|
if (ssl == NULL) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
return false;
|
2020-01-05 10:32:38 +08:00
|
|
|
|
}
|
2014-11-30 21:15:35 +08:00
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return servlet->doRun("127.0.0.1:11211", conn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool master_service::thread_on_accept(acl::socket_stream* conn)
|
|
|
|
|
{
|
|
|
|
|
logger("connect from %s, fd: %d", conn->get_peer(true),
|
|
|
|
|
conn->sock_handle());
|
|
|
|
|
|
|
|
|
|
conn->set_rw_timeout(var_cfg_io_timeout);
|
|
|
|
|
|
|
|
|
|
http_servlet* servlet = new http_servlet();
|
|
|
|
|
conn->set_ctx(servlet);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool master_service::thread_on_timeout(acl::socket_stream* conn)
|
|
|
|
|
{
|
|
|
|
|
logger("read timeout from %s, fd: %d", conn->get_peer(),
|
|
|
|
|
conn->sock_handle());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::thread_on_close(acl::socket_stream* conn)
|
|
|
|
|
{
|
|
|
|
|
logger("disconnect from %s, fd: %d", conn->get_peer(),
|
|
|
|
|
conn->sock_handle());
|
|
|
|
|
|
|
|
|
|
http_servlet* servlet = (http_servlet*) conn->get_ctx();
|
2020-01-05 10:32:38 +08:00
|
|
|
|
delete servlet;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::thread_on_init()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::thread_on_exit()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::proc_on_init()
|
|
|
|
|
{
|
|
|
|
|
if (var_cfg_crt_file == NULL || *var_cfg_crt_file == 0
|
2020-01-03 17:01:51 +08:00
|
|
|
|
|| var_cfg_key_file == NULL || *var_cfg_key_file == 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-20 21:32:08 +08:00
|
|
|
|
bool use_polarssl = false;
|
|
|
|
|
|
2020-01-03 17:01:51 +08:00
|
|
|
|
if (strstr(var_cfg_libssl_path, "mbedtls")) {
|
2020-01-06 15:29:03 +08:00
|
|
|
|
acl::mbedtls_conf::set_libpath(var_cfg_libcrypto_path,
|
|
|
|
|
var_cfg_libx509_path, var_cfg_libssl_path);
|
2020-01-03 17:01:51 +08:00
|
|
|
|
if (!acl::mbedtls_conf::load()) {
|
|
|
|
|
logger_error("load %s error", var_cfg_libssl_path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conf_ = new acl::mbedtls_conf(true);
|
|
|
|
|
} 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conf_ = new acl::polarssl_conf();
|
2022-08-20 21:32:08 +08:00
|
|
|
|
use_polarssl = true;
|
2022-08-21 17:34:05 +08:00
|
|
|
|
} else if (strstr(var_cfg_libssl_path, "libssl")) {
|
2022-08-22 22:03:38 +08:00
|
|
|
|
acl::openssl_conf::set_libpath(var_cfg_libcrypto_path,
|
|
|
|
|
var_cfg_libssl_path);
|
|
|
|
|
if (!acl::openssl_conf::load()) {
|
|
|
|
|
logger_error("load ssl error, crypto=%s, ssl=%s",
|
|
|
|
|
var_cfg_libcrypto_path, var_cfg_libssl_path);
|
|
|
|
|
}
|
2022-08-20 21:32:08 +08:00
|
|
|
|
conf_ = new acl::openssl_conf(true);
|
|
|
|
|
} else {
|
|
|
|
|
logger_error("not support this ssl lib=%s!", var_cfg_libssl_path);
|
|
|
|
|
exit (1);
|
2020-01-03 17:01:51 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD> SSL <20>Ự<EFBFBD><E1BBB0><EFBFBD>湦<EFBFBD><E6B9A6>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
conf_->enable_cache(var_cfg_session_cache);
|
|
|
|
|
|
2022-08-20 21:32:08 +08:00
|
|
|
|
if (use_polarssl) {
|
|
|
|
|
// <20><><EFBFBD>ӱ<EFBFBD><D3B1>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4>
|
|
|
|
|
if (!conf_->add_cert(var_cfg_crt_file)) {
|
|
|
|
|
logger_error("add cert failed, crt: %s, key: %s",
|
|
|
|
|
var_cfg_crt_file, var_cfg_key_file);
|
|
|
|
|
delete conf_;
|
|
|
|
|
conf_ = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ӱ<EFBFBD><D3B1>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>Կ
|
|
|
|
|
if (!conf_->set_key(var_cfg_key_file)) {
|
|
|
|
|
logger_error("set private key error");
|
|
|
|
|
delete conf_;
|
|
|
|
|
conf_ = NULL;
|
|
|
|
|
}
|
|
|
|
|
} else if (!conf_->add_cert(var_cfg_crt_file, var_cfg_key_file)) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
logger_error("add cert failed, crt: %s, key: %s",
|
|
|
|
|
var_cfg_crt_file, var_cfg_key_file);
|
|
|
|
|
delete conf_;
|
|
|
|
|
conf_ = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-20 21:32:08 +08:00
|
|
|
|
logger("load cert ok, crt: %s, key: %s", var_cfg_crt_file, var_cfg_key_file);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::proc_on_exit()
|
|
|
|
|
{
|
|
|
|
|
}
|