Browse Source

perf: avoid repeated hash generation

dev
Dnomd343 2 years ago
parent
commit
9ee2f4c2f7
  1. 2
      include/constant.h
  2. 19
      src/applet/adguard.c
  3. 19
      src/cleardns.c

2
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"

19
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);

19
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;
}

Loading…
Cancel
Save