Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions app/Http/Controllers/V2/Server/ServerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace App\Http\Controllers\V2\Server;

use App\Http\Controllers\Controller;
use App\Services\ServerService;
use Illuminate\Http\Request;
use App\Utils\Helper;


class ServerController extends Controller
{
private $nodeInfo;
private $nodeId;

public function __construct(Request $request)
{
$token = $request->input('token');
if (empty($token)) {
abort(500, 'token is null');
}
if ($token !== admin_setting('server_token')) {
abort(500, 'token is error');
}
$this->nodeId = $request->input('node_id');
$this->serverService = new ServerService();
$this->nodeInfo = $this->serverService->getServer($this->nodeId, "v2node");
if (!$this->nodeInfo) abort(500, 'server is not exist');
}

// 后端获取配置
public function config(Request $request)
{
$protocolSettings = $node->protocol_settings;
$response = [
'listen_ip' => $this->nodeInfo->host,
'server_port' => $this->nodeInfo->server_port,
'network' => data_get($protocolSettings, 'network'),
'network_settings' => data_get($protocolSettings, 'network_settings') ?: null,
'protocol' => $this->nodeInfo->type,
'tls' => (int) $protocolSettings['tls'],
'tls_settings' => match ((int) $protocolSettings['tls']) {
2 => $protocolSettings['reality_settings'],
default => $protocolSettings['tls_settings']
},
'encryption' => data_get($protocolSettings, 'encryption'),
'encryption_settings' => data_get($protocolSettings, 'encryption_settings') ?: null,
'flow' => $protocolSettings['flow'],
'cipher' => $protocolSettings['cipher'],
'congestion_control' => $protocolSettings['congestion_control'],
'zero_rtt_handshake' => $protocolSettings['zero_rtt_handshake'] ? true : false,
'up_mbps' => $protocolSettings['bandwidth']['up'],
'down_mbps' => $protocolSettings['bandwidth']['down'],
'obfs' => $protocolSettings['obfs']['open'] ? $protocolSettings['obfs']['type'] : null,
'obfs_password' => $protocolSettings['obfs']['password'] ?? null,
'padding_scheme' => $protocolSettings['padding_scheme']
];
if ($protocolSettings['cipher'] === '2022-blake3-aes-128-gcm') {
$response['server_key'] = Helper::getServerKey($this->nodeInfo->created_at, 16);
}
if ($protocolSettings['cipher'] === '2022-blake3-aes-256-gcm') {
$response['server_key'] = Helper::getServerKey($this->nodeInfo->created_at, 32);
}

if ($protocolSettings['bandwidth']['up'] == 0 && $protocolSettings['bandwidth']['down'] == 0) {
$response['ignore_client_bandwidth'] = true;
} else {
$response['ignore_client_bandwidth'] = false;
}

$response['base_config'] = [
'push_interval' => (int) admin_setting('server_push_interval', 60),
'pull_interval' => (int) admin_setting('server_pull_interval', 60),
'node_report_min_traffic' => (int) admin_setting('server_node_report_min_traffic', 0),
'device_online_min_traffic' => (int) admin_setting('server_device_online_min_traffic', 0)
];
if ($this->nodeInfo['route_id']) {
$response['routes'] = ServerService::getRoutes($node['route_ids']);
}
$rsp = json_encode($response);
$eTag = sha1($rsp);
if (strpos($request->header('If-None-Match'), $eTag) !== false) {
abort(304);
}
return response($response)->header('ETag', "\"{$eTag}\"");
}
}
18 changes: 18 additions & 0 deletions app/Http/Routes/V2/ServerRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace App\Http\Routes\V2;

use App\Http\Controllers\V2\Server\ServerController;
use Illuminate\Contracts\Routing\Registrar;


class ServerRoute
{
public function map(Registrar $router)
{
$router->group([
'prefix' => 'server'
], function ($router) {
$router->get('/config', [ServerController::class, 'config']);
});
}
}
9 changes: 9 additions & 0 deletions app/Services/ServerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ public static function getRoutes(array $routeIds)
*/
public static function getServer($serverId, $serverType)
{
if($serverType == 'v2node'){
return Server::query()
->where(function ($query) use ($serverId) {
$query->where('code', $serverId)
->orWhere('id', $serverId);
})
->orderByRaw('CASE WHEN code = ? THEN 0 ELSE 1 END', [$serverId])
->first();
}
return Server::query()
->where('type', Server::normalizeType($serverType))
->where(function ($query) use ($serverId) {
Expand Down