finish seed box basic

This commit is contained in:
xiaomlove
2022-07-23 15:05:32 +08:00
parent b507c41bf0
commit 42bf8f0467
32 changed files with 644 additions and 249 deletions

View File

@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.7.18');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2022-07-19');
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.7.19');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2022-07-23');
defined('IN_TRACKER') || define('IN_TRACKER', true);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");

View File

@@ -1751,7 +1751,13 @@ function get_ip_location($ip)
* @since 1.7.4
*/
$arr = get_ip_location_from_geoip($ip);
$result = array($arr["name"] ?? $lang_functions['text_unknown'], $lang_functions['text_user_ip'] . ":&nbsp;" . trim($ip, ','));
$result = [];
if ($arr) {
$result[] = $arr['name'];
} else {
$result[] = $lang_functions['text_unknown'];
}
$result[] = $lang_functions['text_user_ip'] . ":&nbsp;" . trim($ip, ',');
return $locations[$ip] = $result;
$cacheKey = "location_$ip";
@@ -5513,7 +5519,7 @@ function can_access_torrent($torrent)
return false;
}
function get_ip_location_from_geoip($ip)
function get_ip_location_from_geoip($ip): bool|array
{
$database = nexus_env('GEOIP2_DATABASE');
if (empty($database)) {
@@ -5535,49 +5541,33 @@ function get_ip_location_from_geoip($ip)
'en' => 'en',
];
$locale = $langMap[$lang] ?? $lang;
$result = [];
foreach (explode(',', $ip) as $__ip) {
if (empty($__ip)) {
continue;
}
$locationInfo = \Nexus\Database\NexusDB::remember("locations_{$__ip}", 3600, function () use ($locale, $__ip, $reader) {
$info = [
'ip' => $__ip,
'version' => '',
'country' => '',
'city' => '',
];
try {
$record = $reader->city($__ip);
$countryName = $record->country->names[$locale] ?? $record->country->names['en'] ?? '';
$cityName = $record->city->names[$locale] ?? $record->city->names['en'] ?? '';
if (isIPV4($__ip)) {
$info['version'] = 4;
} elseif (isIPV6($__ip)) {
$info['version'] = 6;
}
$info['country'] = $countryName;
$info['city'] = $cityName;
} catch (\Exception $exception) {
do_log($exception->getMessage() . $exception->getTraceAsString(), 'error');
$locationInfo = \Nexus\Database\NexusDB::remember("locations_{$ip}", 3600, function () use ($locale, $ip, $reader) {
$info = [
'ip' => $ip,
'version' => '',
'country' => '',
'city' => '',
];
try {
$record = $reader->city($ip);
$countryName = $record->country->names[$locale] ?? $record->country->names['en'] ?? '';
$cityName = $record->city->names[$locale] ?? $record->city->names['en'] ?? '';
if (isIPV4($ip)) {
$info['version'] = 4;
} elseif (isIPV6($ip)) {
$info['version'] = 6;
}
return $info;
});
$result[] = $locationInfo;
}
usort($result, function ($a, $b) {
if ($a['version'] == $b['version']) {
return 0;
$info['country'] = $countryName;
$info['city'] = $cityName;
} catch (\Exception $exception) {
do_log($exception->getMessage() . $exception->getTraceAsString(), 'error');
}
return $a['version'] > $b['version'] ? 1 : -1;
return $info;
});
do_log("ip: $ip, locale: $locale, result: " . nexus_json_encode($result));
$names = [];
foreach ($result as $item) {
$names[] = sprintf('%s[v%s]', $item['city'] ? ($item['city'] . "·" . $item['country']) : $item['country'], $item['version']);
}
do_log("ip: $ip, locale: $locale, result: " . nexus_json_encode($locationInfo));
$name = sprintf('%s[v%s]', $locationInfo['city'] ? ($locationInfo['city'] . "·" . $locationInfo['country']) : $locationInfo['country'], $locationInfo['version']);
return [
'name' => implode(" + ", $names),
'name' => $name,
'location_main' => '',
'location_sub' => '',
'flagpic' => '',

View File

@@ -767,4 +767,45 @@ function do_action($name, ...$args)
return $hook->doAction(...func_get_args());
}
function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool
{
$redis = \Nexus\Database\NexusDB::redis();
$key = "nexus_is_ip_seed_box";
$hashKey = "ip:$ip:uid:$uid";
$cacheData = $redis->hGet($key, $hashKey);
if ($cacheData && !$withoutCache) {
$cacheDataOriginal = unserialize($cacheData);
if ($cacheDataOriginal['deadline'] > time()) {
do_log("$hashKey, get result from cache: " . json_encode($cacheDataOriginal));
return $cacheDataOriginal['data'];
}
}
$ipObject = \PhpIP\IP::create($ip);
$ipNumeric = $ipObject->numeric();
$ipVersion = $ipObject->getVersion();
$checkSeedBoxAdminSql = sprintf(
'select id from seed_box_records where `ip_begin_numeric` <= "%s" and `ip_end_numeric` >= "%s" and `type` = %s and `version` = %s and `status` = %s limit 1',
$ipNumeric, $ipNumeric, \App\Models\SeedBoxRecord::TYPE_ADMIN, $ipVersion, \App\Models\SeedBoxRecord::STATUS_ALLOWED
);
$res = \Nexus\Database\NexusDB::select($checkSeedBoxAdminSql);
if (!empty($res)) {
$redis->hSet($key, $hashKey, serialize(['data' => true, 'deadline' => time() + 3600]));
do_log("$hashKey, get result from admin, true");
return true;
}
if ($uid !== null) {
$checkSeedBoxUserSql = sprintf(
'select id from seed_box_records where `ip_begin_numeric` <= "%s" and `ip_end_numeric` >= "%s" and `uid` = %s and `type` = %s and `version` = %s and `status` = %s limit 1',
$ipNumeric, $ipNumeric, $uid, \App\Models\SeedBoxRecord::TYPE_USER, $ipVersion, \App\Models\SeedBoxRecord::STATUS_ALLOWED
);
$res = \Nexus\Database\NexusDB::select($checkSeedBoxUserSql);
if (!empty($res)) {
$redis->hSet($key, $hashKey, serialize(['data' => true, 'deadline' => time() + 3600]));
do_log("$hashKey, get result from user, true");
return true;
}
}
$redis->hSet($key, $hashKey, serialize(['data' => false, 'deadline' => time() + 3600]));
do_log("$hashKey, no result, false");
return false;
}