From 9ee2f4c2f7cf9277631cf8b65ca25ac2838d7837 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 5 Mar 2023 15:19:36 +0800 Subject: [PATCH] perf: avoid repeated hash generation --- include/constant.h | 2 ++ src/applet/adguard.c | 19 ++++++++++++++++++- src/cleardns.c | 19 ------------------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/include/constant.h b/include/constant.h index 03390d2..c25c4f7 100644 --- a/include/constant.h +++ b/include/constant.h @@ -13,6 +13,8 @@ #define RESTART_DELAY 1 #define DIVERTER_TIMEOUT 6 +// TODO: using CMAKE template + #define VERSION "1.3.3" #define CONFIG_FILE "cleardns.yml" diff --git a/src/applet/adguard.c b/src/applet/adguard.c index 2c64cfe..74c0d23 100644 --- a/src/applet/adguard.c +++ b/src/applet/adguard.c @@ -46,9 +46,26 @@ char *adguard_config(adguard *info, const char *raw_config) { // modify adguard log_fatal("AdGuardHome configure error"); } + char *password = NULL; + cJSON *user_passwd = cJSON_GetObjectItem(cJSON_GetArrayItem( + cJSON_GetObjectItem(json, "users"), 0), "password"); + if (cJSON_IsString(user_passwd)) { + char *hash_val = user_passwd->valuestring; + log_debug("Legacy hash value -> `%s`", hash_val); + if (bcrypt_verify(info->password, hash_val)) { + log_debug("Legacy hash value verify success"); + password = strdup(hash_val); + } else { + log_debug("Legacy hash value verify failed"); + } + } + if (password == NULL) { // password hash not ready + password = bcrypt_hash(info->password); + } + log_debug("AdGuardHome password -> `%s`", password); + cJSON *user_config = cJSON_CreateObject(); // setting up username and password cJSON *users_config = cJSON_CreateArray(); - char *password = bcrypt_hash(info->password); cJSON_AddItemToObject(user_config, "name", cJSON_CreateString(info->username)); cJSON_AddItemToObject(user_config, "password", cJSON_CreateString(password)); cJSON_AddItemToArray(users_config, user_config); diff --git a/src/cleardns.c b/src/cleardns.c index 2b129dd..31c85a3 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -117,28 +117,9 @@ void cleardns() { // cleardns service process_list_daemon(); // daemon all process } -#include "bcrypt.h" - int main(int argc, char *argv[]) { init(argc, argv); log_info("ClearDNS server start (%s)", VERSION); - - LOG_LEVEL = LOG_DEBUG; - log_debug("test mode start"); - - const char demo_str[] = "dnomd343"; - char *hash_ret = bcrypt_hash(demo_str); - - log_info("bcrypt hash -> `%s`", hash_ret); - - log_info("check ret -> `%s`", show_bool(bcrypt_verify(demo_str, hash_ret))); - - log_info("check ret -> `%s`", show_bool(bcrypt_verify("233333", hash_ret))); - - log_info("exit"); - - return 0; - cleardns(); return 0; }