From 12ce6e783ec97a6c3df0e11273944a3e6cfe466d 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 06a670266..ce6bf669a 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 67256eb1f..948208667 100644 --- a/library/includes/functions.php +++ b/library/includes/functions.php @@ -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)) {