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

168 lines
6.5 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Http\Controllers\Server;
2020-02-28 15:16:23 +08:00
use App\Services\ServerService;
2019-10-29 15:33:36 +08:00
use Illuminate\Http\Request;
2020-01-31 00:03:18 +08:00
use App\Http\Controllers\Controller;
2019-10-29 15:33:36 +08:00
use App\Models\User;
use App\Models\Server;
use App\Models\ServerLog;
use Illuminate\Support\Facades\Log;
2020-01-12 01:27:36 +08:00
use Illuminate\Support\Facades\Cache;
2019-10-29 15:33:36 +08:00
class DeepbworkController extends Controller
{
CONST SERVER_CONFIG = '{"api":{"services":["HandlerService","StatsService"],"tag":"api"},"stats":{},"inbound":{"port":443,"protocol":"vmess","settings":{"clients":[]},"sniffing":{"enabled": true,"destOverride": ["http","tls"]},"streamSettings":{"network":"tcp"},"tag":"proxy"},"inboundDetour":[{"listen":"0.0.0.0","port":23333,"protocol":"dokodemo-door","settings":{"address":"0.0.0.0"},"tag":"api"}],"log":{"loglevel":"debug","access":"access.log","error":"error.log"},"outbound":{"protocol":"freedom","settings":{}},"outboundDetour":[{"protocol":"blackhole","settings":{},"tag":"block"}],"routing":{"rules":[{"inboundTag":"api","outboundTag":"api","type":"field"}]},"policy":{"levels":{"0":{"handshake":4,"connIdle":300,"uplinkOnly":5,"downlinkOnly":30,"statsUserUplink":true,"statsUserDownlink":true}}}}';
2020-01-11 13:36:52 +08:00
2020-01-31 00:03:18 +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');
}
}
2019-10-29 15:33:36 +08:00
// 后端获取用户
2020-01-11 13:36:52 +08:00
public function user(Request $request)
{
2019-10-29 15:33:36 +08:00
$nodeId = $request->input('node_id');
$server = Server::find($nodeId);
2019-11-25 00:22:52 +08:00
if (!$server) {
abort(500, 'fail');
2019-11-25 00:20:26 +08:00
}
Cache::put('server_last_check_at_' . $server->id, time());
2020-02-28 15:16:23 +08:00
$serverService = new ServerService();
$users = $serverService->getAvailableUsers(json_decode($server->group_id));
2019-10-29 15:33:36 +08:00
$result = [];
foreach ($users as $user) {
$user->v2ray_user = [
"uuid" => $user->v2ray_uuid,
2020-01-31 17:33:55 +08:00
"email" => sprintf("%s@v2board.user", $user->v2ray_uuid),
2019-10-29 15:33:36 +08:00
"alter_id" => $user->v2ray_alter_id,
"level" => $user->v2ray_level,
];
unset($user['v2ray_uuid']);
unset($user['v2ray_alter_id']);
unset($user['v2ray_level']);
array_push($result, $user);
}
return response([
'msg' => 'ok',
'data' => $result,
]);
}
// 后端提交数据
2020-01-11 13:36:52 +08:00
public function submit(Request $request)
{
2020-03-07 11:37:39 +08:00
// Log::info('serverSubmitData:' . $request->input('node_id') . ':' . file_get_contents('php://input'));
2019-10-29 15:33:36 +08:00
$server = Server::find($request->input('node_id'));
if (!$server) {
2020-01-11 13:36:52 +08:00
return response([
'ret' => 1,
'msg' => 'ok'
]);
2019-10-29 15:33:36 +08:00
}
$data = file_get_contents('php://input');
$data = json_decode($data, true);
foreach ($data as $item) {
2020-01-11 13:36:52 +08:00
$u = $item['u'] * $server->rate;
$d = $item['d'] * $server->rate;
$user = User::find($item['user_id']);
$user->t = time();
$user->u = $user->u + $u;
$user->d = $user->d + $d;
2019-10-29 15:33:36 +08:00
$user->save();
2020-01-11 13:36:52 +08:00
2019-10-29 15:33:36 +08:00
$serverLog = new ServerLog();
$serverLog->user_id = $item['user_id'];
2019-12-22 23:45:05 +08:00
$serverLog->server_id = $request->input('node_id');
2019-10-29 15:33:36 +08:00
$serverLog->u = $item['u'];
$serverLog->d = $item['d'];
$serverLog->rate = $server->rate;
$serverLog->save();
}
2020-01-11 13:36:52 +08:00
return response([
'ret' => 1,
'msg' => 'ok'
]);
2019-10-29 15:33:36 +08:00
}
// 后端获取配置
2020-01-11 13:36:52 +08:00
public function config(Request $request)
{
2019-10-29 15:33:36 +08:00
$nodeId = $request->input('node_id');
$localPort = $request->input('local_port');
2019-12-11 21:43:20 +08:00
if (empty($nodeId) || empty($localPort)) {
2020-02-05 18:46:10 +08:00
abort(500, '参数错误');
2019-12-11 21:43:20 +08:00
}
2019-10-29 15:33:36 +08:00
$server = Server::find($nodeId);
2019-12-11 21:43:20 +08:00
if (!$server) {
2020-02-05 18:46:10 +08:00
abort(500, '节点不存在');
2019-12-11 21:43:20 +08:00
}
2019-12-10 09:53:34 +08:00
$json = json_decode(self::SERVER_CONFIG);
$json->inboundDetour[0]->port = (int)$localPort;
$json->inbound->port = (int)$server->server_port;
$json->inbound->streamSettings->network = $server->network;
2019-11-06 01:12:40 +08:00
if ($server->settings) {
switch ($server->network) {
2020-01-11 13:36:52 +08:00
case 'tcp':
$json->inbound->streamSettings->tcpSettings = json_decode($server->settings);
2019-11-06 01:12:40 +08:00
break;
2020-01-11 13:36:52 +08:00
case 'kcp':
$json->inbound->streamSettings->kcpSettings = json_decode($server->settings);
2019-11-06 01:12:40 +08:00
break;
2020-01-11 13:36:52 +08:00
case 'ws':
$json->inbound->streamSettings->wsSettings = json_decode($server->settings);
2019-11-06 01:12:40 +08:00
break;
2020-01-11 13:36:52 +08:00
case 'http':
$json->inbound->streamSettings->httpSettings = json_decode($server->settings);
2019-11-06 01:12:40 +08:00
break;
2020-01-11 13:36:52 +08:00
case 'domainsocket':
$json->inbound->streamSettings->dsSettings = json_decode($server->settings);
2019-11-06 01:12:40 +08:00
break;
2020-01-11 13:36:52 +08:00
case 'quic':
$json->inbound->streamSettings->quicSettings = json_decode($server->settings);
2019-11-06 01:12:40 +08:00
break;
}
}
2020-02-10 21:24:49 +08:00
if ($server->rules) {
$rules = json_decode($server->rules);
// domain
2020-02-23 17:20:24 +08:00
if (isset($rules->domain) && !empty($rules->domain)) {
2020-02-16 16:51:25 +08:00
$domainObj = new \StdClass();
$domainObj->type = 'field';
$domainObj->domain = $rules->domain;
$domainObj->outboundTag = 'block';
array_push($json->routing->rules, $domainObj);
2020-02-16 16:51:25 +08:00
}
2020-02-10 21:24:49 +08:00
// protocol
2020-02-23 17:20:24 +08:00
if (isset($rules->protocol) && !empty($rules->protocol)) {
2020-02-16 16:51:25 +08:00
$protocolObj = new \StdClass();
$protocolObj->type = 'field';
$protocolObj->protocol = $rules->protocol;
$protocolObj->outboundTag = 'block';
array_push($json->routing->rules, $protocolObj);
2020-02-16 16:51:25 +08:00
}
2020-02-10 21:24:49 +08:00
}
2019-10-29 15:33:36 +08:00
if ((int)$server->tls) {
2020-02-05 18:46:10 +08:00
$json->inbound->streamSettings->security = 'tls';
$tls = (object)[
'certificateFile' => '/home/v2ray.crt',
'keyFile' => '/home/v2ray.key'
];
$json->inbound->streamSettings->tlsSettings = new \StdClass();
2019-12-10 09:53:34 +08:00
$json->inbound->streamSettings->tlsSettings->certificates[0] = $tls;
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
2019-12-10 09:53:34 +08:00
die(json_encode($json, JSON_UNESCAPED_UNICODE));
2019-10-29 15:33:36 +08:00
}
}