2020-02-28 01:03:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2020-04-25 19:44:47 +08:00
|
|
|
use App\Models\ServerLog;
|
2022-11-27 15:11:10 +08:00
|
|
|
use App\Models\ServerRoute;
|
2020-10-04 14:21:09 +08:00
|
|
|
use App\Models\ServerShadowsocks;
|
2020-02-28 01:03:05 +08:00
|
|
|
use App\Models\User;
|
2023-02-15 14:53:13 +08:00
|
|
|
use App\Models\ServerVmess;
|
2020-06-12 00:18:35 +08:00
|
|
|
use App\Models\ServerTrojan;
|
|
|
|
use App\Utils\CacheKey;
|
2022-02-17 02:44:49 +08:00
|
|
|
use App\Utils\Helper;
|
2020-06-12 00:18:35 +08:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2020-02-28 01:03:05 +08:00
|
|
|
|
|
|
|
class ServerService
|
|
|
|
{
|
2020-03-10 13:11:31 +08:00
|
|
|
|
2023-02-15 14:53:13 +08:00
|
|
|
public function getAvailableVmess(User $user):array
|
2020-06-12 00:18:35 +08:00
|
|
|
{
|
2020-11-16 11:58:34 +08:00
|
|
|
$servers = [];
|
2023-02-15 14:53:13 +08:00
|
|
|
$model = ServerVmess::orderBy('sort', 'ASC');
|
|
|
|
$vmess = $model->get();
|
|
|
|
foreach ($vmess as $key => $v) {
|
2023-01-28 22:42:05 +08:00
|
|
|
if (!$v['show']) continue;
|
2023-02-15 14:53:13 +08:00
|
|
|
$vmess[$key]['type'] = 'vmess';
|
|
|
|
if (!in_array($user->group_id, $vmess[$key]['group_id'])) continue;
|
|
|
|
if (strpos($vmess[$key]['port'], '-') !== false) {
|
|
|
|
$vmess[$key]['port'] = Helper::randomPort($vmess[$key]['port']);
|
2022-02-17 03:17:05 +08:00
|
|
|
}
|
2023-02-15 14:53:13 +08:00
|
|
|
if ($vmess[$key]['parent_id']) {
|
|
|
|
$vmess[$key]['last_check_at'] = Cache::get(CacheKey::get('SERVER_VMESS_LAST_CHECK_AT', $vmess[$key]['parent_id']));
|
2022-02-19 13:45:57 +08:00
|
|
|
} else {
|
2023-02-15 14:53:13 +08:00
|
|
|
$vmess[$key]['last_check_at'] = Cache::get(CacheKey::get('SERVER_VMESS_LAST_CHECK_AT', $vmess[$key]['id']));
|
2022-02-17 02:44:49 +08:00
|
|
|
}
|
2023-02-15 14:53:13 +08:00
|
|
|
$servers[] = $vmess[$key]->toArray();
|
2020-06-12 00:18:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-16 11:58:34 +08:00
|
|
|
return $servers;
|
2020-06-12 00:18:35 +08:00
|
|
|
}
|
|
|
|
|
2023-01-28 22:32:46 +08:00
|
|
|
public function getAvailableTrojan(User $user):array
|
2020-06-12 00:18:35 +08:00
|
|
|
{
|
2020-11-16 11:58:34 +08:00
|
|
|
$servers = [];
|
2023-01-28 22:42:05 +08:00
|
|
|
$model = ServerTrojan::orderBy('sort', 'ASC');
|
2020-11-15 15:25:32 +08:00
|
|
|
$trojan = $model->get();
|
2023-01-28 22:42:05 +08:00
|
|
|
foreach ($trojan as $key => $v) {
|
|
|
|
if (!$v['show']) continue;
|
|
|
|
$trojan[$key]['type'] = 'trojan';
|
|
|
|
if (!in_array($user->group_id, $trojan[$key]['group_id'])) continue;
|
|
|
|
if (strpos($trojan[$key]['port'], '-') !== false) {
|
|
|
|
$trojan[$key]['port'] = Helper::randomPort($trojan[$key]['port']);
|
2022-02-17 03:17:05 +08:00
|
|
|
}
|
2023-01-28 22:42:05 +08:00
|
|
|
if ($trojan[$key]['parent_id']) {
|
|
|
|
$trojan[$key]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojan[$key]['parent_id']));
|
2022-02-19 13:45:57 +08:00
|
|
|
} else {
|
2023-01-28 22:42:05 +08:00
|
|
|
$trojan[$key]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojan[$key]['id']));
|
2022-02-17 02:44:49 +08:00
|
|
|
}
|
2023-01-28 22:42:05 +08:00
|
|
|
$servers[] = $trojan[$key]->toArray();
|
2020-06-12 00:18:35 +08:00
|
|
|
}
|
2020-11-16 11:58:34 +08:00
|
|
|
return $servers;
|
2020-06-12 00:18:35 +08:00
|
|
|
}
|
|
|
|
|
2023-01-28 22:32:46 +08:00
|
|
|
public function getAvailableShadowsocks(User $user)
|
2020-10-04 14:21:09 +08:00
|
|
|
{
|
2020-11-16 11:58:34 +08:00
|
|
|
$servers = [];
|
2023-01-28 22:42:05 +08:00
|
|
|
$model = ServerShadowsocks::orderBy('sort', 'ASC');
|
2023-01-25 23:04:55 +08:00
|
|
|
$shadowsocks = $model->get()->keyBy('id');
|
|
|
|
foreach ($shadowsocks as $key => $v) {
|
2023-01-28 22:42:05 +08:00
|
|
|
if (!$v['show']) continue;
|
2023-01-25 23:04:55 +08:00
|
|
|
$shadowsocks[$key]['type'] = 'shadowsocks';
|
2023-01-28 22:32:46 +08:00
|
|
|
$shadowsocks[$key]['last_check_at'] = Cache::get(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $v['id']));
|
2023-01-25 23:04:55 +08:00
|
|
|
if (!in_array($user->group_id, $v['group_id'])) continue;
|
|
|
|
if (strpos($v['port'], '-') !== false) {
|
|
|
|
$shadowsocks[$key]['port'] = Helper::randomPort($v['port']);
|
2022-02-17 03:17:05 +08:00
|
|
|
}
|
2023-01-28 22:32:46 +08:00
|
|
|
if (isset($shadowsocks[$v['parent_id']])) {
|
2023-01-25 23:04:55 +08:00
|
|
|
$shadowsocks[$key]['last_check_at'] = Cache::get(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $v['parent_id']));
|
|
|
|
$shadowsocks[$key]['created_at'] = $shadowsocks[$v['parent_id']]['created_at'];
|
2022-02-17 02:44:49 +08:00
|
|
|
}
|
2023-01-25 23:04:55 +08:00
|
|
|
$servers[] = $shadowsocks[$key]->toArray();
|
2020-10-04 14:21:09 +08:00
|
|
|
}
|
2020-11-16 11:58:34 +08:00
|
|
|
return $servers;
|
2020-10-04 14:21:09 +08:00
|
|
|
}
|
|
|
|
|
2023-01-28 22:32:46 +08:00
|
|
|
public function getAvailableServers(User $user)
|
2020-06-12 00:18:35 +08:00
|
|
|
{
|
2020-11-14 17:26:17 +08:00
|
|
|
$servers = array_merge(
|
2023-01-28 22:32:46 +08:00
|
|
|
$this->getAvailableShadowsocks($user),
|
2023-02-15 14:53:13 +08:00
|
|
|
$this->getAvailableVmess($user),
|
2023-01-28 22:32:46 +08:00
|
|
|
$this->getAvailableTrojan($user)
|
2020-11-14 17:26:17 +08:00
|
|
|
);
|
|
|
|
$tmp = array_column($servers, 'sort');
|
|
|
|
array_multisort($tmp, SORT_ASC, $servers);
|
2023-01-25 23:04:55 +08:00
|
|
|
return array_map(function ($server) {
|
2022-11-26 18:58:24 +08:00
|
|
|
$server['port'] = (int)$server['port'];
|
2023-01-04 22:52:34 +08:00
|
|
|
$server['is_online'] = (time() - 300 > $server['last_check_at']) ? 0 : 1;
|
2023-01-04 23:02:28 +08:00
|
|
|
$server['cache_key'] = "{$server['type']}-{$server['id']}-{$server['updated_at']}-{$server['is_online']}";
|
2022-11-26 18:58:24 +08:00
|
|
|
return $server;
|
|
|
|
}, $servers);
|
2020-06-12 00:18:35 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:03:05 +08:00
|
|
|
public function getAvailableUsers($groupId)
|
|
|
|
{
|
|
|
|
return User::whereIn('group_id', $groupId)
|
|
|
|
->whereRaw('u + d < transfer_enable')
|
|
|
|
->where(function ($query) {
|
|
|
|
$query->where('expired_at', '>=', time())
|
2020-03-01 23:29:49 +08:00
|
|
|
->orWhere('expired_at', NULL);
|
2020-02-28 01:03:05 +08:00
|
|
|
})
|
2020-03-02 20:47:52 +08:00
|
|
|
->where('banned', 0)
|
2020-02-28 01:03:05 +08:00
|
|
|
->select([
|
|
|
|
'id',
|
2022-12-15 17:08:05 +08:00
|
|
|
'uuid',
|
|
|
|
'speed_limit'
|
2020-02-28 01:03:05 +08:00
|
|
|
])
|
|
|
|
->get();
|
|
|
|
}
|
2020-03-10 13:11:31 +08:00
|
|
|
|
2020-06-11 20:47:02 +08:00
|
|
|
public function log(int $userId, int $serverId, int $u, int $d, float $rate, string $method)
|
2020-04-25 19:44:47 +08:00
|
|
|
{
|
2021-09-01 02:32:15 +08:00
|
|
|
if (($u + $d) < 10240) return true;
|
2021-09-02 20:09:04 +08:00
|
|
|
$timestamp = strtotime(date('Y-m-d'));
|
2020-05-11 17:19:58 +08:00
|
|
|
$serverLog = ServerLog::where('log_at', '>=', $timestamp)
|
|
|
|
->where('log_at', '<', $timestamp + 3600)
|
|
|
|
->where('server_id', $serverId)
|
|
|
|
->where('user_id', $userId)
|
|
|
|
->where('rate', $rate)
|
2020-06-11 20:47:02 +08:00
|
|
|
->where('method', $method)
|
2020-05-11 17:19:58 +08:00
|
|
|
->first();
|
|
|
|
if ($serverLog) {
|
2021-09-02 23:31:12 +08:00
|
|
|
try {
|
|
|
|
$serverLog->increment('u', $u);
|
|
|
|
$serverLog->increment('d', $d);
|
|
|
|
return true;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-11 17:19:58 +08:00
|
|
|
} else {
|
|
|
|
$serverLog = new ServerLog();
|
|
|
|
$serverLog->user_id = $userId;
|
|
|
|
$serverLog->server_id = $serverId;
|
|
|
|
$serverLog->u = $u;
|
|
|
|
$serverLog->d = $d;
|
|
|
|
$serverLog->rate = $rate;
|
|
|
|
$serverLog->log_at = $timestamp;
|
2020-06-11 20:47:02 +08:00
|
|
|
$serverLog->method = $method;
|
2021-08-31 23:55:07 +08:00
|
|
|
return $serverLog->save();
|
2020-05-11 17:19:58 +08:00
|
|
|
}
|
2020-04-25 19:44:47 +08:00
|
|
|
}
|
2020-11-14 17:26:17 +08:00
|
|
|
|
2023-01-28 22:32:46 +08:00
|
|
|
public function getAllShadowsocks()
|
2020-11-14 17:26:17 +08:00
|
|
|
{
|
2023-01-30 20:12:01 +08:00
|
|
|
$servers = ServerShadowsocks::orderBy('sort', 'ASC')
|
|
|
|
->get()
|
|
|
|
->toArray();
|
|
|
|
foreach ($servers as $k => $v) {
|
|
|
|
$servers[$k]['type'] = 'shadowsocks';
|
2020-11-14 17:26:17 +08:00
|
|
|
}
|
2023-01-30 20:12:01 +08:00
|
|
|
return $servers;
|
2020-11-14 17:26:17 +08:00
|
|
|
}
|
|
|
|
|
2023-02-15 14:53:13 +08:00
|
|
|
public function getAllVMess()
|
2020-11-14 17:26:17 +08:00
|
|
|
{
|
2023-02-15 14:53:13 +08:00
|
|
|
$servers = ServerVmess::orderBy('sort', 'ASC')
|
2023-01-30 20:12:01 +08:00
|
|
|
->get()
|
|
|
|
->toArray();
|
|
|
|
foreach ($servers as $k => $v) {
|
2023-02-15 14:53:13 +08:00
|
|
|
$servers[$k]['type'] = 'vmess';
|
2020-11-14 17:26:17 +08:00
|
|
|
}
|
2023-01-30 20:12:01 +08:00
|
|
|
return $servers;
|
2020-11-14 17:26:17 +08:00
|
|
|
}
|
|
|
|
|
2023-01-28 22:32:46 +08:00
|
|
|
public function getAllTrojan()
|
2020-11-14 17:26:17 +08:00
|
|
|
{
|
2023-01-30 20:12:01 +08:00
|
|
|
$servers = ServerTrojan::orderBy('sort', 'ASC')
|
|
|
|
->get()
|
|
|
|
->toArray();
|
|
|
|
foreach ($servers as $k => $v) {
|
|
|
|
$servers[$k]['type'] = 'trojan';
|
2021-03-24 15:04:09 +08:00
|
|
|
}
|
2023-01-30 20:12:01 +08:00
|
|
|
return $servers;
|
2021-03-24 15:04:09 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 15:11:10 +08:00
|
|
|
private function mergeData(&$servers)
|
2021-03-24 15:04:09 +08:00
|
|
|
{
|
|
|
|
foreach ($servers as $k => $v) {
|
2023-01-30 20:12:01 +08:00
|
|
|
$serverType = strtoupper($v['type']);
|
|
|
|
$servers[$k]['online'] = Cache::get(CacheKey::get("SERVER_{$serverType}_ONLINE_USER", $v['parent_id'] ?? $v['id']));
|
|
|
|
$servers[$k]['last_check_at'] = Cache::get(CacheKey::get("SERVER_{$serverType}_LAST_CHECK_AT", $v['parent_id'] ?? $v['id']));
|
|
|
|
$servers[$k]['last_push_at'] = Cache::get(CacheKey::get("SERVER_{$serverType}_LAST_PUSH_AT", $v['parent_id'] ?? $v['id']));
|
2021-03-24 15:04:09 +08:00
|
|
|
if ((time() - 300) >= $servers[$k]['last_check_at']) {
|
|
|
|
$servers[$k]['available_status'] = 0;
|
|
|
|
} else if ((time() - 300) >= $servers[$k]['last_push_at']) {
|
|
|
|
$servers[$k]['available_status'] = 1;
|
|
|
|
} else {
|
|
|
|
$servers[$k]['available_status'] = 2;
|
2020-11-14 17:26:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 19:07:10 +08:00
|
|
|
|
|
|
|
public function getAllServers()
|
|
|
|
{
|
|
|
|
$servers = array_merge(
|
2023-01-28 22:32:46 +08:00
|
|
|
$this->getAllShadowsocks(),
|
2023-02-15 14:53:13 +08:00
|
|
|
$this->getAllVMess(),
|
2023-01-28 22:32:46 +08:00
|
|
|
$this->getAllTrojan()
|
2021-06-08 19:07:10 +08:00
|
|
|
);
|
|
|
|
$this->mergeData($servers);
|
|
|
|
$tmp = array_column($servers, 'sort');
|
|
|
|
array_multisort($tmp, SORT_ASC, $servers);
|
|
|
|
return $servers;
|
|
|
|
}
|
2022-11-27 15:11:10 +08:00
|
|
|
|
2022-11-29 14:33:08 +08:00
|
|
|
public function getRoutes(array $routeIds)
|
2022-11-27 15:11:10 +08:00
|
|
|
{
|
2022-12-17 21:37:08 +08:00
|
|
|
$routes = ServerRoute::select(['id', 'match', 'action', 'action_value'])->whereIn('id', $routeIds)->get();
|
|
|
|
// TODO: remove on 1.8.0
|
|
|
|
foreach ($routes as $k => $route) {
|
|
|
|
$array = json_decode($route->match, true);
|
|
|
|
if (is_array($array)) $routes[$k]['match'] = $array;
|
|
|
|
}
|
|
|
|
// TODO: remove on 1.8.0
|
|
|
|
return $routes;
|
2022-11-27 15:11:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getServer($serverId, $serverType)
|
|
|
|
{
|
|
|
|
switch ($serverType) {
|
2023-02-15 14:53:13 +08:00
|
|
|
case 'vmess':
|
|
|
|
return ServerVmess::find($serverId);
|
2022-11-27 15:11:10 +08:00
|
|
|
case 'shadowsocks':
|
|
|
|
return ServerShadowsocks::find($serverId);
|
|
|
|
case 'trojan':
|
|
|
|
return ServerTrojan::find($serverId);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 01:03:05 +08:00
|
|
|
}
|