Xboard/app/Services/ServerService.php

112 lines
3.1 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Services;
2025-01-06 12:20:11 -05:00
use App\Models\Server;
2023-11-17 01:44:01 -05:00
use App\Models\ServerRoute;
use App\Models\User;
use App\Utils\Helper;
use Illuminate\Support\Collection;
2023-11-17 01:44:01 -05:00
class ServerService
{
2025-01-06 12:20:11 -05:00
/**
* 获取所有服务器列表
* @return Collection
*/
public static function getAllServers()
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
return Server::orderBy('sort', 'ASC')
->get()
->transform(function (Server $server) {
$server->loadServerStatus();
return $server;
});
}
/**
* 获取指定用户可用的服务器列表
* @param User $user
* @return array
*/
public static function getAvailableServers(User $user): array
{
return Server::whereJsonContains('group_ids', (string) $user->group_id)
->where('show', true)
->orderBy('sort', 'ASC')
->get()
->transform(function (Server $server) use ($user) {
$server->loadParentCreatedAt();
$server->handlePortAllocation();
$server->loadServerStatus();
2025-01-09 02:58:32 -05:00
if ($server->type === 'shadowsocks') {
$server->server_key = Helper::getServerKey($server->created_at, 16);
}
2025-01-06 12:20:11 -05:00
$server->generateShadowsocksPassword($user);
return $server;
})
->toArray();
2023-11-17 01:44:01 -05:00
}
2025-01-09 02:58:32 -05:00
/**
*
*/
2025-01-06 12:20:11 -05:00
/**
* 根据权限组获取可用的用户列表
* @param array $groupIds
* @return Collection
*/
public static function getAvailableUsers(array $groupIds)
2023-11-17 01:44:01 -05:00
{
return User::toBase()
2025-01-06 12:20:11 -05:00
->whereIn('group_id', $groupIds)
2023-11-17 01:44:01 -05:00
->whereRaw('u + d < transfer_enable')
->where(function ($query) {
$query->where('expired_at', '>=', time())
->orWhere('expired_at', NULL);
})
->where('banned', 0)
->select([
'id',
'uuid',
'speed_limit'
])
->get();
}
// 获取路由规则
public static function getRoutes(array $routeIds)
2023-11-17 01:44:01 -05: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);
2025-01-06 12:20:11 -05:00
if (is_array($array))
$routes[$k]['match'] = $array;
2023-11-17 01:44:01 -05:00
}
// TODO: remove on 1.8.0
return $routes;
}
2025-01-06 12:20:11 -05:00
/**
* 根据协议类型和标识获取服务器
* @param int $serverId
* @param string $serverType
* @return Server|null
*/
public static function getServer($serverId, $serverType)
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
return Server::query()
->where('type', Server::normalizeType($serverType))
->where(function ($query) use ($serverId) {
$query->where('code', $serverId)
->orWhere('id', $serverId);
})
->orderByRaw('CASE WHEN code = ? THEN 0 ELSE 1 END', [$serverId])
2025-01-06 12:20:11 -05:00
->first();
2023-11-17 01:44:01 -05:00
}
}