2019-07-28 10:31:56 +08:00
|
|
|
|
#include "stdafx.h"
|
2020-06-29 14:34:16 +08:00
|
|
|
|
#include "http_service.h"
|
2020-06-29 17:17:56 +08:00
|
|
|
|
#include "http_servlet.h"
|
|
|
|
|
#include "master_service.h"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2015-09-16 21:03:02 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
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
|
|
|
|
|
2015-08-09 22:42:51 +08:00
|
|
|
|
char *var_cfg_redis_addrs;
|
2022-05-06 00:02:13 +08:00
|
|
|
|
char *var_cfg_libcrypto_path;
|
|
|
|
|
char *var_cfg_libx509_path;
|
|
|
|
|
char *var_cfg_libssl_path;
|
|
|
|
|
char *var_cfg_crt_file;
|
|
|
|
|
char *var_cfg_key_file;
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::master_str_tbl var_conf_str_tab[] = {
|
2015-08-09 22:42:51 +08:00
|
|
|
|
{ "redis_addrs", "127.0.0.1:6379", &var_cfg_redis_addrs },
|
2022-05-06 00:02:13 +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 },
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-09 22:42:51 +08:00
|
|
|
|
int var_cfg_use_redis_session;
|
2022-05-06 00:02:13 +08:00
|
|
|
|
int var_cfg_ssl_session_cache;
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::master_bool_tbl var_conf_bool_tab[] = {
|
2015-08-09 22:42:51 +08:00
|
|
|
|
{ "use_redis_session", 1, &var_cfg_use_redis_session },
|
2022-05-06 00:02:13 +08:00
|
|
|
|
{ "ssl_session_cache", 1, &var_cfg_ssl_session_cache },
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-09 22:42:51 +08:00
|
|
|
|
int var_cfg_conn_timeout;
|
|
|
|
|
int var_cfg_rw_timeout;
|
|
|
|
|
int var_cfg_max_threads;
|
2022-05-06 00:02:13 +08:00
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::master_int_tbl var_conf_int_tab[] = {
|
2015-08-09 22:42:51 +08:00
|
|
|
|
{ "rw_timeout", 120, &var_cfg_rw_timeout, 0, 0 },
|
|
|
|
|
{ "ioctl_max_threads", 128, &var_cfg_max_threads, 0, 0 },
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
{ 0, 0 , 0 , 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-09 22:42:51 +08:00
|
|
|
|
long long int var_cfg_int64;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::master_int64_tbl var_conf_int64_tab[] = {
|
|
|
|
|
{ "int64", 120, &var_cfg_int64, 0, 0 },
|
|
|
|
|
|
|
|
|
|
{ 0, 0 , 0 , 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-16 21:03:02 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
master_service::master_service(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2020-06-29 17:17:56 +08:00
|
|
|
|
redis_ = NULL;
|
2022-05-06 00:02:13 +08:00
|
|
|
|
conf_ = NULL;
|
2020-06-29 17:17:56 +08:00
|
|
|
|
service_ = new http_service;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
master_service::~master_service(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2022-05-06 00:02:13 +08:00
|
|
|
|
delete conf_;
|
2020-06-29 17:17:56 +08:00
|
|
|
|
delete service_;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
http_service& master_service::get_service(void) const
|
|
|
|
|
{
|
|
|
|
|
return *service_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool master_service::thread_on_read(acl::socket_stream* conn)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
http_servlet* servlet = (http_servlet*) conn->get_ctx();
|
2019-07-20 16:14:09 +08:00
|
|
|
|
if (servlet == NULL) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
logger_fatal("servlet null!");
|
2019-07-20 16:14:09 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2015-09-27 20:59:21 +08:00
|
|
|
|
return servlet->doRun();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 00:02:13 +08:00
|
|
|
|
acl::sslbase_io* master_service::setup_ssl(acl::socket_stream& conn,
|
|
|
|
|
acl::sslbase_conf& conf)
|
|
|
|
|
{
|
|
|
|
|
acl::sslbase_io* hook = (acl::sslbase_io*) conn.get_hook();
|
|
|
|
|
if (hook != NULL) {
|
|
|
|
|
return hook;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <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>
|
|
|
|
|
|
|
|
|
|
//logger("begin setup ssl hook...");
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SSL <20><><EFBFBD>ַ<EFBFBD>ʽ
|
|
|
|
|
acl::sslbase_io* ssl = conf.create(false);
|
|
|
|
|
if (conn.setup_hook(ssl) == ssl) {
|
|
|
|
|
logger_error("setup_hook error!");
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ssl->handshake()) {
|
|
|
|
|
logger_error("ssl handshake failed");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ssl->handshake_ok()) {
|
|
|
|
|
logger("handshake trying again...");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger("handshake_ok");
|
|
|
|
|
|
|
|
|
|
return ssl;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
bool master_service::thread_on_accept(acl::socket_stream* conn)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
logger("connect from %s, fd: %d", conn->get_peer(true),
|
|
|
|
|
conn->sock_handle());
|
2016-11-18 21:45:40 +08:00
|
|
|
|
|
2022-05-08 19:19:28 +08:00
|
|
|
|
if (conf_) {
|
|
|
|
|
acl::sslbase_io* ssl = setup_ssl(*conn, *conf_);
|
|
|
|
|
if (ssl == NULL) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-05-06 00:02:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-09 22:42:51 +08:00
|
|
|
|
conn->set_rw_timeout(var_cfg_rw_timeout);
|
2019-07-20 16:14:09 +08:00
|
|
|
|
if (var_cfg_rw_timeout > 0) {
|
2016-11-18 21:45:40 +08:00
|
|
|
|
conn->set_tcp_non_blocking(true);
|
2019-07-20 16:14:09 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2015-09-27 20:59:21 +08:00
|
|
|
|
acl::session* session;
|
2019-07-20 16:14:09 +08:00
|
|
|
|
if (var_cfg_use_redis_session) {
|
2020-07-20 11:21:47 +08:00
|
|
|
|
session = new acl::redis_session(*redis_);
|
2019-07-20 16:14:09 +08:00
|
|
|
|
} else {
|
2015-09-27 20:59:21 +08:00
|
|
|
|
session = new acl::memcache_session("127.0.0.1:11211");
|
2019-07-20 16:14:09 +08:00
|
|
|
|
}
|
2015-09-27 20:59:21 +08:00
|
|
|
|
|
2020-08-21 14:51:09 +08:00
|
|
|
|
http_servlet* servlet = new http_servlet(*service_, conn, session);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
conn->set_ctx(servlet);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
bool master_service::thread_on_timeout(acl::socket_stream* conn)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
logger("read timeout from %s, fd: %d", conn->get_peer(),
|
|
|
|
|
conn->sock_handle());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
void master_service::thread_on_close(acl::socket_stream* conn)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
logger("disconnect from %s, fd: %d", conn->get_peer(),
|
|
|
|
|
conn->sock_handle());
|
|
|
|
|
|
|
|
|
|
http_servlet* servlet = (http_servlet*) conn->get_ctx();
|
2015-09-27 20:59:21 +08:00
|
|
|
|
acl::session* session = &servlet->getSession();
|
|
|
|
|
delete session;
|
|
|
|
|
delete servlet;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
void master_service::thread_on_init(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
void master_service::thread_on_exit(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
void master_service::proc_on_listen(acl::server_socket& ss)
|
2016-12-03 20:09:21 +08:00
|
|
|
|
{
|
|
|
|
|
logger(">>>listen %s ok<<<", ss.get_addr());
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
void master_service::proc_on_init(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2015-08-09 22:42:51 +08:00
|
|
|
|
// create redis cluster for session cluster
|
2015-12-20 21:24:28 +08:00
|
|
|
|
redis_ = new acl::redis_client_cluster;
|
|
|
|
|
redis_->init(NULL, var_cfg_redis_addrs, var_cfg_max_threads,
|
|
|
|
|
var_cfg_conn_timeout, var_cfg_rw_timeout);
|
2022-05-06 00:02:13 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC> SSL <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
if (var_cfg_crt_file == NULL || *var_cfg_crt_file == 0
|
|
|
|
|
|| var_cfg_key_file == NULL || *var_cfg_key_file == 0) {
|
2022-05-08 19:19:28 +08:00
|
|
|
|
logger("not use SSL mode");
|
2022-05-06 00:02:13 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD> SSL <20>Ự<EFBFBD><E1BBB0><EFBFBD>湦<EFBFBD><E6B9A6>
|
|
|
|
|
conf_->enable_cache(var_cfg_ssl_session_cache);
|
|
|
|
|
|
|
|
|
|
// <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;
|
|
|
|
|
}
|
|
|
|
|
logger("load cert ok, crt: %s, key: %s",
|
|
|
|
|
var_cfg_crt_file, var_cfg_key_file);
|
|
|
|
|
|
|
|
|
|
// <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;
|
|
|
|
|
}
|
2015-08-09 22:42:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
void master_service::proc_on_exit(void)
|
2015-08-09 22:42:51 +08:00
|
|
|
|
{
|
2015-09-27 20:59:21 +08:00
|
|
|
|
delete redis_;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
bool master_service::proc_exit_timer(size_t nclients, size_t nthreads)
|
2014-11-25 19:44:37 +08:00
|
|
|
|
{
|
2019-07-20 16:14:09 +08:00
|
|
|
|
if (nclients == 0) {
|
2014-11-25 19:44:37 +08:00
|
|
|
|
logger("clients count: %d, threads count: %d",
|
|
|
|
|
(int) nclients, (int) nthreads);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-07-05 10:27:33 +08:00
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
bool master_service::proc_on_sighup(acl::string&)
|
2017-07-05 10:27:33 +08:00
|
|
|
|
{
|
|
|
|
|
logger(">>>proc_on_sighup<<<");
|
2017-09-09 00:30:39 +08:00
|
|
|
|
return true;
|
2017-07-05 10:27:33 +08:00
|
|
|
|
}
|