From 2ff6ec03aa116866c08cfd32b84ff375fed22449 Mon Sep 17 00:00:00 2001 From: bol-van Date: Fri, 24 Jan 2025 11:12:56 +0300 Subject: [PATCH] tpws,nfqws: return lists reload on HUP --- nfq/hostlist.h | 2 ++ nfq/ipset.h | 2 ++ nfq/nfqws.c | 29 +++++++++++++++++++++++++++-- nfq/pools.c | 15 ++++++++++++++- nfq/pools.h | 2 ++ tpws/hostlist.h | 2 ++ tpws/ipset.h | 2 ++ tpws/pools.c | 14 ++++++++++++++ tpws/pools.h | 2 ++ tpws/tpws.c | 23 ++++++++++++++++++++++- tpws/tpws.h | 2 +- tpws/tpws_conn.c | 2 ++ 12 files changed, 92 insertions(+), 5 deletions(-) diff --git a/nfq/hostlist.h b/nfq/hostlist.h index deedc5c..9676613 100644 --- a/nfq/hostlist.h +++ b/nfq/hostlist.h @@ -13,3 +13,5 @@ bool HostlistCheck(const struct desync_profile *dp,const char *host, bool *exclu struct hostlist_file *RegisterHostlist(struct desync_profile *dp, bool bExclude, const char *filename); bool HostlistsReloadCheckForProfile(const struct desync_profile *dp); void HostlistsDebug(); + +#define ResetAllHostlistsModTime() hostlist_files_reset_modtime(¶ms.hostlists) diff --git a/nfq/ipset.h b/nfq/ipset.h index f283c2f..b711ac5 100644 --- a/nfq/ipset.h +++ b/nfq/ipset.h @@ -10,3 +10,5 @@ bool IpsetCheck(const struct desync_profile *dp, const struct in_addr *ipv4, con struct ipset_file *RegisterIpset(struct desync_profile *dp, bool bExclude, const char *filename); void IpsetsDebug(); bool AppendIpsetItem(ipset *ips, char *ip); + +#define ResetAllIpsetModTime() ipset_files_reset_modtime(¶ms.ipsets) diff --git a/nfq/nfqws.c b/nfq/nfqws.c index 965893f..fdd4712 100644 --- a/nfq/nfqws.c +++ b/nfq/nfqws.c @@ -49,14 +49,34 @@ #define MAX_CONFIG_FILE_SIZE 16384 struct params_s params; +static bool bReload=false; #ifdef __CYGWIN__ bool bQuit=false; #endif static void onhup(int sig) { - printf("HUP received !\n"); - // do not do anything. lists auto reload themselves based on file time. + printf("HUP received ! Lists will be reloaded.\n"); + bReload=true; +} +static void ReloadCheck() +{ + if (bReload) + { + ResetAllHostlistsModTime(); + if (!LoadAllHostLists()) + { + DLOG_ERR("hostlists load failed. this is fatal.\n"); + exit(1); + } + ResetAllIpsetModTime(); + if (!LoadAllIpsets()) + { + DLOG_ERR("ipset load failed. this is fatal.\n"); + exit(1); + } + bReload=false; + } } static void onusr1(int sig) @@ -251,6 +271,7 @@ static int nfq_main(void) { while ((rd = recv(fd, buf, sizeof(buf), 0)) >= 0) { + ReloadCheck(); if (rd) { int r = nfq_handle_packet(h, (char *)buf, (int)rd); @@ -371,6 +392,8 @@ static int dvt_main(void) uint8_t verdict; size_t len = rd; + ReloadCheck(); + DLOG("packet: id=%u len=%zu\n", id, len); verdict = processPacketData(&mark, NULL, buf, &len); switch (verdict & VERDICT_MASK) @@ -489,6 +512,8 @@ static int win_main(const char *windivert_filter) return w_win32_error; } + ReloadCheck(); + *ifout=0; if (wa.Outbound) snprintf(ifout,sizeof(ifout),"%u.%u", wa.Network.IfIdx, wa.Network.SubIfIdx); DLOG("packet: id=%u len=%zu %s IPv6=%u IPChecksum=%u TCPChecksum=%u UDPChecksum=%u IfIdx=%u.%u\n", id, len, wa.Outbound ? "outbound" : "inbound", wa.IPv6, wa.IPChecksum, wa.TCPChecksum, wa.UDPChecksum, wa.Network.IfIdx, wa.Network.SubIfIdx); diff --git a/nfq/pools.c b/nfq/pools.c index abb82d8..ac9cb14 100644 --- a/nfq/pools.c +++ b/nfq/pools.c @@ -154,7 +154,6 @@ void strlist_destroy(struct str_list_head *head) - struct hostlist_file *hostlist_files_add(struct hostlist_files_head *head, const char *filename) { struct hostlist_file *entry = malloc(sizeof(struct hostlist_file)); @@ -202,6 +201,13 @@ struct hostlist_file *hostlist_files_search(struct hostlist_files_head *head, co } return NULL; } +void hostlist_files_reset_modtime(struct hostlist_files_head *list) +{ + struct hostlist_file *hfile; + + LIST_FOREACH(hfile, list, next) + hfile->mod_time=0; +} struct hostlist_item *hostlist_collection_add(struct hostlist_collection_head *head, struct hostlist_file *hfile) { @@ -416,6 +422,13 @@ struct ipset_file *ipset_files_search(struct ipset_files_head *head, const char } return NULL; } +void ipset_files_reset_modtime(struct ipset_files_head *list) +{ + struct ipset_file *hfile; + + LIST_FOREACH(hfile, list, next) + hfile->mod_time=0; +} struct ipset_item *ipset_collection_add(struct ipset_collection_head *head, struct ipset_file *hfile) { diff --git a/nfq/pools.h b/nfq/pools.h index cd37bdd..41bf1d6 100644 --- a/nfq/pools.h +++ b/nfq/pools.h @@ -59,6 +59,7 @@ LIST_HEAD(hostlist_files_head, hostlist_file); struct hostlist_file *hostlist_files_add(struct hostlist_files_head *head, const char *filename); void hostlist_files_destroy(struct hostlist_files_head *head); struct hostlist_file *hostlist_files_search(struct hostlist_files_head *head, const char *filename); +void hostlist_files_reset_modtime(struct hostlist_files_head *list); struct hostlist_item { struct hostlist_file *hfile; @@ -120,6 +121,7 @@ LIST_HEAD(ipset_files_head, ipset_file); struct ipset_file *ipset_files_add(struct ipset_files_head *head, const char *filename); void ipset_files_destroy(struct ipset_files_head *head); struct ipset_file *ipset_files_search(struct ipset_files_head *head, const char *filename); +void ipset_files_reset_modtime(struct ipset_files_head *list); struct ipset_item { struct ipset_file *hfile; diff --git a/tpws/hostlist.h b/tpws/hostlist.h index deedc5c..9676613 100644 --- a/tpws/hostlist.h +++ b/tpws/hostlist.h @@ -13,3 +13,5 @@ bool HostlistCheck(const struct desync_profile *dp,const char *host, bool *exclu struct hostlist_file *RegisterHostlist(struct desync_profile *dp, bool bExclude, const char *filename); bool HostlistsReloadCheckForProfile(const struct desync_profile *dp); void HostlistsDebug(); + +#define ResetAllHostlistsModTime() hostlist_files_reset_modtime(¶ms.hostlists) diff --git a/tpws/ipset.h b/tpws/ipset.h index f283c2f..b711ac5 100644 --- a/tpws/ipset.h +++ b/tpws/ipset.h @@ -10,3 +10,5 @@ bool IpsetCheck(const struct desync_profile *dp, const struct in_addr *ipv4, con struct ipset_file *RegisterIpset(struct desync_profile *dp, bool bExclude, const char *filename); void IpsetsDebug(); bool AppendIpsetItem(ipset *ips, char *ip); + +#define ResetAllIpsetModTime() ipset_files_reset_modtime(¶ms.ipsets) diff --git a/tpws/pools.c b/tpws/pools.c index 631a251..ac9cb14 100644 --- a/tpws/pools.c +++ b/tpws/pools.c @@ -201,6 +201,13 @@ struct hostlist_file *hostlist_files_search(struct hostlist_files_head *head, co } return NULL; } +void hostlist_files_reset_modtime(struct hostlist_files_head *list) +{ + struct hostlist_file *hfile; + + LIST_FOREACH(hfile, list, next) + hfile->mod_time=0; +} struct hostlist_item *hostlist_collection_add(struct hostlist_collection_head *head, struct hostlist_file *hfile) { @@ -415,6 +422,13 @@ struct ipset_file *ipset_files_search(struct ipset_files_head *head, const char } return NULL; } +void ipset_files_reset_modtime(struct ipset_files_head *list) +{ + struct ipset_file *hfile; + + LIST_FOREACH(hfile, list, next) + hfile->mod_time=0; +} struct ipset_item *ipset_collection_add(struct ipset_collection_head *head, struct ipset_file *hfile) { diff --git a/tpws/pools.h b/tpws/pools.h index cd37bdd..41bf1d6 100644 --- a/tpws/pools.h +++ b/tpws/pools.h @@ -59,6 +59,7 @@ LIST_HEAD(hostlist_files_head, hostlist_file); struct hostlist_file *hostlist_files_add(struct hostlist_files_head *head, const char *filename); void hostlist_files_destroy(struct hostlist_files_head *head); struct hostlist_file *hostlist_files_search(struct hostlist_files_head *head, const char *filename); +void hostlist_files_reset_modtime(struct hostlist_files_head *list); struct hostlist_item { struct hostlist_file *hfile; @@ -120,6 +121,7 @@ LIST_HEAD(ipset_files_head, ipset_file); struct ipset_file *ipset_files_add(struct ipset_files_head *head, const char *filename); void ipset_files_destroy(struct ipset_files_head *head); struct ipset_file *ipset_files_search(struct ipset_files_head *head, const char *filename); +void ipset_files_reset_modtime(struct ipset_files_head *list); struct ipset_item { struct ipset_file *hfile; diff --git a/tpws/tpws.c b/tpws/tpws.c index d261ebf..c52a275 100644 --- a/tpws/tpws.c +++ b/tpws/tpws.c @@ -50,10 +50,31 @@ #define MAX_CONFIG_FILE_SIZE 16384 struct params_s params; +static bool bReload=false; static void onhup(int sig) { - printf("HUP received !\n"); + printf("HUP received ! Lists will be reloaded.\n"); + bReload=true; +} +void ReloadCheck() +{ + if (bReload) + { + ResetAllHostlistsModTime(); + if (!LoadAllHostLists()) + { + DLOG_ERR("hostlists load failed. this is fatal.\n"); + exit(1); + } + ResetAllIpsetModTime(); + if (!LoadAllIpsets()) + { + DLOG_ERR("ipset load failed. this is fatal.\n"); + exit(1); + } + bReload=false; + } } static void onusr2(int sig) diff --git a/tpws/tpws.h b/tpws/tpws.h index fa4eb30..baa7d60 100644 --- a/tpws/tpws.h +++ b/tpws/tpws.h @@ -6,4 +6,4 @@ #include -void dohup(void); +void ReloadCheck(); diff --git a/tpws/tpws_conn.c b/tpws/tpws_conn.c index e30bd1a..1294e74 100644 --- a/tpws/tpws_conn.c +++ b/tpws/tpws_conn.c @@ -1544,6 +1544,8 @@ int event_loop(const int *listen_fd, size_t listen_fd_ct) for(;;) { + ReloadCheck(); + DBGPRINT("epoll_wait\n"); if ((num_events = epoll_wait(efd, events, MAX_EPOLL_EVENTS, -1)) == -1)