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

196 lines
7.3 KiB
PHP
Raw Normal View History

2022-05-02 13:37:11 +08:00
<?php
2023-06-12 02:32:49 +08:00
namespace App\Http\Controllers\V1\Server;
2022-05-02 13:37:11 +08:00
2023-06-12 02:32:49 +08:00
use App\Http\Controllers\Controller;
2022-05-02 13:37:11 +08:00
use App\Services\ServerService;
use App\Services\UserService;
use App\Utils\CacheKey;
2022-11-17 02:19:11 +08:00
use App\Utils\Helper;
2022-05-02 13:37:11 +08:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
2022-11-17 02:19:11 +08:00
class UniProxyController extends Controller
2022-05-02 13:37:11 +08:00
{
private $nodeType;
private $nodeInfo;
private $nodeId;
2022-11-27 15:11:10 +08:00
private $serverService;
2022-05-02 13:37:11 +08:00
public function __construct(Request $request)
{
$token = $request->input('token');
if (empty($token)) {
abort(500, 'token is null');
}
if ($token !== config('v2board.server_token')) {
abort(500, 'token is error');
}
$this->nodeType = $request->input('node_type');
2023-02-15 14:53:13 +08:00
if ($this->nodeType === 'v2ray') $this->nodeType = 'vmess';
if ($this->nodeType === 'hysteria2') $this->nodeType = 'hysteria';
2022-05-02 13:37:11 +08:00
$this->nodeId = $request->input('node_id');
2022-11-27 15:11:10 +08:00
$this->serverService = new ServerService();
$this->nodeInfo = $this->serverService->getServer($this->nodeId, $this->nodeType);
if (!$this->nodeInfo) abort(500, 'server is not exist');
2022-05-02 13:37:11 +08:00
}
// 后端获取用户
public function user(Request $request)
{
ini_set('memory_limit', -1);
2022-05-12 02:39:59 +08:00
Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_CHECK_AT', $this->nodeInfo->id), time(), 3600);
2022-11-27 15:11:10 +08:00
$users = $this->serverService->getAvailableUsers($this->nodeInfo->group_id);
2022-05-02 13:37:11 +08:00
$users = $users->toArray();
2023-10-14 18:32:15 +09:00
$users = array_map(function ($user) {
$count =0;
$ips_array = Cache::get('ALIVE_IP_USER_'. $user['id']);
if ($ips_array) {
foreach ($ips_array as $nodetypeid => $ip_array) {
2023-10-14 18:32:15 +09:00
foreach ($ip_array['aliveips'] as $ip) {
$count++;
}
}
}
$user['alive_ip'] = $count;
return $user;
}, $users);
2022-05-02 13:37:11 +08:00
$response['users'] = $users;
$eTag = sha1(json_encode($response));
if (strpos($request->header('If-None-Match'), $eTag) !== false ) {
abort(304);
}
return response($response)->header('ETag', "\"{$eTag}\"");
}
// 后端提交数据
2022-11-17 02:19:11 +08:00
public function push(Request $request)
2022-05-02 13:37:11 +08:00
{
$data = file_get_contents('php://input');
$data = json_decode($data, true);
2022-05-12 02:39:59 +08:00
Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_ONLINE_USER', $this->nodeInfo->id), count($data), 3600);
Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_PUSH_AT', $this->nodeInfo->id), time(), 3600);
2022-05-02 13:37:11 +08:00
$userService = new UserService();
2023-05-17 11:28:59 +08:00
$userService->trafficFetch($this->nodeInfo->toArray(), $this->nodeType, $data);
2023-04-15 19:34:54 +08:00
2022-05-02 13:37:11 +08:00
return response([
'data' => true
]);
}
2023-10-14 18:32:15 +09:00
// 后端提交在线数据
public function alive(Request $request)
{
$data = file_get_contents('php://input');
$data = json_decode($data, true);
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
// JSON decoding error
return response([
'error' => 'Invalid online data'
], 400);
}
foreach ($data as $k => $v) {
$updateAt = time();
$oldips_array = Cache::get('ALIVE_IP_USER_'. $k) ?? [];
// 更新节点数据
$oldips_array[$this->nodeType . $this->nodeId] = ['aliveips' => $v, 'lastupdateAt' => $updateAt];
2023-10-14 18:32:15 +09:00
//删除过期节点在线数据
$expired_array = [];
foreach($oldips_array as $nodetypeid => $oldips) {
2023-10-14 18:32:15 +09:00
if ($updateAt - $oldips['lastupdateAt'] > 300){
$expired_array[$nodeid] = '';
}
}
// 清理过期数据并存回缓存
$new_array = array_diff_key($oldips_array, $expired_array);
Cache::put('ALIVE_IP_USER_'. $k, $new_array, 120);
}
return response([
'data' => true
]);
}
2022-05-02 13:37:11 +08:00
// 后端获取配置
public function config(Request $request)
{
switch ($this->nodeType) {
case 'shadowsocks':
2022-11-17 02:19:11 +08:00
$response = [
2022-05-02 13:37:11 +08:00
'server_port' => $this->nodeInfo->server_port,
2022-05-12 02:39:59 +08:00
'cipher' => $this->nodeInfo->cipher,
'obfs' => $this->nodeInfo->obfs,
'obfs_settings' => $this->nodeInfo->obfs_settings
2022-11-17 02:19:11 +08:00
];
2022-11-18 03:25:02 +08:00
if ($this->nodeInfo->cipher === '2022-blake3-aes-128-gcm') {
2023-03-08 02:28:00 +08:00
$response['server_key'] = Helper::getServerKey($this->nodeInfo->created_at, 16);
2022-11-18 03:25:02 +08:00
}
if ($this->nodeInfo->cipher === '2022-blake3-aes-256-gcm') {
2023-03-08 02:28:00 +08:00
$response['server_key'] = Helper::getServerKey($this->nodeInfo->created_at, 32);
2022-11-18 03:25:02 +08:00
}
2022-05-02 13:37:11 +08:00
break;
2023-02-15 14:53:13 +08:00
case 'vmess':
2022-11-17 02:19:11 +08:00
$response = [
2022-05-02 13:37:11 +08:00
'server_port' => $this->nodeInfo->server_port,
'network' => $this->nodeInfo->network,
'networkSettings' => $this->nodeInfo->networkSettings,
'tls' => $this->nodeInfo->tls
2022-11-17 02:19:11 +08:00
];
2022-05-02 13:37:11 +08:00
break;
2023-09-25 22:01:15 +09:00
case 'vless':
$response = [
'server_port' => $this->nodeInfo->server_port,
'network' => $this->nodeInfo->network,
'networkSettings' => $this->nodeInfo->network_settings,
'tls' => $this->nodeInfo->tls,
'flow' => $this->nodeInfo->flow,
'tls_settings' => $this->nodeInfo->tls_settings
];
break;
2022-05-02 13:37:11 +08:00
case 'trojan':
2022-11-17 02:19:11 +08:00
$response = [
2022-05-02 13:37:11 +08:00
'host' => $this->nodeInfo->host,
2022-11-17 02:19:11 +08:00
'server_port' => $this->nodeInfo->server_port,
2023-03-08 02:28:00 +08:00
'server_name' => $this->nodeInfo->server_name,
];
break;
case 'hysteria':
$response = [
2023-09-25 22:01:15 +09:00
'version' => $this->nodeInfo->version,
2023-03-08 02:28:00 +08:00
'host' => $this->nodeInfo->host,
'server_port' => $this->nodeInfo->server_port,
'server_name' => $this->nodeInfo->server_name,
'up_mbps' => $this->nodeInfo->up_mbps,
'down_mbps' => $this->nodeInfo->down_mbps
2022-11-17 02:19:11 +08:00
];
if ($this->nodeInfo->version == 1) {
$response['obfs'] = $this->nodeInfo->obfs_password ?? null;
} elseif ($this->nodeInfo->version == 2) {
$response['obfs'] = $this->nodeInfo->obfs ?? null;
$response['obfs-password'] = $this->nodeInfo->obfs_password ?? null;
}
2022-05-02 13:37:11 +08:00
break;
}
2022-11-17 02:19:11 +08:00
$response['base_config'] = [
2022-12-15 23:30:47 +08:00
'push_interval' => (int)config('v2board.server_push_interval', 60),
'pull_interval' => (int)config('v2board.server_pull_interval', 60)
2022-11-17 02:19:11 +08:00
];
2022-11-29 14:33:08 +08:00
if ($this->nodeInfo['route_id']) {
$response['routes'] = $this->serverService->getRoutes($this->nodeInfo['route_id']);
}
2022-11-17 02:19:11 +08:00
$eTag = sha1(json_encode($response));
if (strpos($request->header('If-None-Match'), $eTag) !== false ) {
abort(304);
}
return response($response)->header('ETag', "\"{$eTag}\"");
2022-05-02 13:37:11 +08:00
}
}