v2board/app/Http/Controllers/Client/ClientController.php

61 lines
2.4 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
2020-01-29 16:08:50 +08:00
namespace App\Http\Controllers\Client;
2019-10-29 15:33:36 +08:00
use App\Http\Controllers\Client\Protocols\V2rayN;
2019-10-29 15:33:36 +08:00
use App\Http\Controllers\Controller;
2020-06-12 00:18:35 +08:00
use App\Services\ServerService;
2020-01-29 16:08:50 +08:00
use Illuminate\Http\Request;
2020-02-28 01:12:55 +08:00
use App\Services\UserService;
2019-10-29 15:33:36 +08:00
class ClientController extends Controller
{
2020-01-11 13:36:52 +08:00
public function subscribe(Request $request)
{
2020-10-24 23:08:14 +08:00
$flag = $request->input('flag')
2022-12-02 13:52:20 +08:00
?? ($_SERVER['HTTP_USER_AGENT'] ?? '');
2020-10-24 23:08:14 +08:00
$flag = strtolower($flag);
2019-10-29 15:33:36 +08:00
$user = $request->user;
2020-02-07 19:34:24 +08:00
// account not expired and is not banned.
2020-02-28 17:16:05 +08:00
$userService = new UserService();
if ($userService->isAvailable($user)) {
2020-06-12 00:18:35 +08:00
$serverService = new ServerService();
2020-11-14 17:26:17 +08:00
$servers = $serverService->getAvailableServers($user);
$this->setSubscribeInfoToServers($servers, $user);
2020-10-24 23:08:14 +08:00
if ($flag) {
2023-03-08 02:31:46 +08:00
foreach (array_reverse(glob(app_path('Http//Controllers//Client//Protocols') . '/*.php')) as $file) {
2021-07-02 22:34:42 +08:00
$file = 'App\\Http\\Controllers\\Client\\Protocols\\' . basename($file, '.php');
$class = new $file($user, $servers);
if (strpos($flag, $class->flag) !== false) {
die($class->handle());
}
}
}
2021-07-02 21:24:39 +08:00
die('该客户端暂不支持进行订阅');
}
}
private function setSubscribeInfoToServers(&$servers, $user)
{
2023-01-19 15:43:46 +08:00
if (!isset($servers[0])) return;
if (!(int)config('v2board.show_info_to_server_enable', 0)) return;
$useTraffic = round($user['u'] / (1024*1024*1024), 2) + round($user['d'] / (1024*1024*1024), 2);
$totalTraffic = round($user['transfer_enable'] / (1024*1024*1024), 2);
$remainingTraffic = $totalTraffic - $useTraffic;
$expiredDate = $user['expired_at'] ? date('Y-m-d', $user['expired_at']) : '长期有效';
$userService = new UserService();
$resetDay = $userService->getResetDay($user);
array_unshift($servers, array_merge($servers[0], [
'name' => "套餐到期:{$expiredDate}",
]));
if ($resetDay) {
array_unshift($servers, array_merge($servers[0], [
'name' => "距离下次重置剩余:{$resetDay}",
]));
}
array_unshift($servers, array_merge($servers[0], [
'name' => "剩余流量:{$remainingTraffic} GB",
]));
}
2019-10-29 15:33:36 +08:00
}