2022-08-20 19:31:37 +08:00
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "master_service.h"
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
char *var_cfg_crt_file;
|
|
|
|
|
char *var_cfg_key_file;
|
|
|
|
|
acl::master_str_tbl var_conf_str_tab[] = {
|
|
|
|
|
{ "crt_file", "../ssl_crt.pem", &var_cfg_crt_file },
|
|
|
|
|
{ "key_file", "../ssl_key.pem", &var_cfg_key_file },
|
|
|
|
|
|
|
|
|
|
{ 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()
|
|
|
|
|
{
|
|
|
|
|
if (conf_) {
|
|
|
|
|
delete conf_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static acl::sslbase_io* 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><C3B7><EFBFBD><EFBFBD><EFBFBD> SSL <20><><EFBFBD>ַ<EFBFBD>ʽ
|
|
|
|
|
acl::sslbase_io* ssl = conf.create(true);
|
|
|
|
|
if (conn.setup_hook(ssl) == ssl) {
|
|
|
|
|
logger_error("setup_hook error!");
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger("setup hook ok, tid: %lu", acl::thread::thread_self());
|
|
|
|
|
return ssl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool do_run(acl::socket_stream& conn, acl::sslbase_io*)
|
|
|
|
|
{
|
|
|
|
|
acl::string* buf =(acl::string*) conn.get_ctx();
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>ȡһ<C8A1>У<EFBFBD><D0A3>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> polarssl_io <20><><EFBFBD>ĵײ<C4B5>
|
|
|
|
|
// IO <20><><EFBFBD>̱<EFBFBD><CCB1>ϵ<EFBFBD>
|
|
|
|
|
if (!conn.gets_peek(buf, false)) {
|
|
|
|
|
if (conn.eof()) {
|
|
|
|
|
printf("peek error\n");
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>д<EFBFBD><D0B4><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><D7BD>ֲ<EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (conn.write(*buf) == -1) {
|
|
|
|
|
logger("write error!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf->clear();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool master_service::thread_on_read(acl::socket_stream* conn)
|
|
|
|
|
{
|
|
|
|
|
if (conf_ == NULL) {
|
|
|
|
|
return do_run(*conn, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acl::sslbase_io* ssl = setup_ssl(*conn, *conf_);
|
|
|
|
|
if (ssl == NULL) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ssl->handshake()) {
|
|
|
|
|
logger_error("ssl handshake failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ssl->handshake_ok()) {
|
|
|
|
|
logger("handshake trying again...");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//logger("handshake_ok");
|
|
|
|
|
|
|
|
|
|
return do_run(*conn, ssl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool master_service::thread_on_accept(acl::socket_stream* conn)
|
|
|
|
|
{
|
|
|
|
|
//logger("connect from %s, fd: %d, timeout: %d", conn->get_peer(true),
|
|
|
|
|
// conn->sock_handle(), conn->get_rw_timeout());
|
|
|
|
|
|
|
|
|
|
acl::string* buf = new acl::string(128);
|
|
|
|
|
conn->set_ctx(buf);
|
|
|
|
|
conn->set_rw_timeout(var_cfg_io_timeout);
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
acl::string* buf = (acl::string*) conn->get_ctx();
|
|
|
|
|
delete buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|| var_cfg_key_file == NULL || *var_cfg_key_file == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conf_ = new acl::openssl_conf(true);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD> SSL <20>Ự<EFBFBD><E1BBB0><EFBFBD>湦<EFBFBD><E6B9A6>
|
|
|
|
|
conf_->enable_cache(var_cfg_session_cache);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ӱ<EFBFBD><D3B1>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD>鼰<EFBFBD><E9BCB0><EFBFBD>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>Կ
|
2022-08-22 22:03:38 +08:00
|
|
|
|
if (!conf_->add_cert(var_cfg_crt_file, var_cfg_key_file)) {
|
2022-08-20 19:31:37 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::proc_on_exit()
|
|
|
|
|
{
|
|
|
|
|
}
|