Sort user-facing nodes by region across XB instances
Docker Build and Publish / build (push) Waiting to run
Docker Build and Publish / build (push) Waiting to run
This commit is contained in:
1 parent
3633ba32a6
commit
6e29d4fdf7
3 files changed
+157
-2
No files matched your search
@@ -14,6 +14,12 @@ use Illuminate\Support\Collection;
|
||||
|
||||
class ServerService
|
||||
{
|
||||
private const DISPLAY_REGIONS = [
|
||||
'hk' => '🇭🇰|香港|Hong[ -]?Kong(?=$|[^\p{L}])|HK(?=$|[^\p{L}])',
|
||||
'jp' => '🇯🇵|日本|Japan(?=$|[^\p{L}])|JP(?=$|[^\p{L}])',
|
||||
'sg' => '🇸🇬|新加坡|Singapore(?=$|[^\p{L}])|SG(?=$|[^\p{L}])',
|
||||
'us' => '🇺🇸|美国|美國|United States(?=$|[^\p{L}])|USA?(?=$|[^\p{L}])',
|
||||
];
|
||||
|
||||
/**
|
||||
* 获取所有服务器列表
|
||||
@@ -62,10 +68,11 @@ class ServerService
|
||||
->orWhereRaw('u + d < transfer_enable');
|
||||
})
|
||||
->orderBy('sort', 'ASC')
|
||||
->orderBy('id', 'ASC')
|
||||
->get()
|
||||
->append(['last_check_at', 'last_push_at', 'online', 'is_online', 'available_status', 'cache_key', 'server_key']);
|
||||
|
||||
$servers = collect($servers)->map(function ($server) use ($user) {
|
||||
return self::sortForDisplay($servers)->map(function ($server) use ($user) {
|
||||
// 判断动态端口
|
||||
if (str_contains($server->port, '-')) {
|
||||
$port = $server->port;
|
||||
@@ -78,8 +85,54 @@ class ServerService
|
||||
$server->rate = $server->getCurrentRate();
|
||||
return $server;
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
return $servers;
|
||||
private static function sortForDisplay(Collection $servers): Collection
|
||||
{
|
||||
$positions = $servers->pluck('id')->flip();
|
||||
|
||||
return $servers
|
||||
->groupBy(fn(Server $server) => $server->parent_id ?: $server->id)
|
||||
->sortBy(function (Collection $group, int $id) use ($positions) {
|
||||
$primary = $group->firstWhere('id', $id) ?? $group->first();
|
||||
|
||||
return [...self::displayOrder($primary), $positions[$primary->id]];
|
||||
})
|
||||
->flatMap(fn(Collection $group) => $group
|
||||
->sortBy(fn(Server $server) => (bool) $server->parent_id)
|
||||
->values())
|
||||
->values();
|
||||
}
|
||||
|
||||
private static function displayOrder(Server $server): array
|
||||
{
|
||||
$name = preg_replace('/^(?:\[[^\]]+\]\s*)+/', '', trim($server->name));
|
||||
$region = 0;
|
||||
|
||||
foreach (self::DISPLAY_REGIONS as $code => $prefix) {
|
||||
if (preg_match('/^(?:(?:' . $prefix . ')[\s_-]*)+/iu', $name, $match) === 1) {
|
||||
$number = self::displayNumber($server, $code, substr($name, strlen($match[0])));
|
||||
return [$region, $number];
|
||||
}
|
||||
$region++;
|
||||
}
|
||||
|
||||
return [$region, PHP_INT_MAX];
|
||||
}
|
||||
|
||||
private static function displayNumber(Server $server, string $region, string $label): int
|
||||
{
|
||||
if (preg_match('/^(\d+)(?=$|[^\p{L}\d])/u', $label, $match) === 1) {
|
||||
return (int) $match[1];
|
||||
}
|
||||
|
||||
foreach ([data_get($server->protocol_settings, 'tls.server_name'), $server->host] as $host) {
|
||||
if ($host && preg_match('/^(?:[a-z0-9]+-)*' . $region . '(\d+)(?=\.|$)/i', $host, $match) === 1) {
|
||||
return (int) $match[1];
|
||||
}
|
||||
}
|
||||
|
||||
return PHP_INT_MAX;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user