2023-11-17 01:44:01 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2024-04-09 12:51:03 -04:00
|
|
|
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
|
|
|
|
*/
|
2024-04-12 05:34:06 -04:00
|
|
|
public function handle(Request $request, Closure $next, $node_type = null)
|
2023-11-17 01:44:01 -05:00
|
|
|
{
|
2023-11-18 20:54:40 -05:00
|
|
|
// alias
|
|
|
|
$aliasTypes = [
|
|
|
|
'v2ray' => 'vmess',
|
|
|
|
'hysteria2' => 'hysteria'
|
|
|
|
];
|
2023-11-17 01:44:01 -05:00
|
|
|
$request->validate([
|
2024-04-09 12:51:03 -04:00
|
|
|
'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' => [
|
2024-04-12 05:34:06 -04:00
|
|
|
'nullable',
|
2023-11-18 20:54:40 -05:00
|
|
|
'regex:/^(?i)(hysteria|hysteria2|vless|trojan|vmess|v2ray|tuic|shadowsocks|shadowsocks-plugin)$/',
|
2024-04-09 12:51:03 -04:00
|
|
|
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
|
|
|
]
|
2024-04-09 12:51:03 -04:00
|
|
|
], [
|
|
|
|
'token.in' => 'Token is error!',
|
|
|
|
'node_type.regex' => 'node_type is error!'
|
2023-11-17 01:44:01 -05:00
|
|
|
]);
|
2024-04-12 05:34:06 -04:00
|
|
|
$nodeInfo = ServerService::getServer($request->input('node_id'), $request->input('node_type') ?? $node_type);
|
2024-04-09 12:51:03 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|