订阅信息缓存

dev
GeekQu 7 years ago
parent b8d383f8f3
commit c7d2b5f614
No known key found for this signature in database
GPG Key ID: F0EEABFCD04A5891

@ -582,4 +582,13 @@ class UserController extends AdminController
];
return json_encode($info, true);
}
public function cleanSubCache($request, $response, $args)
{
$id = $args['id'];
$user_path = (BASE_PATH . '/storage/SubscribeCache/' . $id . '/');
Tools::delDirAndFile($user_path);
return $response->withStatus(302)->withHeader('Location', '/admin/user');
}
}

@ -13,6 +13,7 @@ use App\Utils\Tools;
use App\Utils\URL;
use App\Services\Config;
use App\Services\AppsProfiles;
use Ramsey\Uuid\Uuid;
/**
* HomeController
@ -136,16 +137,28 @@ class LinkController extends BaseController
5 => 'V2Ray + SS + SSR'
];
// 请求路径以及查询参数
$path = ($request->getPath() . $request->getQuery());
$getBody = '';
foreach ($sub_type_array as $key => $value) {
if ($key != 'sub' && isset($opts[$key])) {
$int = (int) $opts[$key];
$class = ('get' . $value['class']);
if ($int >= 1) {
if (Config::get('enable_sub_cache') === true) {
$content = self::getSubscribeCache($user, $path);
if ($content === false) {
$content = self::$class($user, $int, $opts, $Rule, $find, $emoji);
}
self::SubscribeCache($user, $path, $content);
} else {
$content = self::$class($user, $int, $opts, $Rule, $find, $emoji);
}
$getBody = self::getBody(
$user,
$response,
self::$class($user, $int, $opts, $Rule, $find, $emoji),
$content,
$value['filename']
);
$subscribe_type = $value['class'];
@ -163,10 +176,19 @@ class LinkController extends BaseController
$int = 1;
}
$subscribe_type = $sub_int_type[$int];
if (Config::get('enable_sub_cache') === true) {
$content = self::getSubscribeCache($user, $path);
if ($content === false) {
$content = self::getSub($user, $int, $opts, $Rule, $find, $emoji);
}
self::SubscribeCache($user, $path, $content);
} else {
$content = self::getSub($user, $int, $opts, $Rule, $find, $emoji);
}
$getBody = self::getBody(
$user,
$response,
self::getSub($user, $int, $opts, $Rule, $find, $emoji),
$content,
$value['filename']
);
}
@ -179,6 +201,49 @@ class LinkController extends BaseController
return $getBody;
}
/**
* 获取订阅文件缓存
*
* @param object $user 用户
* @param string $path 路径以及查询参数
*
*/
private static function getSubscribeCache($user, $path)
{
$user_path = (BASE_PATH . '/storage/SubscribeCache/' . $user->id . '/');
if (!is_dir($user_path)) mkdir($user_path);
$user_path_hash = ($user_path . Uuid::uuid3(Uuid::NAMESPACE_DNS, $path)->toString());
if (!is_file($user_path_hash)) return false;
$filemtime = filemtime($user_path_hash);
if ($filemtime === false) {
unlink($user_path_hash);
return false;
}
if ((time() - $filemtime) >= (Config::get('sub_cache_time') * 60)) {
unlink($user_path_hash);
return false;
}
return file_get_contents($user_path_hash);
}
/**
* 订阅文件写入缓存
*
* @param object $user 用户
* @param string $path 路径以及查询参数
*
*/
private static function SubscribeCache($user, $path, $content)
{
$user_path = (BASE_PATH . '/storage/SubscribeCache/' . $user->id . '/');
if (!is_dir($user_path)) mkdir($user_path);
$user_path_hash = ($user_path . Uuid::uuid3(Uuid::NAMESPACE_DNS, $path)->toString());
$file = fopen($user_path_hash, 'wb');
fwrite($file, $content);
fclose($file);
}
/**
* 记录订阅日志
*

@ -1968,4 +1968,19 @@ class UserController extends BaseController
return $newResponse;
}
/**
* 清理订阅缓存
*
* @param Request $request
* @param Response $response
* @param array $args
*/
public function cleanSubCache($request, $response, $args)
{
$user_path = (BASE_PATH . '/storage/SubscribeCache/' . $this->user->id . '/');
Tools::delDirAndFile($user_path);
return $response->withStatus(302)->withHeader('Location', '/user');
}
}

