Skip to content

Commit 95e8e7b

Browse files
author
xboard
committed
feat: add v2node support
1 parent 8d0e33b commit 95e8e7b

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

app/Http/Controllers/V1/Server/UniProxyController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public function config(Request $request)
9595
$host = $node->host;
9696

9797
$baseConfig = [
98+
'protocol' => $nodeType,
99+
'listen_ip' => '0.0.0.0',
98100
'server_port' => (int) $serverPort,
99101
'network' => data_get($protocolSettings, 'network'),
100102
'networkSettings' => data_get($protocolSettings, 'network_settings') ?: null,
@@ -132,6 +134,7 @@ public function config(Request $request)
132134
}
133135
],
134136
'hysteria' => [
137+
...$baseConfig,
135138
'server_port' => (int) $serverPort,
136139
'version' => (int) $protocolSettings['version'],
137140
'host' => $host,
@@ -148,6 +151,7 @@ public function config(Request $request)
148151
}
149152
],
150153
'tuic' => [
154+
...$baseConfig,
151155
'version' => (int) $protocolSettings['version'],
152156
'server_port' => (int) $serverPort,
153157
'server_name' => $protocolSettings['tls']['server_name'],
@@ -157,24 +161,29 @@ public function config(Request $request)
157161
'heartbeat' => "3s",
158162
],
159163
'anytls' => [
164+
...$baseConfig,
160165
'server_port' => (int) $serverPort,
161166
'server_name' => $protocolSettings['tls']['server_name'],
162167
'padding_scheme' => $protocolSettings['padding_scheme'],
163168
],
164169
'socks' => [
170+
...$baseConfig,
165171
'server_port' => (int) $serverPort,
166172
],
167173
'naive' => [
174+
...$baseConfig,
168175
'server_port' => (int) $serverPort,
169176
'tls' => (int) $protocolSettings['tls'],
170177
'tls_settings' => $protocolSettings['tls_settings']
171178
],
172179
'http' => [
180+
...$baseConfig,
173181
'server_port' => (int) $serverPort,
174182
'tls' => (int) $protocolSettings['tls'],
175183
'tls_settings' => $protocolSettings['tls_settings']
176184
],
177185
'mieru' => [
186+
...$baseConfig,
178187
'server_port' => (string) $serverPort,
179188
'protocol' => (int) $protocolSettings['protocol'],
180189
],

app/Http/Middleware/Server.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ class Server
1414
public function handle(Request $request, Closure $next, ?string $nodeType = null)
1515
{
1616
$this->validateRequest($request);
17-
17+
$nodeType = $request->input('node_type', $nodeType);
18+
$normalizedNodeType = ServerModel::normalizeType($nodeType);
1819
$serverInfo = ServerService::getServer(
1920
$request->input('node_id'),
20-
$request->input('node_type') ?? $nodeType
21+
$normalizedNodeType
2122
);
2223
if (!$serverInfo) {
2324
throw new ApiException('Server does not exist');
@@ -43,6 +44,9 @@ function ($attribute, $value, $fail) {
4344
'node_type' => [
4445
'nullable',
4546
function ($attribute, $value, $fail) use ($request) {
47+
if ($value === "v2node") {
48+
$value = null;
49+
}
4650
if (!ServerModel::isValidType($value)) {
4751
$fail("Invalid node type specified");
4852
return;

app/Http/Routes/V2/ServerRoute.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace App\Http\Routes\V2;
3+
4+
use App\Http\Controllers\V1\Server\ShadowsocksTidalabController;
5+
use App\Http\Controllers\V1\Server\TrojanTidalabController;
6+
use App\Http\Controllers\V1\Server\UniProxyController;
7+
use Illuminate\Contracts\Routing\Registrar;
8+
9+
class ServerRoute
10+
{
11+
public function map(Registrar $router)
12+
{
13+
14+
$router->group([
15+
'prefix' => 'server',
16+
'middleware' => 'server'
17+
], function ($route) {
18+
$route->get('config', [UniProxyController::class, 'config']);
19+
$route->get('user', [UniProxyController::class, 'user']);
20+
$route->post('push', [UniProxyController::class, 'push']);
21+
$route->post('alive', [UniProxyController::class, 'alive']);
22+
$route->get('alivelist', [UniProxyController::class, 'alivelist']);
23+
$route->post('status', [UniProxyController::class, 'status']);
24+
});
25+
}
26+
}

app/Models/Server.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,14 @@ public function generateServerPassword(User $user): string
317317
return "{$serverKey}:{$userKey}";
318318
}
319319

320-
public static function normalizeType(string $type): string
320+
public static function normalizeType(?string $type): string | null
321321
{
322-
return strtolower(self::TYPE_ALIASES[$type] ?? $type);
322+
return $type ? strtolower(self::TYPE_ALIASES[$type] ?? $type) : null;
323323
}
324-
325-
public static function isValidType(string $type): bool
324+
325+
public static function isValidType(?string $type): bool
326326
{
327-
return in_array(self::normalizeType($type), self::VALID_TYPES, true);
327+
return $type ? in_array(self::normalizeType($type), self::VALID_TYPES, true) : true;
328328
}
329329

330330
public function getAvailableStatusAttribute(): int

app/Services/ServerService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ public static function getRoutes(array $routeIds)
9898
* @param string $serverType
9999
* @return Server|null
100100
*/
101-
public static function getServer($serverId, $serverType)
101+
public static function getServer($serverId, ?string $serverType)
102102
{
103103
return Server::query()
104-
->where('type', Server::normalizeType($serverType))
104+
->when($serverType, function ($query) use ($serverType) {
105+
$query->where('type', Server::normalizeType($serverType));
106+
})
105107
->where(function ($query) use ($serverId) {
106108
$query->where('code', $serverId)
107109
->orWhere('id', $serverId);

0 commit comments

Comments
 (0)