From ed9f7cfb5749d00515211fbf5263f04bac6b6bf3 Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Sat, 30 Oct 2021 09:56:40 +0200 Subject: [PATCH] Add /GEOIP command so it's a bit easier to debug these things. Load geoip_classic with correct settings (for now) in modules.optional.conf. --- doc/conf/modules.optional.conf | 7 ++++ src/modules/geoip_base.c | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/doc/conf/modules.optional.conf b/doc/conf/modules.optional.conf index 3d477c21a..0553f384b 100644 --- a/doc/conf/modules.optional.conf +++ b/doc/conf/modules.optional.conf @@ -228,3 +228,10 @@ set { // }; //}; +loadmodule "geoip_classic"; +set { + geoip-classic { + ipv4-database "https://www.unrealircd.org/files/geo/classic/GeoIP.dat"; + ipv6-database "https://www.unrealircd.org/files/geo/classic/GeoIPv6.dat"; + } +} diff --git a/src/modules/geoip_base.c b/src/modules/geoip_base.c index a42315824..3244e1f7c 100644 --- a/src/modules/geoip_base.c +++ b/src/modules/geoip_base.c @@ -30,6 +30,7 @@ int geoip_connect_extinfo(Client *client, NameValuePrioList **list); int geoip_base_configtest(ConfigFile *cf, ConfigEntry *ce, int type, int *errs); int geoip_base_configrun(ConfigFile *cf, ConfigEntry *ce, int type); EVENT(geoip_base_set_existing_users_evt); +CMD_FUNC(cmd_geoip); ModDataInfo *geoip_md; /* Module Data structure which we acquire */ struct geoip_base_config_s geoip_base_config; @@ -124,6 +125,8 @@ MOD_INIT() HookAdd(modinfo->handle, HOOKTYPE_REMOTE_CONNECT, 0, geoip_base_handshake); /* remote user */ HookAdd(modinfo->handle, HOOKTYPE_WHOIS, 0, geoip_base_whois); + CommandAdd(modinfo->handle, "GEOIP", cmd_geoip, MAXPARA, CMD_USER); + /* set defaults */ geoip_base_config.check_on_load = 1; @@ -266,3 +269,58 @@ int geoip_base_whois(Client *client, Client *target, NameValuePrioList **list) geo->country_name); return 0; } + +CMD_FUNC(cmd_geoip) +{ + const char *ip = NULL; + Client *target; + GeoIPResult *res; + + if (!IsOper(client)) + { + sendnumeric(client, ERR_NOPRIVILEGES); + return; + } + + if ((parc < 2) || BadPtr(parv[1])) + { + /* Maybe some report */ + return; + } + + if (strchr(parv[1], '.') || strchr(parv[1], ':')) + { + ip = parv[1]; + } else { + target = find_user(parv[1], NULL); + if (!target) + { + sendnumeric(client, ERR_NOSUCHNICK, parv[1]); + return; + } + ip = target->ip; + if (!ip) + { + sendnotice(client, "User %s has no known IP address", client->name); // (eg: services bot) + return; + } + } + + res = geoip_lookup(ip); + + sendnotice(client, "*** GEOIP information for IP %s ***", ip); + if (!res) + { + sendnotice(client, "- No information available"); + return; + } else { + if (res->country_code) + sendnotice(client, "- Country code: %s", res->country_code); + if (res->country_name) + sendnotice(client, "- Country name: %s", res->country_name); + } + + free_geoip_result(res); + + sendnotice(client, "*** End of information ***"); +}