feat(freeipapi): Added ability to use own API token

This commit is contained in:
Roman Kelesidis 2025-05-05 18:34:31 +03:00
commit c82ccd0d31
5 changed files with 42 additions and 17 deletions

View file

@ -475,7 +475,7 @@ if ($tor_reged && $tor_info) {
'ROW_BGR' => $row_bgr,
'NAME' => ($peer['update_time']) ? $name : "<s>$name</s>",
'PEER_ID' => $peerTorrentClient,
'COUNTRY' => $peerCountry,
'COUNTRY' => $bb_cfg['ip2country_settings']['enabled'] ? $peerCountry : false,
'COMPL_PRC' => $compl_perc,
'UP_TOTAL' => ($max_up_id[$x] == $pid) ? "<b>$up_tot</b>" : $up_tot,
'DOWN_TOTAL' => ($max_down_id[$x] == $pid) ? "<b>$down_tot</b>" : $down_tot,

View file

@ -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?

View file

@ -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');

View file

@ -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);
}
}
}

View file

@ -155,7 +155,7 @@ ajax.callback.callseed = function (data) {
<th class="{sorter: 'digit'}"><b class="tbs-text">{L_DL_PORT}</b><img width="75" class="spacer" src="{SPACER}" alt="" /></th>
<!-- END porthead -->
<th class="{sorter: false}"><b class="tbs-text">{L_DL_CLIENT}</b><img width="75" class="spacer" src="{SPACER}" alt="" /></th>
<th class="{sorter: false}"><b class="tbs-text">{L_COUNTRY}</b><img width="75" class="spacer" src="{SPACER}" alt="" /></th>
<!-- IF $bb_cfg['ip2country_settings']['enabled'] --><th class="{sorter: false}"><b class="tbs-text">{L_COUNTRY}</b><img width="75" class="spacer" src="{SPACER}" alt="" /></th><!-- ENDIF -->
</tr>
</thead>
<!-- BEGIN srow -->
@ -173,7 +173,7 @@ ajax.callback.callseed = function (data) {
<td>{sfull.srow.port.PORT}</td>
<!-- END port -->
<td>{sfull.srow.PEER_ID}</td>
<td>{sfull.srow.COUNTRY}</td>
<!-- IF sfull.srow.COUNTRY --><td>{sfull.srow.COUNTRY}</td><!-- ENDIF -->
</tr>
<!-- END srow -->
</table>
@ -208,7 +208,7 @@ ajax.callback.callseed = function (data) {
<th class="{sorter: 'digit'}"><b class="tbs-text">{L_DL_PORT}</b><img width="75" class="spacer" src="{SPACER}" alt="" /></th>
<!-- END porthead -->
<th class="{sorter: false}"><b class="tbs-text">{L_DL_CLIENT}</b><img width="75" class="spacer" src="{SPACER}" alt="" /></th>
<th class="{sorter: false}"><b class="tbs-text">{L_COUNTRY}</b><img width="75" class="spacer" src="{SPACER}" alt="" /></th>
<!-- IF $bb_cfg['ip2country_settings']['enabled'] --><th class="{sorter: false}"><b class="tbs-text">{L_COUNTRY}</b><img width="75" class="spacer" src="{SPACER}" alt="" /></th><!-- ENDIF -->
</tr>
</thead>
<!-- BEGIN lrow -->
@ -226,7 +226,7 @@ ajax.callback.callseed = function (data) {
<td>{lfull.lrow.port.PORT}</td>
<!-- END port -->
<td>{lfull.lrow.PEER_ID}</td>
<td>{lfull.lrow.COUNTRY}</td>
<!-- IF lfull.lrow.COUNTRY --><td>{lfull.lrow.COUNTRY}</td><!-- ENDIF -->
</tr>
<!-- END lrow -->
</table>