fix(ip-api): Add error handling and logging for freeipapi.com requests (#2006)

This commit is contained in:
Roman Kelesidis 2025-06-24 12:55:41 +03:00
commit 12ce6e783e
No known key found for this signature in database
GPG key ID: D8157C4D4C4C6DB4
2 changed files with 19 additions and 10 deletions

View file

@ -475,6 +475,8 @@ if ($tor_reged && $tor_info) {
if ($infoByIP = infoByIP((!empty($peer['ipv6']) ? $peer['ipv6'] : $peer['ip']), $peer['port'])) {
if (!empty($infoByIP['countryCode'])) {
$peerCountry = render_flag($infoByIP['countryCode'], false);
} else {
$peerCountry = $lang['NOT_AVAILABLE'];
}
}
}

View file

@ -2210,19 +2210,26 @@ function infoByIP(string $ipAddress, int $port = 0): array
}
$context = stream_context_create($contextOptions);
$response = file_get_contents($bb_cfg['ip2country_settings']['endpoint'] . $ipAddress, context: $context);
if ($response !== false) {
$json = json_decode($response, true);
try {
$response = file_get_contents($bb_cfg['ip2country_settings']['endpoint'] . $ipAddress, context: $context);
if (is_array($json) && !empty($json)) {
$data = [
'ipVersion' => $json['ipVersion'],
'countryCode' => $json['countryCode'],
'continent' => $json['continent'],
'continentCode' => $json['continentCode']
];
if ($response !== false) {
$json = json_decode($response, true);
if (is_array($json) && !empty($json)) {
$data = [
'ipVersion' => $json['ipVersion'],
'countryCode' => $json['countryCode'],
'continent' => $json['continent'],
'continentCode' => $json['continentCode']
];
}
} else {
bb_log("[FreeIPAPI] Failed to get IP info for: $ipAddress");
}
} catch (Exception $e) {
bb_log("[FreeIPAPI] " . $e->getMessage());
}
if (empty($data)) {