@ -793,4 +793,25 @@ class Tools
}
closedir($handle);
}
/**
* 清空文件夹
*
* @param string $dirName
*/
public static function delDirAndFile($dirPath)
{
if ($handle = opendir($dirPath)){
while (false !== ($item = readdir($handle))){
if ($item != '.' && $item != '..'){
if (is_dir($dirPath . '/' . $item)){
self::delDirAndFile($dirPath . '/' . $item);
} else {
unlink($dirPath . '/' . $item);
}
}
}
closedir($handle);
}
}
}

@ -323,6 +323,8 @@ $_ENV['mu_port_migration'] = false; //为后端直接下发偏移后的端
$_ENV['relay_port_migration'] = false; //为中转规则下发偏移后的端口,此项未经测试
$_ENV['add_emoji_to_node_name'] = false; //为部分订阅中默认添加 emoji
$_ENV['add_appName_to_ss_uri'] = true; //为 SS 节点名称中添加站点名
$_ENV['enable_sub_cache'] = false; //订阅信息缓存
$_ENV['sub_cache_time'] = 3600; //订阅信息缓存有效时间 (分钟)
// 审计自动封禁开关
$_ENV['enable_auto_detect_ban'] = false;

@ -158,6 +158,9 @@ $app->group('/user', function () {
// getPcClient
$this->get('/getPcClient', App\Controllers\UserController::class . ':getPcClient');
// CleanSubCache
$this->get('/cleanSubCache', App\Controllers\UserController::class . ':cleanSubCache');
})->add(new Auth());
$app->group('/payment', function () {
@ -315,6 +318,9 @@ $app->group('/admin', function () {
$this->get('/logout', App\Controllers\AdminController::class . ':logout');
$this->post('/payback/ajax', App\Controllers\AdminController::class . ':ajax_payback');
// CleanSubCache
$this->get('/user/{id}/cleanSubCache', App\Controllers\Admin\UserController::class . ':cleanSubCache');
// Config Mange
$this->group('/config', function () {
$this->get('/telegram', App\Controllers\Admin\GConfigController::class . ':telegram');

@ -11,6 +11,20 @@
<div class="col-lg-12 col-sm-12">
<section class="content-inner margin-top-no">
<div class="card">
<div class="card-main">
<div class="card-inner">
<div class="form-group">
<div class="row">
<div class="col-md-10 col-md-push-1">
<a class="btn btn-block btn-brand waves-attach waves-light" href="/admin/user/{$edit_user->id}/cleanSubCache">清空订阅缓存</a>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-main">
<div class="card-inner">

@ -294,12 +294,23 @@ table tr td:first-child {
<li>
<a class="" data-toggle="tab" href="#info_center"><i class="icon icon-lg">flight_takeoff</i>&nbsp;连接信息</a>
</li>
<li>
<a class="" data-toggle="tab" href="#other"><i class="icon icon-lg">add_circle</i>&nbsp;其他</a>
</li>
</ul>
</nav>
<div class="card-inner">
<div class="tab-content">
<div class="tab-pane fade" id="other">
<p>清理订阅缓存:
<a class="kaobei copy-text btn btn-subscription" type="button" href="/user/cleanSubCache">点击清理</a>
</p>
</div>
<div class="tab-pane fade" id="info_center">
<p>您的链接信息:</p>

Loading…
Cancel
Save