diff --git a/CMakeLists.txt b/CMakeLists.txt index b8ab298486152842388a0616cc148ffd7c142227..61a81f20584496b93acb229992865dedb54180b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ add_subdirectory(dap-sdk) add_subdirectory(3rdparty/monero_crypto) add_subdirectory(3rdparty/cuttdb) add_subdirectory(modules/) +add_subdirectory(modules_dynamic) add_library(${PROJECT_NAME} STATIC cellframe-sdk.c) @@ -123,6 +124,12 @@ if (CELLFRAME_MODULES MATCHES "srv-stake") set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_chain_net_srv_stake ) endif() +# Enable service for dynamic modules +if (CELLFRAME_MODULES MATCHES "modules-dynamic") + message("[+] Module 'dap_modules_dynamic_cdb'") + set(CELLFRAME_LIBS ${CELLFRAME_LIBS} dap_modules_dynamic_cdb ) +endif() + if (WIN32) set(CELLFRAME_LIBS ${CELLFRAME_LIBS} KERNEL32 USER32 SHELL32 WINMM GDI32 ADVAPI32 Ole32 Version Imm32 OleAut32 ws2_32 ntdll psapi diff --git a/modules/net/dap_chain_net_bugreport.c b/modules/net/dap_chain_net_bugreport.c deleted file mode 100644 index e174e39a8a759a635c266624de02e258b2535db0..0000000000000000000000000000000000000000 --- a/modules/net/dap_chain_net_bugreport.c +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Authors: - * Alexander Lysikov <alexander.lysikov@demlabs.net> - * DeM Labs Inc. https://demlabs.net - * Kelvin Project https://github.com/kelvinblockchain - * Copyright (c) 2020 - * All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. - */ -#include <stdlib.h> -#include <stdio.h> -#include <time.h> -#include <stdlib.h> -#include <stddef.h> -#include <stdint.h> - -#include "dap_common.h" -#include "dap_string.h" -#include "dap_strfuncs.h" -#include "dap_file_utils.h" -#include "dap_config.h" -#include "rand/dap_rand.h" - -#include "http_status_code.h" -#include "dap_http_simple.h" -#include "dap_enc_http.h" -#include "dap_chain_net_bugreport.h" - -#define LOG_TAG "chain_net_bugreport" - -#define BUGREPORT_URL "/bugreport" - -static int64_t bugreport_write_to_file(byte_t *a_request_byte, size_t a_request_size) -{ - int64_t l_report_number = -2; - if(!a_request_byte || !a_request_size) - return -1; - char *l_dir_str = dap_strdup_printf("%s/var/bugreport", g_sys_dir_path); - dap_mkdir_with_parents(l_dir_str); - - const time_t l_timer = time(NULL); - struct tm l_tm; - localtime_r(&l_timer, &l_tm); - // create unique number for bugreport - randombytes(&l_report_number, sizeof(int64_t)); - if(l_report_number < 0) - l_report_number = -l_report_number; - //l_report_number 5 characters long - l_report_number %= 100000ll; - /* - // l_report_number 20 characters long - l_report_number -= l_report_number%1000000000000ll; - l_report_number+=(int64_t)(l_tm.tm_year - 100)*10000000000; - l_report_number+=(int64_t)(l_tm.tm_mon)*100000000; - l_report_number+=(int64_t)(l_tm.tm_mday)*1000000; - l_report_number+=(int64_t)(l_tm.tm_hour)*10000; - l_report_number+=(int64_t)(l_tm.tm_min)*100; - l_report_number+=(int64_t)(l_tm.tm_sec); - */ - char *l_filename_str = dap_strdup_printf("%s/%02d-%02d-%02d_%02d:%02d:%02d_%05lld.brt", l_dir_str, - l_tm.tm_year - 100, l_tm.tm_mon, l_tm.tm_mday, - l_tm.tm_hour, l_tm.tm_min, l_tm.tm_sec, - l_report_number); - FILE *l_fp; - if((l_fp = fopen(l_filename_str, "wb")) != NULL) { - if(fwrite(a_request_byte, 1, a_request_size, l_fp) != a_request_size) - l_report_number = -3; - fclose(l_fp); - } - DAP_DELETE(l_filename_str); - DAP_DELETE(l_dir_str); - return l_report_number; -} - -/** - * @brief bugreport_http_proc - * @param a_http_simple - * @param a_arg - */ -static void bugreport_http_proc(struct dap_http_simple *a_http_simple, void * a_arg) -{ - // data:text/html,<form action=http://192.168.100.92:8079/bugreport/ method=post><input name=a></form> - // data:text/html,<form action=http://cdb.klvn.io/bugreport/ method=post><input name=a></form> - log_it(L_DEBUG, "bugreport_http_proc request"); - http_status_code_t * return_code = (http_status_code_t*) a_arg; - //if(dap_strcmp(cl_st->http->url_path, BUGREPORT_URL) == 0 ) - if(dap_strcmp(a_http_simple->http->action, "GET") == 0) { - size_t l_url_len = dap_strlen(a_http_simple->http->url_path); - if(!l_url_len) { - a_http_simple->reply = dap_strdup_printf("Unique Bug Report number required)"); - *return_code = Http_Status_NotFound; - } - else{ - *return_code = Http_Status_OK; - } - a_http_simple->reply_size = strlen(a_http_simple->reply); - *return_code = Http_Status_NotFound; - } - if(dap_strcmp(a_http_simple->http->action, "POST") == 0) { - //a_http_simple->request_byte; - //a_http_simple->request_size; - //a_http_simple->http->in_content_length; - - int64_t l_bugreport_number = bugreport_write_to_file(a_http_simple->request_byte, a_http_simple->request_size); - if(l_bugreport_number >= 0) { - //l_report_number 5 characters long - a_http_simple->reply = dap_strdup_printf("Bug Report #%05lld saved successfully)", l_bugreport_number); - //l_report_number 20 characters long - //a_http_simple->reply = dap_strdup_printf("Bug Report #%020lld saved successfully)", l_bugreport_number); - } - else { - a_http_simple->reply = dap_strdup_printf("Bug Report not saved( code=%lld", l_bugreport_number); - } - a_http_simple->reply_size = strlen(a_http_simple->reply); - *return_code = Http_Status_OK; - - } else { - log_it(L_ERROR, "Wrong action '%s' for the request. Must be 'POST'", a_http_simple->http->action); - a_http_simple->reply = dap_strdup_printf("Wrong action '%s' for the request. Must be 'POST'", - a_http_simple->http->action); - a_http_simple->reply_size = strlen(a_http_simple->reply); - *return_code = Http_Status_NotFound; - } -} - -/** - * @brief dap_chain_net_bugreport_add_proc - * @param sh HTTP server instance - */ -void dap_chain_net_bugreport_add_proc(struct dap_http * sh) -{ - const char * url = BUGREPORT_URL; - dap_http_simple_proc_add(sh, url, 14096, bugreport_http_proc); -} - diff --git a/modules/net/dap_chain_net_news.c b/modules/net/dap_chain_net_news.c deleted file mode 100644 index 8c4984ae9bda0160d89c2ad63366971542d85f9b..0000000000000000000000000000000000000000 --- a/modules/net/dap_chain_net_news.c +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Authors: - * Alexander Lysikov <alexander.lysikov@demlabs.net> - * DeM Labs Inc. https://demlabs.net - * Kelvin Project https://github.com/kelvinblockchain - * Copyright (c) 2020 - * All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <stddef.h> - -#include "dap_common.h" -#include "dap_string.h" -#include "dap_strfuncs.h" -#include "dap_file_utils.h" -#include "dap_config.h" - -#include "http_status_code.h" -#include "dap_http_simple.h" -#include "dap_enc_http.h" -//#include "<dap_chain_global_db_driver.h> -#include "dap_chain_global_db.h" -#include "dap_chain_net_news.h" -#define LOG_TAG "chain_net_news" - -#define NEWS_URL "/news" -#define GROUP_NEWS "cdb.news" -#define DEFAULT_LANG "en" - -/* Set news in the selected language - * a_lang - a language like "en", "ru", "fr" - * a_data_news - news data - * a_data_news_len length of news - */ -int dap_chain_net_news_write(const char *a_lang, char *a_data_news, size_t a_data_news_len) -{ - if(!a_data_news || !a_data_news_len) - return -2; - if(!a_lang) - a_lang = DEFAULT_LANG; - if(dap_chain_global_db_gr_set(dap_strdup(a_lang), a_data_news, a_data_news_len, GROUP_NEWS)) - return 0; - return -1; -} - -/* Get news in the selected language - * a_lang - a language like "en", "ru", "fr" - */ -byte_t* dap_chain_net_news_read(const char *a_lang, size_t *a_news_len) -{ - if(!a_lang) - return NULL; - byte_t *l_ret_data = NULL; - size_t l_data_len_num = 0; - dap_store_obj_t *l_obj = dap_chain_global_db_obj_gr_get(a_lang, &l_data_len_num, GROUP_NEWS); - if(l_obj && l_obj->value_len) { - l_ret_data = DAP_NEW_Z_SIZE(byte_t, l_obj->value_len); - memcpy(l_ret_data, l_obj->value, l_obj->value_len); - if(a_news_len) - *a_news_len = l_obj->value_len; - } - dap_store_obj_free(l_obj, l_data_len_num); - return l_ret_data; -} - -/** - * @brief news_http_proc - * @param a_http_simple - * @param a_arg - */ -static void news_http_proc(struct dap_http_simple *a_http_simple, void * a_arg) -{ - log_it(L_DEBUG, "news_http_proc request"); - http_status_code_t * return_code = (http_status_code_t*) a_arg; - const char *l_lang = DEFAULT_LANG; - if(dap_strcmp(a_http_simple->http->url_path, NEWS_URL)) { - l_lang = a_http_simple->http->url_path; - } - - if(l_lang) - { - size_t l_news_data_len = 0; - // get news in the selected language - byte_t *l_news_data = dap_chain_net_news_read(l_lang, &l_news_data_len); - // get news in the default language - if(!l_news_data && dap_strcmp(a_http_simple->http->in_query_string, "LocalNewsOnly")) - l_news_data = dap_chain_net_news_read(DEFAULT_LANG, &l_news_data_len); - if( l_news_data){ - a_http_simple->reply = l_news_data ; - a_http_simple->reply_size = l_news_data_len; - }else{ - a_http_simple->reply = dap_strdup("no news"); - a_http_simple->reply_size = dap_strlen((char*) a_http_simple->reply) + 1; - } - *return_code = Http_Status_OK; - } - else { - log_it(L_ERROR, "Wrong request. Must be %s/<lang_code>, example http:/<addr>%s/en", NEWS_URL, NEWS_URL); - a_http_simple->reply = dap_strdup_printf("Wrong request. Must be %s/<lang_code>, example http:/<addr>%s/en", - NEWS_URL, NEWS_URL); - a_http_simple->reply_size = strlen(a_http_simple->reply); - *return_code = Http_Status_NotFound; - } -} - -/** - * @brief dap_chain_net_news_add_proc - * @param sh HTTP server instance - */ -void dap_chain_net_news_add_proc(struct dap_http * sh) -{ - const char * url = NEWS_URL; - dap_http_simple_proc_add(sh, url, 14096, news_http_proc); -} - diff --git a/modules/net/dap_chain_net_news.h b/modules/net/dap_chain_net_news.h index 08bd6ebb2be1eb3024e75431db97c0f7a60d02a2..409063d83e76198351aee2916537c25dcac049ef 100644 --- a/modules/net/dap_chain_net_news.h +++ b/modules/net/dap_chain_net_news.h @@ -24,16 +24,4 @@ #include "dap_http.h" -/* Set news in the selected language - * a_lang - a language like "en", "ru", "fr" - * a_data_news - news data - * a_data_news_len length of news - */ -int dap_chain_net_news_write(const char *a_lang, char *a_data_news, size_t a_data_news_len); - -/* Get news in the selected language - * a_lang - a language like "en", "ru", "fr" - */ -byte_t* dap_chain_net_news_read(const char *a_lang, size_t *a_news_len); - -void dap_chain_net_news_add_proc(struct dap_http * sh); +int dap_chain_net_news_init(dap_http_t * a_http); diff --git a/modules/net/dap_chain_node_cli.c b/modules/net/dap_chain_node_cli.c index 958af35a70ce4969f07ef6d3b8862777e4f6731d..3ecd46abc29c7c3e132f1f95be4751f774164818 100644 --- a/modules/net/dap_chain_node_cli.c +++ b/modules/net/dap_chain_node_cli.c @@ -960,11 +960,6 @@ int dap_chain_node_cli_init(dap_config_t * g_config) dap_chain_node_cli_cmd_item_create ("exit", com_exit, NULL, "Stop application and exit", "exit\n" ); -//#ifndef _WIN32 - // News - dap_chain_node_cli_cmd_item_create("news", com_news, NULL, "Add News for VPN clients. Language code is a text code like \"en\", \"ru\", \"fr\"", - "news [-text <news text> | -file <filename with news>] -lang <language code> \n"); -//#endif // create thread for waiting of clients pthread_t l_thread_id; diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c index 97357593701c88e9ded8de7869db40d00de395bf..8e79e0e6a3eb1db6076d153ea77991f539a40ab7 100644 --- a/modules/net/dap_chain_node_cli_cmd.c +++ b/modules/net/dap_chain_node_cli_cmd.c @@ -3755,47 +3755,3 @@ int com_print_log(int argc, char ** argv, void *arg_func, char **str_reply) return 0; } -/** - * Add News for VPN clients - * news [-text <news text> | -file <filename with news>] -lang <language code> - */ -int com_news(int a_argc, char ** a_argv, void *a_arg_func, char **a_str_reply) -{ - int arg_index = 1; - const char * l_str_lang = NULL; - const char * l_str_text = NULL; - const char * l_str_file = NULL; - dap_chain_node_cli_find_option_val(a_argv, arg_index, a_argc, "-lang", &l_str_lang); - dap_chain_node_cli_find_option_val(a_argv, arg_index, a_argc, "-text", &l_str_text); - dap_chain_node_cli_find_option_val(a_argv, arg_index, a_argc, "-file", &l_str_file); - if(!l_str_text && !l_str_file) { - dap_chain_node_cli_set_reply_text(a_str_reply, "no source of news, add parameter -text or -file"); - return -1; - } - char *l_data_news; - size_t l_data_news_len = 0; - const char *l_from = NULL; - - if(l_str_text) { - l_data_news = dap_strdup(l_str_text); - l_data_news_len = dap_strlen(l_str_text); - l_from = "text"; - } - else if(l_str_file) { - if(dap_file_get_contents(l_str_file, &l_data_news,&l_data_news_len)) { - l_from = "file"; - } - else{ - dap_chain_node_cli_set_reply_text(a_str_reply, "Can't read file %s", l_str_file); - return -2; - } - } - - int l_res = dap_chain_net_news_write(l_str_lang, l_data_news, l_data_news_len); - if(l_res){ - dap_chain_node_cli_set_reply_text(a_str_reply, "Error, News cannot be added from %s", l_from); - return -3; - } - dap_chain_node_cli_set_reply_text(a_str_reply, "News added from %s successfully", l_from); - return 0; -} diff --git a/modules/net/include/dap_chain_net_bugreport.h b/modules/net/include/dap_chain_net_bugreport.h index 00f4c2d9f6d8d5271b8f80a76591e107232a1020..c930015f0b79897ba7502452b74da80595b89872 100644 --- a/modules/net/include/dap_chain_net_bugreport.h +++ b/modules/net/include/dap_chain_net_bugreport.h @@ -24,4 +24,4 @@ #include "dap_http.h" -void dap_chain_net_bugreport_add_proc(struct dap_http * sh); +int dap_chain_net_bugreport_init(dap_http_t * a_http); diff --git a/modules/net/include/dap_chain_node_cli_cmd.h b/modules/net/include/dap_chain_node_cli_cmd.h index 19b73a6239f3dc7610936fe4dcee91bfff1431d0..a5be2cd0f863195bf39ccb6ea53c1f5ac78f9a03 100644 --- a/modules/net/include/dap_chain_node_cli_cmd.h +++ b/modules/net/include/dap_chain_node_cli_cmd.h @@ -133,11 +133,6 @@ int com_stats(int argc, char ** argv, void *arg_func, char **str_reply); int com_exit(int argc, char ** argv, void *arg_func, char **str_reply); -//#ifndef _WIN32 -// Add News for VPN clients -int com_news(int a_argc, char ** a_argv, void *a_arg_func, char **a_str_reply); -//#endif - int com_mempool_delete(int argc, char ** argv, void *arg_func, char ** a_str_reply); int com_mempool_list(int argc, char ** argv, void *arg_func, char ** a_str_reply); int com_mempool_proc(int argc, char ** argv, void *arg_func, char ** a_str_reply); diff --git a/modules_dynamic/CMakeLists.txt b/modules_dynamic/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f4c99055a84659c988d3da1e5da165433129084e --- /dev/null +++ b/modules_dynamic/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(cdb) + diff --git a/modules_dynamic/cdb/CMakeLists.txt b/modules_dynamic/cdb/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..a53e016eb7276048420e90c215c72ee992acd1da --- /dev/null +++ b/modules_dynamic/cdb/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 2.8) +project (dap_modules_dynamic_cdb) + +file(GLOB DAP_MODULES_DYNAMIC_CDB_SRCS *.c) + +file(GLOB DAP_MODULES_DYNAMIC_CDB_HEADERS include/*.h) + +add_library(${PROJECT_NAME} STATIC ${DAP_MODULES_DYNAMIC_CDB_SRCS} ${DAP_MODULES_DYNAMIC_CDB_HEADERS}) + +#target_link_libraries(dap_chain_net_srv dap_core dap_crypto dap_chain dap_chain_net dap_chain_wallet) +target_link_libraries(${PROJECT_NAME} dap_core dap_http_server ${CMAKE_DL_LIBS}) + +target_include_directories(${PROJECT_NAME} INTERFACE .) +target_include_directories(${PROJECT_NAME} PUBLIC include) diff --git a/modules_dynamic/cdb/dap_modules_dynamic_cdb.c b/modules_dynamic/cdb/dap_modules_dynamic_cdb.c new file mode 100644 index 0000000000000000000000000000000000000000..696838cbeb2a120d60ea81bdf9bacc4daa3d0cf9 --- /dev/null +++ b/modules_dynamic/cdb/dap_modules_dynamic_cdb.c @@ -0,0 +1,69 @@ +/* + * Authors: + * Aleksei I. Voronin <aleksei.voronin@demlabs.net> + * DeM Labs Inc. https://demlabs.net + * Kelvin Project https://github.com/kelvinblockchain + * Copyright (c) 2017-2019 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "dap_modules_dynamic_cdb.h" +#include "dap_common.h" + +#ifdef DAP_OS_LINUX +#include <dlfcn.h> +#endif + +#define LOG_TAG "dap_http" + +static const char * s_default_path_modules = "var/modules"; + +int dap_modules_dynamic_load_cdb(dap_http_t * a_server){ + char l_lib_path[MAX_PATH] = {'\0'}; +#if defined (DAP_OS_LINUX) && !defined (__ANDROID__) + const char * l_cdb_so_name = "libcellframe-node-cdb.so"; + dap_sprintf(l_lib_path, "%s/%s/%s", g_sys_dir_path, s_default_path_modules, l_cdb_so_name); + + void* l_cdb_handle = NULL; + l_cdb_handle = dlopen(l_lib_path, RTLD_NOW); + if(!l_cdb_handle){ + log_it(L_ERROR,"Can't load %s module: %s", l_cdb_so_name, dlerror()); + return -1; + } + + int (*dap_chain_net_srv_vpn_cdb_init)(dap_http_t*); + const char * l_init_func_name = "dap_chain_net_srv_vpn_cdb_init"; + *(void **) (&dap_chain_net_srv_vpn_cdb_init) = dlsym(l_cdb_handle, l_init_func_name); + char* error; + if (( error = dlerror()) != NULL) { + log_it(L_ERROR,"%s module: %s error loading %s (%s)", l_cdb_so_name, l_init_func_name, error); + return -2; + } + + int l_init_res = (*dap_chain_net_srv_vpn_cdb_init)(a_server); + if(l_init_res){ + log_it(L_ERROR,"%s: %s returns %d", l_cdb_so_name, l_init_func_name, error); + return -3; + } + + return 0; +#else + log_it(L_ERROR,"%s: module is not supported on current platfrom", __PRETTY_FUNCTION__); + return -3; +#endif +} diff --git a/modules_dynamic/cdb/include/dap_modules_dynamic_cdb.h b/modules_dynamic/cdb/include/dap_modules_dynamic_cdb.h new file mode 100644 index 0000000000000000000000000000000000000000..492b98c71127b030ed44527e276fa41eeda9d9e1 --- /dev/null +++ b/modules_dynamic/cdb/include/dap_modules_dynamic_cdb.h @@ -0,0 +1,29 @@ +/* + * Authors: + * Aleksei I. Voronin <aleksei.voronin@demlabs.net> + * DeM Labs Inc. https://demlabs.net + * Kelvin Project https://github.com/kelvinblockchain + * Copyright (c) 2017-2019 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ + +#pragma once +#include "dap_http.h" + +int dap_modules_dynamic_load_cdb(dap_http_t * a_server); +