diff --git a/include/Web_Interface.hpp b/include/Web_Interface.hpp new file mode 100644 index 0000000000..65640f67e2 --- /dev/null +++ b/include/Web_Interface.hpp @@ -0,0 +1,15 @@ +#ifndef CLASS_WEB_INTERFACE +#define CLASS_WEB_INTERFACE + +class Web_Interface { + public: + Web_Interface() {}; + virtual ~Web_Interface() {}; + virtual void start(int p) {}; + virtual void stop() {}; + virtual void print_version() {}; +}; + +typedef Web_Interface * create_Web_Interface_t(); + +#endif /* CLASS_WEB_INTERFACE */ diff --git a/include/proxysql_glovars.hpp b/include/proxysql_glovars.hpp index b2ec57e352..6c4f0f46f9 100644 --- a/include/proxysql_glovars.hpp +++ b/include/proxysql_glovars.hpp @@ -57,6 +57,7 @@ class ProxySQL_GlobalVariables { char *pid; int restart_on_missing_heartbeats; char * execute_on_exit_failure; + char * web_interface_plugin; char * ldap_auth_plugin; struct { unsigned long long start_time; diff --git a/include/proxysql_structs.h b/include/proxysql_structs.h index 8e61c23999..efa51eed30 100644 --- a/include/proxysql_structs.h +++ b/include/proxysql_structs.h @@ -409,6 +409,7 @@ class ProxySQL_Cluster; class MySQL_ResultSet; class Query_Processor_Output; class MySrvC; +class Web_Interface_plugin; #endif /* PROXYSQL_CLASSES */ //#endif /* __cplusplus */ diff --git a/include/query_processor.h b/include/query_processor.h index 41c7318f91..3166768c51 100644 --- a/include/query_processor.h +++ b/include/query_processor.h @@ -374,6 +374,7 @@ class Query_Processor { SQLite3_result * get_mysql_firewall_whitelist_rules(); SQLite3_result * get_mysql_firewall_whitelist_sqli_fingerprints(); bool whitelisted_sqli_fingerprint(char *); + friend Web_Interface_plugin; }; typedef Query_Processor * create_Query_Processor_t(); diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 33cc84e5fa..dca9a5ef73 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -20,6 +20,8 @@ #include "MySQL_Logger.hpp" #include "SQLite3_Server.h" +#include "Web_Interface.hpp" + #include #include #include @@ -236,6 +238,8 @@ extern MySQL_Logger *GloMyLogger; extern MySQL_STMT_Manager_v14 *GloMyStmt; extern MySQL_Monitor *GloMyMon; +extern Web_Interface *GloWebInterface; + extern ProxySQL_Cluster *GloProxyCluster; #ifdef PROXYSQLCLICKHOUSE extern ClickHouse_Authentication *GloClickHouseAuth; @@ -5342,29 +5346,7 @@ void ProxySQL_Admin::flush_admin_variables___database_to_runtime(SQLite3DB *db, } if (variables.web_enabled != variables.web_enabled_old) { if (variables.web_enabled) { - char *key_pem; - char *cert_pem; - key_pem = load_file(ssl_key_fp); - cert_pem = load_file(ssl_cert_fp); - Admin_HTTP_Server = MHD_start_daemon(MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_SSL, - variables.web_port, - NULL, NULL, http_handler, NULL, - MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, MHD_OPTION_STRICT_FOR_CLIENT, (int) 1, - MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) 4, - MHD_OPTION_NONCE_NC_SIZE, (unsigned int) 300, - MHD_OPTION_HTTPS_MEM_KEY, key_pem, - MHD_OPTION_HTTPS_MEM_CERT, cert_pem, - MHD_OPTION_END); - } else { - MHD_stop_daemon(Admin_HTTP_Server); - Admin_HTTP_Server = NULL; - } - variables.web_enabled_old = variables.web_enabled; - } else { - if (variables.web_port != variables.web_port_old) { - if (variables.web_enabled) { - MHD_stop_daemon(Admin_HTTP_Server); - Admin_HTTP_Server = NULL; + if (GloVars.web_interface_plugin == NULL) { char *key_pem; char *cert_pem; key_pem = load_file(ssl_key_fp); @@ -5378,6 +5360,40 @@ void ProxySQL_Admin::flush_admin_variables___database_to_runtime(SQLite3DB *db, MHD_OPTION_HTTPS_MEM_KEY, key_pem, MHD_OPTION_HTTPS_MEM_CERT, cert_pem, MHD_OPTION_END); + } else { + GloWebInterface->start(variables.web_port); + } + } else { + if (GloVars.web_interface_plugin == NULL) { + MHD_stop_daemon(Admin_HTTP_Server); + Admin_HTTP_Server = NULL; + } else { + GloWebInterface->stop(); + } + } + variables.web_enabled_old = variables.web_enabled; + } else { + if (variables.web_port != variables.web_port_old) { + if (variables.web_enabled) { + if (GloVars.web_interface_plugin == NULL) { + MHD_stop_daemon(Admin_HTTP_Server); + Admin_HTTP_Server = NULL; + char *key_pem; + char *cert_pem; + key_pem = load_file(ssl_key_fp); + cert_pem = load_file(ssl_cert_fp); + Admin_HTTP_Server = MHD_start_daemon(MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_SSL, + variables.web_port, + NULL, NULL, http_handler, NULL, + MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, MHD_OPTION_STRICT_FOR_CLIENT, (int) 1, + MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) 4, + MHD_OPTION_NONCE_NC_SIZE, (unsigned int) 300, + MHD_OPTION_HTTPS_MEM_KEY, key_pem, + MHD_OPTION_HTTPS_MEM_CERT, cert_pem, + MHD_OPTION_END); + } else { + GloWebInterface->start(variables.web_port); + } } variables.web_port_old = variables.web_port; } diff --git a/src/main.cpp b/src/main.cpp index 6009f88942..005c3fd770 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ #include "MySQL_Authentication.hpp" #include "MySQL_LDAP_Authentication.hpp" #include "proxysql_restapi.h" +#include "Web_Interface.hpp" #include @@ -40,6 +41,9 @@ extern "C" MySQL_LDAP_Authentication * create_MySQL_LDAP_Authentication_func() { volatile create_MySQL_LDAP_Authentication_t * create_MySQL_LDAP_Authentication = NULL; void * __mysql_ldap_auth; +volatile create_Web_Interface_t * create_Web_Interface = NULL; +void * __web_interface; + // absolute path of ssl files char *ssl_key_fp = NULL; char *ssl_cert_fp = NULL; @@ -526,9 +530,10 @@ int ssl_mkit(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days) { proxy_error("Unable to run EVP_PKEY_assign_RSA()\n"); exit(EXIT_SUCCESS); // we exit gracefully to avoid being restarted } - x1 = generate_x509(pk, (const unsigned char *)"ProxySQL_Auto_Generated_CA_Certificate", 2, 3650, NULL, NULL); + time_t t = time(NULL); + x1 = generate_x509(pk, (const unsigned char *)"ProxySQL_Auto_Generated_CA_Certificate", t, 3650, NULL, NULL); write_x509(ssl_ca_fp, x1); - x2 = generate_x509(pk, (const unsigned char *)"ProxySQL_Auto_Generated_Server_Certificate", 3, 3650, x1, pk); + x2 = generate_x509(pk, (const unsigned char *)"ProxySQL_Auto_Generated_Server_Certificate", t, 3650, x1, pk); write_x509(ssl_cert_fp, x2); rsa = NULL; @@ -695,7 +700,7 @@ ClickHouse_Authentication *GloClickHouseAuth; Query_Processor *GloQPro; ProxySQL_Admin *GloAdmin; MySQL_Threads_Handler *GloMTH = NULL; - +Web_Interface *GloWebInterface; MySQL_STMT_Manager_v14 *GloMyStmt; MySQL_Monitor *GloMyMon; @@ -819,6 +824,14 @@ void ProxySQL_Main_process_global_variables(int argc, const char **argv) { GloVars.errorlog = strdup(errorlog_path.c_str()); } } + if (root.exists("web_interface_plugin")==true) { + string web_interface_plugin; + bool rc; + rc=root.lookupValue("web_interface_plugin", web_interface_plugin); + if (rc==true) { + GloVars.web_interface_plugin=strdup(web_interface_plugin.c_str()); + } + } if (root.exists("ldap_auth_plugin")==true) { string ldap_auth_plugin; bool rc; @@ -966,6 +979,9 @@ void ProxySQL_Main_init_Query_module() { GloQPro->print_version(); GloAdmin->init_mysql_query_rules(); GloAdmin->init_mysql_firewall(); +// if (GloWebInterface) { +// GloWebInterface->print_version(); +// } } void ProxySQL_Main_init_MySQL_Threads_Handler_module() { @@ -1184,6 +1200,35 @@ void ProxySQL_Main_init() { static void LoadPlugins() { + if (GloVars.web_interface_plugin) { + dlerror(); + char * dlsym_error = NULL; + dlerror(); + dlsym_error=NULL; + __web_interface = dlopen(GloVars.web_interface_plugin, RTLD_NOW); + if (!__web_interface) { + cerr << "Cannot load library: " << dlerror() << '\n'; + exit(EXIT_FAILURE); + } else { + dlerror(); + create_Web_Interface = (create_Web_Interface_t *) dlsym(__web_interface, "create_Web_Interface_func"); + dlsym_error = dlerror(); + if (dlsym_error!=NULL) { + cerr << "Cannot load symbol create_Web_Interface: " << dlsym_error << '\n'; + exit(EXIT_FAILURE); + } + } + if (__web_interface==NULL || dlsym_error) { + proxy_error("Unable to load Web_Interface from %s\n", GloVars.web_interface_plugin); + exit(EXIT_FAILURE); + } else { + GloWebInterface = create_Web_Interface(); + if (GloWebInterface) { + //GloAdmin->init_WebInterfacePlugin(); + //GloAdmin->load_ldap_variables_to_runtime(); + } + } + } if (GloVars.ldap_auth_plugin) { dlerror(); char * dlsym_error = NULL; @@ -1218,6 +1263,8 @@ static void LoadPlugins() { void ProxySQL_Main_init_phase2___not_started() { + LoadPlugins(); + ProxySQL_Main_init_main_modules(); ProxySQL_Main_init_Admin_module(); GloMTH->print_version(); @@ -1234,7 +1281,6 @@ void ProxySQL_Main_init_phase2___not_started() { GloVars.confFile->CloseFile(); } - LoadPlugins(); ProxySQL_Main_init_Auth_module(); @@ -1814,6 +1860,9 @@ int main(int argc, const char * argv[]) { #ifdef RUNNING_ON_VALGRIND if (RUNNING_ON_VALGRIND==0) { + if (__web_interface) { + dlclose(__web_interface); + } if (__mysql_ldap_auth) { dlclose(__mysql_ldap_auth); } diff --git a/src/proxysql.cfg b/src/proxysql.cfg index e3fe5cd4d0..d210cc56ea 100644 --- a/src/proxysql.cfg +++ b/src/proxysql.cfg @@ -8,6 +8,7 @@ restart_on_missing_heartbeats=10 datadir="/var/lib/proxysql" //execute_on_exit_failure="/path/to/script" //ldap_auth_plugin="../../proxysql_ldap_plugin/MySQL_LDAP_Authentication_plugin.so" +web_interface_plugin="../../proxysql_web_interface_plugin/src/Web_Interface_plugin.so" admin_variables= {