Skip to content

Commit 1e76ffa

Browse files
author
Yorhel
committed
Add geoip_cc[46] settings to disable GeoIP or specify custom database
1 parent 00f407c commit 1e76ffa

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

src/doc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ static const doc_set_t doc_sets[] = {
435435
" your system for other things besides ncdc, you share large files (>100MB)"
436436
" and people are not constantly downloading the same file from you."
437437
},
438+
{ "geoip_cc4", 0, "<path>|disabled",
439+
"Path to the GeoIP Country database file for IPv4, or 'disabled' to disable"
440+
" GeoIP lookup for IPv4 addresses."
441+
},
442+
{ "geoip_cc6", 0, "<path>|disabled",
443+
"Path to the GeoIP Country database file for IPv6, or 'disabled' to disable"
444+
" GeoIP lookup for IPv6 addresses."
445+
},
438446
{ "hash_rate", 0, "<speed>",
439447
"Maximum file hashing speed. See the `download_rate' setting for allowed"
440448
" formats for this setting."

src/geoip.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,37 @@ static GeoIP *geoip6;
3535
#endif
3636

3737

38-
void geoip_reinit() {
38+
void geoip_reinit(int v) {
3939
#ifdef USE_GEOIP
40-
if(geoip4)
41-
GeoIP_delete(geoip4);
42-
if(geoip6)
43-
GeoIP_delete(geoip6);
44-
/* Get the file paths directly, so that we can offer more useful diagnostic
45-
* messages in case we fail to open it. Calling GeoIP_db_avail() ensures that
46-
* the GeoIPDBFileName variable has been initialized. */
47-
if(!GeoIPDBFileName)
48-
GeoIP_db_avail(GEOIP_COUNTRY_EDITION);
49-
const char *f4 = GeoIPDBFileName[GEOIP_COUNTRY_EDITION];
50-
const char *f6 = GeoIPDBFileName[GEOIP_COUNTRY_EDITION_V6];
51-
52-
/* The '16' flag is GEOIP_SILENCE, but it's a fairly new option and not
53-
* defined in older versions. Just pass it along directly, ABI compatibility
54-
* should ensure this works with both old and new versions.
55-
* Also perform a g_file_test() first to ensure we're not opening
56-
* non-existing files. GeoIP versions that do not support GEOIP_SILENCE will
57-
* throw error messages on stdout, which screws up our ncurses UI, so
58-
* catching the most common error here is worth it. */
59-
geoip4 = g_file_test(f4, G_FILE_TEST_EXISTS) ? GeoIP_open(f4, 16 | GEOIP_MEMORY_CACHE) : NULL;
60-
geoip6 = g_file_test(f6, G_FILE_TEST_EXISTS) ? GeoIP_open(f6, 16 | GEOIP_MEMORY_CACHE) : NULL;
61-
if(!geoip4)
62-
ui_mf(uit_main_tab, 0, "Unable to open '%s', no country codes will be displayed for IPv4 addresses.", f4);
63-
if(!geoip6)
64-
ui_mf(uit_main_tab, 0, "Unable to open '%s', no country codes will be displayed for IPv6 addresses.", f6);
40+
GeoIP **var = v == 4 ? &geoip4 : &geoip6;
41+
if(*var) {
42+
GeoIP_delete(*var);
43+
*var = NULL;
44+
}
45+
46+
const char *fn = var_get(0, v == 4 ? VAR_geoip_cc4 : VAR_geoip_cc6);
47+
if(!fn) {
48+
/* Get the file paths directly, so that we can offer more useful diagnostic
49+
* messages in case we fail to open it. Calling GeoIP_db_avail() ensures that
50+
* the GeoIPDBFileName variable has been initialized. */
51+
if(!GeoIPDBFileName)
52+
GeoIP_db_avail(GEOIP_COUNTRY_EDITION);
53+
fn = GeoIPDBFileName[v == 4 ? GEOIP_COUNTRY_EDITION : GEOIP_COUNTRY_EDITION_V6];
54+
}
55+
56+
if(strcmp(fn, "disabled") != 0) {
57+
/* The '16' flag is GEOIP_SILENCE, but it's a fairly new option and not
58+
* defined in older versions. Just pass it along directly, ABI compatibility
59+
* should ensure this works with both old and new versions.
60+
* Also perform a g_file_test() first to ensure we're not opening
61+
* non-existing files. GeoIP versions that do not support GEOIP_SILENCE
62+
* will throw error messages on stdout/stderr, which screws up our ncurses
63+
* UI, so catching the most common error here is worth it. */
64+
*var = g_file_test(fn, G_FILE_TEST_EXISTS) ? GeoIP_open(fn, 16 | GEOIP_MEMORY_CACHE) : NULL;
65+
if(!*var)
66+
ui_mf(NULL, 0, "Can't open '%s', no country codes will be displayed for IPv%d addresses.", fn, v);
67+
}
68+
6569
geoip_available = geoip4 || geoip6;
6670
#endif
6771
}

src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ int main(int argc, char **argv) {
453453
dl_init_global();
454454
ui_cmdhist_init("history");
455455
ui_init(bracketed_paste);
456-
geoip_reinit();
456+
geoip_reinit(4);
457+
geoip_reinit(6);
457458

458459
// setup SIGWINCH
459460
struct sigaction act;

src/vars.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,21 @@ static char *i_ffc() {
603603
}
604604

605605

606+
// geoip
607+
608+
static gboolean s_geoip_cc(guint64 hub, const char *key, const char *val, GError **err) {
609+
#ifdef USE_GEOIP
610+
int v = strcmp(key, "geoip_cc4") == 0 ? 4 : 6;
611+
db_vars_set(hub, key, val);
612+
geoip_reinit(v);
613+
return TRUE;
614+
#else
615+
g_set_error(err, 1, 0, "This option can't be modified: %s.", "Ncdc has not been compiled with GeoIP support");
616+
return FALSE;
617+
#endif
618+
}
619+
620+
606621
// hubname
607622

608623
static char *p_hubname(const char *val, GError **err) {
@@ -947,6 +962,8 @@ struct var_t {
947962
V(filelist_maxage, 1,0, f_interval, p_interval, su_old, NULL, NULL, "604800")\
948963
V(fl_done, 0,0, NULL, NULL, NULL, NULL, NULL, "false")\
949964
V(flush_file_cache, 1,0, f_ffc, p_ffc, su_ffc, g_ffc, s_ffc, i_ffc())\
965+
V(geoip_cc4, 1,0, f_id, p_id, su_path, NULL, s_geoip_cc, NULL)\
966+
V(geoip_cc6, 1,0, f_id, p_id, su_path, NULL, s_geoip_cc, NULL)\
950967
V(hash_rate, 1,0, f_speed, p_speed, NULL, NULL, NULL, NULL)\
951968
V(hubaddr, 0,0, NULL, NULL, NULL, NULL, NULL, NULL)\
952969
V(hubkp, 0,0, NULL, NULL, NULL, NULL, NULL, NULL)\

0 commit comments

Comments
 (0)