From c82ccd0d31ef8f6cd6de0d945e6b509f02f13975 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Mon, 5 May 2025 18:34:31 +0300 Subject: [PATCH] feat(freeipapi): Added ability to use own API token --- library/attach_mod/displaying_torrent.php | 2 +- library/config.php | 8 ++++ library/defines.php | 3 -- library/includes/functions.php | 38 ++++++++++++++----- .../templates/default/viewtopic_torrent.tpl | 8 ++-- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/library/attach_mod/displaying_torrent.php b/library/attach_mod/displaying_torrent.php index 426bce393..5f61a98d3 100644 --- a/library/attach_mod/displaying_torrent.php +++ b/library/attach_mod/displaying_torrent.php @@ -475,7 +475,7 @@ if ($tor_reged && $tor_info) { 'ROW_BGR' => $row_bgr, 'NAME' => ($peer['update_time']) ? $name : "$name", 'PEER_ID' => $peerTorrentClient, - 'COUNTRY' => $peerCountry, + 'COUNTRY' => $bb_cfg['ip2country_settings']['enabled'] ? $peerCountry : false, 'COMPL_PRC' => $compl_perc, 'UP_TOTAL' => ($max_up_id[$x] == $pid) ? "$up_tot" : $up_tot, 'DOWN_TOTAL' => ($max_down_id[$x] == $pid) ? "$down_tot" : $down_tot, diff --git a/library/config.php b/library/config.php index 232f0f96d..55de1ba8d 100644 --- a/library/config.php +++ b/library/config.php @@ -144,6 +144,14 @@ $bb_cfg['torr_server'] = [ 'disable_for_guest' => true ]; +// FreeIPAPI settings +$bb_cfg['ip2country_settings'] = [ + // Documentation: https://docs.freeipapi.com/ + 'enabled' => true, + 'endpoint' => 'https://freeipapi.com/api/json/', + 'api_token' => '', // not required for basic usage +]; + // FAQ url help link $bb_cfg['how_to_download_url_help'] = 'viewtopic.php?t=1'; // How to download? $bb_cfg['what_is_torrent_url_help'] = 'viewtopic.php?t=2'; // What is a torrent? diff --git a/library/defines.php b/library/defines.php index d7078a7fd..dbb636585 100644 --- a/library/defines.php +++ b/library/defines.php @@ -35,9 +35,6 @@ define('UPDATER_URL', 'https://api.github.com/repos/torrentpier/torrentpier/rele define('UPDATER_FILE', INT_DATA_DIR . '/updater.json'); define('COOKIE_DBG', 'bb_dbg'); -// TODO: Move in another section -define('API_IP_URL', 'https://freeipapi.com/api/json/'); - // Templates define('ADMIN_TPL_DIR', TEMPLATES_DIR . '/admin/'); define('XS_USE_ISSET', '1'); diff --git a/library/includes/functions.php b/library/includes/functions.php index bf055495b..943648d29 100644 --- a/library/includes/functions.php +++ b/library/includes/functions.php @@ -2186,20 +2186,40 @@ function readUpdaterFile(): array|bool */ function infoByIP(string $ipAddress, int $port = 0): array { + global $bb_cfg; + + if (!$bb_cfg['ip2country_settings']['enabled']) { + return []; + } + $ipAddress = \TorrentPier\Helpers\IPHelper::long2ip_extended($ipAddress); $cacheName = hash('xxh128', ($ipAddress . '_' . $port)); + if (!$data = CACHE('bb_ip2countries')->get($cacheName)) { $data = []; - $response = file_get_contents(API_IP_URL . $ipAddress); - $json = json_decode($response, true); - if (is_array($json) && !empty($json)) { - $data = [ - 'ipVersion' => $json['ipVersion'], - 'countryCode' => $json['countryCode'], - 'continent' => $json['continent'], - 'continentCode' => $json['continentCode'] + + $contextOptions = []; + if (!empty($bb_cfg['ip2country_settings']['api_token'])) { + $contextOptions['http'] = [ + 'header' => "Authorization: Bearer " . $bb_cfg['ip2country_settings']['api_token'] . "\r\n" ]; - CACHE('bb_ip2countries')->set($cacheName, $data, 1200); + } + + $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); + + if (is_array($json) && !empty($json)) { + $data = [ + 'ipVersion' => $json['ipVersion'], + 'countryCode' => $json['countryCode'], + 'continent' => $json['continent'], + 'continentCode' => $json['continentCode'] + ]; + CACHE('bb_ip2countries')->set($cacheName, $data, 1200); + } } } diff --git a/styles/templates/default/viewtopic_torrent.tpl b/styles/templates/default/viewtopic_torrent.tpl index 8d7b621e4..e111ffa13 100644 --- a/styles/templates/default/viewtopic_torrent.tpl +++ b/styles/templates/default/viewtopic_torrent.tpl @@ -155,7 +155,7 @@ ajax.callback.callseed = function (data) { {L_DL_PORT} {L_DL_CLIENT} - {L_COUNTRY} + {L_COUNTRY} @@ -173,7 +173,7 @@ ajax.callback.callseed = function (data) { {sfull.srow.port.PORT} {sfull.srow.PEER_ID} - {sfull.srow.COUNTRY} + {sfull.srow.COUNTRY} @@ -208,7 +208,7 @@ ajax.callback.callseed = function (data) { {L_DL_PORT} {L_DL_CLIENT} - {L_COUNTRY} + {L_COUNTRY} @@ -226,7 +226,7 @@ ajax.callback.callseed = function (data) { {lfull.lrow.port.PORT} {lfull.lrow.PEER_ID} - {lfull.lrow.COUNTRY} + {lfull.lrow.COUNTRY}