Added IPHelper implementation (#631)

This commit is contained in:
Roman Kelesidis 2023-03-20 13:52:28 +07:00 committed by GitHub
commit ef4e1aa18f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 152 additions and 60 deletions

View file

@ -259,7 +259,7 @@ if ($log_rowset) {
'USER_ID' => $row['log_user_id'],
'USERNAME' => profile_url($row),
'USER_HREF_S' => url_arg($url, $user_key, $row['log_user_id']),
'USER_IP' => Longman\IPTools\Ip::isValid($row['log_user_ip']) ? decode_ip($row['log_user_ip']) : '127.0.0.1',
'USER_IP' => \TorrentPier\Helpers\IPHelper::isValid($row['log_user_ip']) ? \TorrentPier\Helpers\IPHelper::decodeIP($row['log_user_ip']) : '127.0.0.1',
'FORUM_ID' => $row['log_forum_id'],
'FORUM_HREF' => BB_ROOT . FORUM_URL . $row['log_forum_id'],

View file

@ -34,8 +34,8 @@ if (isset($_POST['submit'])) {
$ip_list_temp = explode(',', $_POST['ban_ip']);
foreach ($ip_list_temp as $ip) {
if (Longman\IPTools\Ip::isValid($ip)) {
$ip_list[] = encode_ip($ip);
if (\TorrentPier\Helpers\IPHelper::isValid($ip)) {
$ip_list[] = \TorrentPier\Helpers\IPHelper::encodeIP($ip);
}
}
}
@ -215,7 +215,7 @@ if (isset($_POST['submit'])) {
$ban_id = $banlist[$i]['ban_id'];
if (!empty($banlist[$i]['ban_ip'])) {
$ban_ip = str_replace('255', '*', decode_ip($banlist[$i]['ban_ip']));
$ban_ip = str_replace('255', '*', \TorrentPier\Helpers\IPHelper::decodeIP($banlist[$i]['ban_ip']));
$select_iplist .= '<option value="' . $ban_id . '">' . $ban_ip . '</option>';
$ipban_count++;
} elseif (!empty($banlist[$i]['ban_email'])) {

View file

@ -299,8 +299,8 @@ if (!isset($_REQUEST['dosearch'])) {
unset($users);
$users = [];
if (Longman\IPTools\Ip::isValid($ip_address)) {
$ip = encode_ip($ip_address);
if (\TorrentPier\Helpers\IPHelper::isValid($ip_address)) {
$ip = \TorrentPier\Helpers\IPHelper::encodeIP($ip_address);
$users[] = $ip;
} else {
bb_die($lang['SEARCH_INVALID_IP']);

View file

@ -150,7 +150,7 @@ if (isset($_GET['pane']) && $_GET['pane'] == 'left') {
if ($onlinerow_reg[$i]['user_id'] == $userdata['user_id'] || !bf($onlinerow_reg[$i]['user_opt'], 'user_opt', 'user_viewonline')) {
$reg_userid_ary[] = $onlinerow_reg[$i]['user_id'];
$row_class = 'row1';
$reg_ip = decode_ip($onlinerow_reg[$i]['session_ip']);
$reg_ip = \TorrentPier\Helpers\IPHelper::decodeIP($onlinerow_reg[$i]['session_ip']);
$template->assign_block_vars('reg_user_row', array(
'ROW_CLASS' => $row_class,
@ -173,7 +173,7 @@ if (isset($_GET['pane']) && $_GET['pane'] == 'left') {
$guest_userip_ary[] = $onlinerow_guest[$i]['session_ip'];
$guest_users++;
$row_class = 'row2';
$guest_ip = decode_ip($onlinerow_guest[$i]['session_ip']);
$guest_ip = \TorrentPier\Helpers\IPHelper::decodeIP($onlinerow_guest[$i]['session_ip']);
$template->assign_block_vars('guest_user_row', array(
'ROW_CLASS' => $row_class,

View file

@ -104,11 +104,11 @@ if (!$bb_cfg['ignore_reported_ip'] && isset($_GET['ip']) && $ip !== $_GET['ip'])
}
}
// Check that IP format is valid
if (!verify_ip($ip)) {
if (!\TorrentPier\Helpers\IPHelper::isValid($ip)) {
msg_die("Invalid IP: $ip");
}
// Convert IP to HEX format
$ip_sql = encode_ip($ip);
$ip_sql = \TorrentPier\Helpers\IPHelper::encodeIP($ip);
// Peer unique id
$peer_hash = md5(
@ -372,13 +372,13 @@ if (!$output) {
$peers = '';
foreach ($rowset as $peer) {
$peers .= pack('Nn', ip2long(decode_ip($peer['ip'])), $peer['port']);
$peers .= pack('Nn', \TorrentPier\Helpers\IPHelper::encodeIP(\TorrentPier\Helpers\IPHelper::decodeIP($peer['ip'])), $peer['port']);
}
} else {
$peers = [];
foreach ($rowset as $peer) {
$peers[] = ['ip' => decode_ip($peer['ip']), 'port' => (int)$peer['port']];
$peers[] = ['ip' => \TorrentPier\Helpers\IPHelper::decodeIP($peer['ip']), 'port' => (int)$peer['port']];
}
}

View file

@ -304,37 +304,6 @@ function clean_filename($fname)
return str_replace($s, '_', str_compact($fname));
}
/**
* Декодирование оригинального IP
* @param $ip
* @return string
*/
function encode_ip($ip)
{
return Longman\IPTools\Ip::ip2long($ip);
}
/**
* Восстановление декодированного IP
* @param $ip
* @return string
*/
function decode_ip($ip)
{
return Longman\IPTools\Ip::long2ip($ip);
}
/**
* Проверка IP на валидность
*
* @param $ip
* @return bool
*/
function verify_ip($ip)
{
return Longman\IPTools\Ip::isValid($ip);
}
function bb_crc32($str)
{
return (float)sprintf('%u', crc32($str));
@ -469,7 +438,7 @@ function log_request($file = '', $prepend_str = false, $add_post = true)
if (!defined('IN_TRACKER')) {
require INC_DIR . '/init_bb.php';
} else {
define('DUMMY_PEER', pack('Nn', ip2long($_SERVER['REMOTE_ADDR']), !empty($_GET['port']) ? (int)$_GET['port'] : random_int(1000, 65000)));
define('DUMMY_PEER', pack('Nn', \TorrentPier\Helpers\IPHelper::encodeIP($_SERVER['REMOTE_ADDR']), !empty($_GET['port']) ? (int)$_GET['port'] : random_int(1000, 65000)));
function dummy_exit($interval = 1800)
{

View file

@ -112,8 +112,8 @@ switch ($mode) {
} elseif ($profiledata['user_level'] == MOD && IS_MOD) {
$reg_ip = $last_ip = $lang['HIDDEN'];
} else {
$user_reg_ip = decode_ip($profiledata['user_reg_ip']);
$user_last_ip = decode_ip($profiledata['user_last_ip']);
$user_reg_ip = \TorrentPier\Helpers\IPHelper::decodeIP($profiledata['user_reg_ip']);
$user_last_ip = \TorrentPier\Helpers\IPHelper::decodeIP($profiledata['user_last_ip']);
$reg_ip = '<a href="' . $bb_cfg['whois_info'] . $user_reg_ip . '" class="gen" target="_blank">' . $user_reg_ip . '</a>';
$last_ip = '<a href="' . $bb_cfg['whois_info'] . $user_last_ip . '" class="gen" target="_blank">' . $user_last_ip . '</a>';
}

View file

@ -585,12 +585,12 @@ function bt_show_ip($ip, $port = '')
global $bb_cfg;
if (IS_AM) {
$ip = decode_ip($ip);
$ip = \TorrentPier\Helpers\IPHelper::decodeIP($ip);
$ip .= ($port) ? ":$port" : '';
return $ip;
}
return ($bb_cfg['bt_show_ip_only_moder']) ? false : decode_ip_xx($ip);
return ($bb_cfg['bt_show_ip_only_moder']) ? false : \TorrentPier\Helpers\IPHelper::anonymizeIP($ip);
}
function bt_show_port($port)
@ -604,12 +604,6 @@ function bt_show_port($port)
return ($bb_cfg['bt_show_port_only_moder']) ? false : $port;
}
function decode_ip_xx($ip)
{
$h = explode('.', chunk_split($ip, 2, '.'));
return hexdec($h[0]) . '.' . hexdec($h[1]) . '.' . hexdec($h[2]) . '.xx';
}
function checkbox_get_val(&$key, &$val, $default = 1, $on = 1, $off = 0)
{
global $previous_settings, $search_id;
@ -762,7 +756,7 @@ function str_short($text, $max_length, $space = ' ')
if ($max_length && mb_strlen($text, 'UTF-8') > $max_length) {
$text = mb_substr($text, 0, $max_length, 'UTF-8');
if ($last_space_pos = $max_length - (int)strpos(strrev($text), (string) $space)) {
if ($last_space_pos = $max_length - (int)strpos(strrev($text), (string)$space)) {
if ($last_space_pos > round($max_length * 3 / 4)) {
$last_space_pos--;
$text = mb_substr($text, 0, $last_space_pos, 'UTF-8');

View file

@ -28,7 +28,7 @@ $user = null;
// Obtain and encode user IP
$client_ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
$user_ip = encode_ip($client_ip);
$user_ip = \TorrentPier\Helpers\IPHelper::encodeIP($client_ip);
define('CLIENT_IP', $client_ip);
define('USER_IP', $user_ip);

View file

@ -587,7 +587,7 @@ switch ($mode) {
bb_die($lang['NO_SUCH_POST']);
}
if (!$ip_this_post = decode_ip($post_row['poster_ip'])) {
if (!$ip_this_post = \TorrentPier\Helpers\IPHelper::decodeIP($post_row['poster_ip'])) {
$ip_this_post = $lang['NOT_AVAILABLE'];
}
@ -619,7 +619,7 @@ switch ($mode) {
continue;
}
if (!$ip = decode_ip($row['poster_ip'])) {
if (!$ip = \TorrentPier\Helpers\IPHelper::decodeIP($row['poster_ip'])) {
$ip = $lang['NOT_AVAILABLE'];
}

129
src/Helpers/IPHelper.php Normal file
View file

@ -0,0 +1,129 @@
<?php
/**
* TorrentPier Bull-powered BitTorrent tracker engine
*
* @copyright Copyright (c) 2005-2023 TorrentPier (https://torrentpier.com)
* @link https://github.com/torrentpier/torrentpier for the canonical source repository
* @license https://github.com/torrentpier/torrentpier/blob/master/LICENSE MIT License
*/
namespace TorrentPier\Helpers;
use Longman\IPTools\Ip;
use function strlen;
/**
* Class IPHelper
* @package TorrentPier\Helpers
*/
class IPHelper
{
/**
* Decode original IP
*
* @param $ip
*
* @return string
*/
public static function encodeIP($ip): string
{
return Ip::ip2long($ip);
}
/**
* Recovery of decoded IP
*
* @param $ip
*
* @return string
*/
public static function decodeIP($ip): string
{
return Ip::long2ip($ip);
}
/**
* Checking IP for validity
*
* @param $ip
*
* @return bool
*/
public static function isValid($ip): bool
{
return Ip::isValid($ip);
}
/**
* Checking if it is a local IP
*
* @param $ip
*
* @return bool
*/
public static function isLocal($ip): bool
{
return IP::isLocal($ip);
}
/**
* Checking if it is a remote IP
*
* @param $ip
*
* @return bool
*/
public static function isRemote($ip): bool
{
return IP::isRemote($ip);
}
/**
* Compare IP
*
* @param $ip
* @param $range
*
* @return bool
*/
public static function compareIP($ip, $range): bool
{
return Ip::compare($ip, $range);
}
/**
* Anonymizes an IP/IPv6.
*
* Removes the last byte for v4 and the last 8 bytes for v6 IPs
*
* -------------------------------------------------------------
* From Symfony
*/
public static function anonymizeIP(string $ip): string
{
$wrappedIPv6 = false;
if ('[' === substr($ip, 0, 1) && ']' === substr($ip, -1, 1)) {
$wrappedIPv6 = true;
$ip = substr($ip, 1, -1);
}
$packedAddress = inet_pton($ip);
if (4 === strlen($packedAddress)) {
$mask = '255.255.255.0';
} elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff:ffff'))) {
$mask = '::ffff:ffff:ff00';
} elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff'))) {
$mask = '::ffff:ff00';
} else {
$mask = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000';
}
$ip = inet_ntop($packedAddress & inet_pton($mask));
if ($wrappedIPv6) {
$ip = '[' . $ip . ']';
}
return $ip;
}
}