Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add module GeoLite Citys. #281

Merged
merged 34 commits into from
Jan 7, 2025
Merged
Changes from 30 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1d5e9af
add new module for profiles link in /whois response.
revrsefr Jul 3, 2024
9441690
remove ulines thing
revrsefr Jul 3, 2024
c36c6e3
Merge branch 'inspircd:master' into master
revrsefr Jul 5, 2024
1907dfe
Create m_ipinfo_io.cpp
revrsefr Jul 5, 2024
a48d250
Update m_ipinfo_io.cpp
revrsefr Jul 5, 2024
db925cc
Update m_ipinfo_io.cpp
revrsefr Jul 5, 2024
651d665
Update m_profileLink.cpp
revrsefr Jul 5, 2024
c74beb1
Update m_ipinfo_io.cpp
revrsefr Jul 5, 2024
b24f15d
Update m_ipinfo_io.cpp
revrsefr Jul 7, 2024
d2e18fc
Update m_ipinfo_io.cpp
revrsefr Jul 7, 2024
92e60bc
Create m_randomidxlines.cpp
revrsefr Jul 9, 2024
18f3d66
Merge branch 'inspircd:master' into master
revrsefr Jul 9, 2024
686662f
Update m_randomidxlines.cpp
revrsefr Jul 9, 2024
f5e1bdf
Update m_randomidxlines.cpp
revrsefr Jul 9, 2024
bb23989
Update m_randomidxlines.cpp
revrsefr Jul 9, 2024
36c0957
Merge branch 'inspircd:master' into master
revrsefr Jul 10, 2024
2ad4274
Update m_censor.cpp
revrsefr Jul 13, 2024
a73f28a
Merge pull request #1 from revrsedev/patch-2
revrsefr Jul 13, 2024
dc10c91
Update m_censor.cpp
revrsefr Jul 13, 2024
c64e5cc
Update m_censor.cpp
revrsefr Jul 13, 2024
c01f4b9
Update m_censor.cpp
revrsefr Jul 13, 2024
e1a7035
Merge branch 'inspircd:master' into master
revrsefr Sep 24, 2024
4b7344f
Merge branch 'inspircd:master' into master
revrsefr Nov 24, 2024
3be7b08
Merge branch 'inspircd:master' into master
revrsefr Nov 30, 2024
9a5d0e8
Merge branch 'inspircd:master' into master
revrsefr Dec 26, 2024
5600f9b
Merge branch 'inspircd:master' into master
revrsefr Jan 6, 2025
17cad1a
Create m_whoisgeolite.cpp
revrsefr Jan 7, 2025
4bf4798
Delete m_whoisgeolite.cpp
revrsefr Jan 7, 2025
cc0a8d6
Create m_whoisgeolite.cpp
revrsefr Jan 7, 2025
f34b784
Update m_censor.cpp
revrsefr Jan 7, 2025
acc6b26
Update m_whoisgeolite.cpp
revrsefr Jan 7, 2025
f7680f8
Update m_whoisgeolite.cpp
revrsefr Jan 7, 2025
23d7e92
Update m_whoisgeolite.cpp
revrsefr Jan 7, 2025
478f716
Update m_whoisgeolite.cpp
revrsefr Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions 4/m_whoisgeolite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2024 reverse
*
* This file contains a third-party module for InspIRCd. You can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.

* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/// $ModAuthor: reverse <mike.chevronnet@gmail.com>
/// $ModDesc: Adds city information to WHOIS using the MaxMind database.
/// $ModConfig: add path <geolite dbpath="path/geodata/GeoLite2-City.mmdb">
/// $ModDepends: core 4

/// $CompilerFlags: find_compiler_flags("libmaxminddb")
/// $LinkerFlags: find_linker_flags("libmaxminddb")

/// $PackageInfo: require_system("alpine") libmaxminddb-dev pkgconf
/// $PackageInfo: require_system("arch") pkgconf libmaxminddb
/// $PackageInfo: require_system("darwin") libmaxminddb pkg-config
/// $PackageInfo: require_system("debian~") libmaxminddb-dev pkg-config
/// $PackageInfo: require_system("rhel~") pkg-config libmaxminddb-devel

