diff --git a/src/api/config.c b/src/api/config.c index f04604bc..167763c4 100644 --- a/src/api/config.c +++ b/src/api/config.c @@ -26,6 +26,8 @@ #include "shmem.h" // hash_password() #include "config/password.h" +// main_pid() +#include "signals.h" static struct { const char *name; @@ -659,6 +661,7 @@ static int api_config_patch(struct ftl_conn *api) // Read all known config items bool config_changed = false; bool dnsmasq_changed = false; + bool rewrite_hosts = false; struct config newconf; duplicate_config(&newconf, &config); for(unsigned int i = 0; i < CONFIG_ELEMENTS; i++) @@ -730,6 +733,10 @@ static int api_config_patch(struct ftl_conn *api) if(conf_item->f & FLAG_RESTART_FTL) dnsmasq_changed = true; + // Check if this item requires rewriting the HOSTS file + if(conf_item == &config.dns.hosts) + rewrite_hosts = true; + // If the privacy level was decreased, we need to restart if(new_item == &newconf.misc.privacylevel && new_item->v.privacy_level < conf_item->v.privacy_level) @@ -768,6 +775,14 @@ static int api_config_patch(struct ftl_conn *api) // Store changed configuration to disk writeFTLtoml(true); + + // Rewrite HOSTS file if required + if(rewrite_hosts) + { + write_custom_list(); + // Reload HOSTS file + kill(main_pid(), SIGHUP); + } } else { @@ -816,6 +831,7 @@ static int api_config_put_delete(struct ftl_conn *api) // Read all known config items bool dnsmasq_changed = false; + bool rewrite_hosts = false; bool found = false; struct config newconf; duplicate_config(&newconf, &config); @@ -907,6 +923,10 @@ static int api_config_put_delete(struct ftl_conn *api) if(new_item->f & FLAG_RESTART_FTL) dnsmasq_changed = true; + // Check if this item requires rewriting the HOSTS file + if(new_item == &newconf.dns.hosts) + rewrite_hosts = true; + break; } @@ -954,6 +974,14 @@ static int api_config_put_delete(struct ftl_conn *api) // Store changed configuration to disk writeFTLtoml(true); + // Rewrite HOSTS file if required + if(rewrite_hosts) + { + write_custom_list(); + // Reload HOSTS file + kill(main_pid(), SIGHUP); + } + // Send empty reply with matching HTTP status code // 201 - Created or 204 - No content cJSON *json = JSON_NEW_OBJECT(); diff --git a/src/config/config.c b/src/config/config.c index 72c36407..553705db 100644 --- a/src/config/config.c +++ b/src/config/config.c @@ -462,7 +462,7 @@ void initConfig(struct config *conf) conf->dns.hosts.h = "Array of custom DNS records\n Example: hosts = [ \"127.0.0.1 mylocal\", \"192.168.0.1 therouter\" ]"; conf->dns.hosts.a = cJSON_CreateStringReference("Array of custom DNS records each one in HOSTS form: \"IP HOSTNAME\""); conf->dns.hosts.t = CONF_JSON_STRING_ARRAY; - conf->dns.hosts.f = FLAG_ADVANCED_SETTING | FLAG_RESTART_FTL; + conf->dns.hosts.f = FLAG_ADVANCED_SETTING; conf->dns.hosts.d.json = cJSON_CreateArray(); conf->dns.domainNeeded.k = "dns.domainNeeded"; diff --git a/src/config/config.h b/src/config/config.h index c230627e..72ee5aea 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -88,7 +88,7 @@ enum conf_type { #define FLAG_PSEUDO_ITEM (1 << 2) #define FLAG_INVALIDATE_SESSIONS (1 << 3) #define FLAG_WRITE_ONLY (1 << 4) -#define FLAG_ENV_VAR (1 << 5) +#define FLAG_ENV_VAR (1 << 5) struct conf_item { const char *k; // item Key