Xboard/app/Http/Middleware/Server.php

49 lines
1.5 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Http\Middleware;
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',function ($attribute, $value, $fail) {
if ($value != admin_setting('server_token')) {
$fail("The $attribute is error.");
}
}],
2023-11-18 20:54:40 -05:00
'node_id' => 'required',
2023-11-17 01:44:01 -05:00
'node_type' => [
'nullable',
2023-11-17 01:44:01 -05:00
'string',
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) {
2023-11-17 01:44:01 -05:00
// 将值转换为小写
request()->merge([$attribute => strtolower($value)]);
2023-11-18 20:54:40 -05:00
// 类别别名
if (in_array($value, array_keys($aliasTypes))){
request()->merge([$attribute => $aliasTypes[$value]]);
}
2023-11-17 01:44:01 -05:00
},
2023-11-18 20:54:40 -05:00
]
2023-11-17 01:44:01 -05:00
]);
return $next($request);
}
}