#include "inspircd.h"
#include "modules/whois.h"
#include <maxminddb.h>

class ModuleWhoisGeoLite final
: public Module
, public Whois::EventListener
{
private:
MMDB_s mmdb; // MaxMind database object
bool db_loaded; // Flag to indicate if the database was successfully loaded
std::string dbpath;

public:
ModuleWhoisGeoLite()
: Module(VF_OPTCOMMON, "Adds city information to WHOIS using the MaxMind database.")
, Whois::EventListener(this), db_loaded(false)
{
}

void ReadConfig(ConfigStatus& status) override
{
// Load configuration and get the path to the GeoLite2 database
auto& tag = ServerInstance->Config->ConfValue("geolite");
dbpath = tag->getString("dbpath", "/etc/GeoLite2-City.mmdb");

// Attempt to open the MaxMind GeoLite2-City database
int status_open = MMDB_open(dbpath.c_str(), MMDB_MODE_MMAP, &mmdb);
if (status_open != MMDB_SUCCESS) {
ServerInstance->SNO.WriteGlobalSno('a', "GeoLite2: Failed to open GeoLite2 database: " + std::string(MMDB_strerror(status_open)));
db_loaded = false;
} else {
ServerInstance->SNO.WriteGlobalSno('a', "GeoLite2: Successfully opened GeoLite2 database.");
db_loaded = true;
}
}

void OnWhois(Whois::Context& whois) override
{
User* source = whois.GetSource(); // The user issuing the WHOIS command
User* target = whois.GetTarget(); // The user being WHOIS'd

// Only allow IRC operators to see the city information
if (!source->IsOper()) {
return;
}

// Check if the user is remote
if (!IS_LOCAL(target)) {
whois.SendLine(RPL_WHOISSPECIAL, "*", "City: User is connected from a remote server.");
return;
}

LocalUser* luser = IS_LOCAL(target);

// Ensure the user is local and has a valid IP address
if (!luser || !luser->client_sa.is_ip()) {
whois.SendLine(RPL_WHOISSPECIAL, "*", "City: No valid IP address (possibly using a Unix socket).");
return;
}

// Ensure the MaxMind database is loaded
if (!db_loaded) {
whois.SendLine(RPL_WHOISSPECIAL, "*", "City: GeoLite2 database not loaded.");
return;
}

// Perform the GeoLite2 lookup using the socket address
int gai_error = 0;
const struct sockaddr* addr = reinterpret_cast<const struct sockaddr*>(&luser->client_sa);
MMDB_lookup_result_s result = MMDB_lookup_sockaddr(&mmdb, addr, &gai_error);

if (gai_error != 0) {
ServerInstance->SNO.WriteGlobalSno('a', "GeoLite2: getaddrinfo error: " + std::string(gai_strerror(gai_error)));
whois.SendLine(RPL_WHOISSPECIAL, "*", "City: Unknown (lookup error).");
return;
}

if (!result.found_entry) {
whois.SendLine(RPL_WHOISSPECIAL, "*", "City: Unknown (no database entry).");
return;
}

// Retrieve the city name
MMDB_entry_data_s city_data = {};
int status = MMDB_get_value(&result.entry, &city_data, "city", "names", "en", nullptr);

if (status == MMDB_SUCCESS && city_data.has_data) {
// If the city is found, add it to the WHOIS response
std::string city(city_data.utf8_string, city_data.data_size);
whois.SendLine(RPL_WHOISSPECIAL, "*", "is connecting from City: " + city);
} else {
// City not found
whois.SendLine(RPL_WHOISSPECIAL, "*", "is connecting from City: Unknown.");
}
}

~ModuleWhoisGeoLite() override
{
// Close the MaxMind database when the module is unloaded
if (db_loaded) {
MMDB_close(&mmdb);
}
}
};

MODULE_INIT(ModuleWhoisGeoLite)
Loading