v2board/app/Http/Controllers/Server/PoseidonController.php

159 lines
4.8 KiB
PHP
Raw Normal View History

2020-01-31 12:50:11 +08:00
<?php
namespace App\Http\Controllers\Server;
2020-02-29 17:00:28 +08:00
use App\Services\ServerService;
2020-04-25 19:44:47 +08:00
use App\Services\UserService;
2020-06-05 13:25:32 +08:00
use App\Utils\CacheKey;
2020-01-31 12:50:11 +08:00
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Plan;
use App\Models\Server;
use App\Models\ServerLog;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
2020-06-18 02:25:05 +08:00
/*
* V2ray Poseidon
* Github: https://github.com/ColetteContreras/trojan-poseidon
*/
2020-01-31 12:50:11 +08:00
class PoseidonController extends Controller
{
2020-05-23 15:08:33 +08:00
public $poseidonVersion;
public function __construct(Request $request)
{
$this->poseidonVersion = $request->input('poseidon_version');
}
2020-01-31 12:50:11 +08:00
// 后端获取用户
public function user(Request $request)
{
if ($r = $this->verifyToken($request)) { return $r; }
$nodeId = $request->input('node_id');
$server = Server::find($nodeId);
if (!$server) {
return $this->error("server could not be found", 404);
}
2020-07-03 15:24:10 +08:00
Cache::put(CacheKey::get('SERVER_V2RAY_LAST_CHECK_AT', $server->id), time(), 3600);
2020-02-29 17:00:28 +08:00
$serverService = new ServerService();
2021-08-06 00:43:01 +08:00
$users = $serverService->getAvailableUsers($server->group_id);
2020-01-31 12:50:11 +08:00
$result = [];
foreach ($users as $user) {
$user->v2ray_user = [
2020-06-08 01:08:07 +08:00
"uuid" => $user->uuid,
"email" => sprintf("%s@v2board.user", $user->uuid),
2020-11-18 17:56:27 +08:00
"alter_id" => $server->alter_id,
"level" => 0,
2020-01-31 12:50:11 +08:00
];
2020-06-08 01:08:07 +08:00
unset($user['uuid']);
2020-11-18 17:56:27 +08:00
unset($user['email']);
2020-01-31 12:50:11 +08:00
array_push($result, $user);
}
return $this->success($result);
}
// 后端提交数据
public function submit(Request $request)
{
if ($r = $this->verifyToken($request)) { return $r; }
$server = Server::find($request->input('node_id'));
if (!$server) {
return $this->error("server could not be found", 404);
}
$data = file_get_contents('php://input');
$data = json_decode($data, true);
2020-07-03 15:24:10 +08:00
Cache::put(CacheKey::get('SERVER_V2RAY_ONLINE_USER', $server->id), count($data), 3600);
Cache::put(CacheKey::get('SERVER_V2RAY_LAST_PUSH_AT', $server->id), time(), 3600);
2020-04-25 19:44:47 +08:00
$userService = new UserService();
2020-01-31 12:50:11 +08:00
foreach ($data as $item) {
$u = $item['u'] * $server->rate;
$d = $item['d'] * $server->rate;
2020-11-09 00:12:19 +08:00
if (!$userService->trafficFetch($u, $d, $item['user_id'], $server, 'vmess')) {
2020-05-28 17:05:36 +08:00
return $this->error("user fetch fail", 500);
2020-04-25 19:44:47 +08:00
}
2020-01-31 12:50:11 +08:00
}
return $this->success('');
}
// 后端获取配置
public function config(Request $request)
{
if ($r = $this->verifyToken($request)) { return $r; }
$nodeId = $request->input('node_id');
$localPort = $request->input('local_port');
if (empty($nodeId) || empty($localPort)) {
return $this->error('invalid parameters', 400);
}
2020-02-17 11:30:58 +08:00
2020-03-19 15:25:38 +08:00
$serverService = new ServerService();
try {
2021-01-10 02:25:31 +08:00
$json = $serverService->getV2RayConfig($nodeId, $localPort);
2020-03-19 15:25:38 +08:00
$json->poseidon = [
'license_key' => (string)config('v2board.server_license'),
2020-02-17 11:30:58 +08:00
];
2020-05-23 15:08:33 +08:00
if ($this->poseidonVersion >= 'v1.5.0') {
// don't need it after v1.5.0
unset($json->inboundDetour);
unset($json->stats);
unset($json->api);
array_shift($json->routing->rules);
}
foreach($json->policy->levels as &$level) {
$level->handshake = 2;
$level->uplinkOnly = 2;
$level->downlinkOnly = 2;
$level->connIdle = 60;
}
2020-01-31 12:50:11 +08:00
2020-03-19 15:25:38 +08:00
return $this->success($json);
} catch (\Exception $e) {
return $this->error($e->getMessage(), 500);
}
2020-01-31 12:50:11 +08:00
}
protected function verifyToken(Request $request)
{
$token = $request->input('token');
if (empty($token)) {
return $this->error("token must be set");
}
if ($token !== config('v2board.server_token')) {
return $this->error("invalid token");
}
}
protected function error($msg, int $status = 400) {
return response([
'msg' => $msg,
], $status);
}
protected function success($data) {
2020-11-18 18:10:32 +08:00
$req = request();
// Only for "GET" method
if (!$req->isMethod('GET') || !$data) {
return response([
'msg' => 'ok',
'data' => $data,
]);
}
$etag = sha1(json_encode($data));
if ($etag == $req->header("IF-NONE-MATCH")) {
return response(null, 304);
}
2020-01-31 12:50:11 +08:00
return response([
'msg' => 'ok',
'data' => $data,
2020-11-18 18:10:32 +08:00
])->header('ETAG', $etag);
2020-01-31 12:50:11 +08:00
}
}