From f1d6e74e5d4c74b6e12e9e742f60f62e71783d11 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Tue, 24 Jun 2025 12:55:41 +0300 Subject: [PATCH] fix(ip-api): Add error handling and logging for freeipapi.com requests (#2006) --- library/attach_mod/displaying_torrent.php | 2 ++ library/includes/functions.php | 27 ++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/library/attach_mod/displaying_torrent.php b/library/attach_mod/displaying_torrent.php index edded7f47..18c6a5bbf 100644 --- a/library/attach_mod/displaying_torrent.php +++ b/library/attach_mod/displaying_torrent.php @@ -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']; } } } diff --git a/library/includes/functions.php b/library/includes/functions.php index 6398907c9..79c187db0 100644 --- a/library/includes/functions.php +++ b/library/includes/functions.php @@ -2196,19 +2196,26 @@ function infoByIP(string $ipAddress, int $port = 0): array } $context = stream_context_create($contextOptions); - $response = file_get_contents(config()->get('ip2country_settings.endpoint') . $ipAddress, context: $context); - if ($response !== false) { - $json = json_decode($response, true); + try { + $response = file_get_contents(config()->get('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)) {