Xboard/app/Http/Middleware/Server.php

46 lines
1.6 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Http\Middleware;
use App\Exceptions\ApiException;
use App\Services\ServerService;
2023-11-17 01:44:01 -05:00
use Closure;
use Illuminate\Http\Request;
class Server
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
2023-11-18 20:54:40 -05:00
// alias
$aliasTypes = [
'v2ray' => 'vmess',
'hysteria2' => 'hysteria'
];
2023-11-17 01:44:01 -05:00
$request->validate([
'token' => ['required', 'string', 'in:' . admin_setting('server_token')],
2023-11-18 20:54:40 -05:00
'node_id' => 'required',
2023-11-17 01:44:01 -05:00
'node_type' => [
2023-11-18 20:54:40 -05:00
'regex:/^(?i)(hysteria|hysteria2|vless|trojan|vmess|v2ray|tuic|shadowsocks|shadowsocks-plugin)$/',
function ($attribute, $value, $fail) use ($aliasTypes, $request) {
$request->merge([$attribute => strtolower(isset ($aliasTypes[$value]) ? $aliasTypes[$value] : $value)]);
2023-11-17 01:44:01 -05:00
},
2023-11-18 20:54:40 -05:00
]
], [
'token.in' => 'Token is error!',
'node_type.regex' => 'node_type is error!'
2023-11-17 01:44:01 -05:00
]);
$nodeInfo = ServerService::getServer($request->input('node_id'), $request->input('node_type'));
if (!$nodeInfo)
throw new ApiException('server is not exist!');
$request->merge(['node_info' => $nodeInfo]);
2023-11-17 01:44:01 -05:00
return $next($request);
}
}