v2board/app/Http/Controllers/Client/AppController.php

112 lines
5.5 KiB
PHP
Raw Normal View History

2019-12-02 17:07:44 +08:00
<?php
2020-01-29 16:08:50 +08:00
namespace App\Http\Controllers\Client;
2019-12-02 17:07:44 +08:00
use App\Http\Controllers\Controller;
2020-01-29 16:08:50 +08:00
use Illuminate\Http\Request;
2019-12-02 17:07:44 +08:00
use App\Models\User;
use App\Models\Plan;
use App\Models\Server;
2019-12-25 14:50:04 +08:00
use App\Models\Notice;
2019-12-02 17:07:44 +08:00
use App\Utils\Helper;
class AppController extends Controller
{
2020-01-20 13:57:30 +08:00
CONST CLIENT_CONFIG = '{"policy":{"levels":{"0":{"uplinkOnly":0}}},"dns":{"servers":["114.114.114.114","8.8.8.8"]},"outboundDetour":[{"protocol":"freedom","tag":"direct","settings":{}}],"inbound":{"listen":"0.0.0.0","port":31211,"protocol":"socks","settings":{"auth":"noauth","udp":true,"ip":"127.0.0.1"}},"inboundDetour":[{"listen":"0.0.0.0","allocate":{"strategy":"always","refresh":5,"concurrency":3},"port":31210,"protocol":"http","tag":"httpDetour","domainOverride":["http","tls"],"streamSettings":{},"settings":{"timeout":0}}],"routing":{"strategy":"rules","settings":{"domainStrategy":"IPIfNonMatch","rules":[{"type":"field","ip":["geoip:cn"],"outboundTag":"direct"},{"type":"field","ip":["0.0.0.0/8","10.0.0.0/8","100.64.0.0/10","127.0.0.0/8","169.254.0.0/16","172.16.0.0/12","192.0.0.0/24","192.0.2.0/24","192.168.0.0/16","198.18.0.0/15","198.51.100.0/24","203.0.113.0/24","::1/128","fc00::/7","fe80::/10"],"outboundTag":"direct"}]}},"outbound":{"tag":"proxy","sendThrough":"0.0.0.0","mux":{"enabled":false,"concurrency":8},"protocol":"vmess","settings":{"vnext":[{"address":"server","port":443,"users":[{"id":"uuid","alterId":2,"security":"auto","level":0}],"remark":"remark"}]},"streamSettings":{"network":"tcp","tcpSettings":{"header":{"type":"none"}},"security":"none","tlsSettings":{"allowInsecure":true,"allowInsecureCiphers":true},"kcpSettings":{"header":{"type":"none"},"mtu":1350,"congestion":false,"tti":20,"uplinkCapacity":5,"writeBufferSize":1,"readBufferSize":1,"downlinkCapacity":20},"wsSettings":{"path":"","headers":{"Host":"server.cc"}}}}}';
2019-12-10 09:53:34 +08:00
CONST SOCKS_PORT = 10010;
CONST HTTP_PORT = 10011;
2020-02-15 23:04:50 +08:00
// TODO: 1.1.1 abolish
2020-01-11 13:36:52 +08:00
public function data(Request $request)
{
2019-12-02 17:07:44 +08:00
$user = $request->user;
$nodes = [];
if ($user->plan_id) {
$user['plan'] = Plan::find($user->plan_id);
if (!$user['plan']) {
abort(500, '订阅计划不存在');
}
if ($user->expired_at > time()) {
2019-12-22 23:54:33 +08:00
$servers = Server::where('show', 1)
->orderBy('name')
->get();
2019-12-02 17:07:44 +08:00
foreach ($servers as $item) {
$groupId = json_decode($item['group_id']);
if (in_array($user->group_id, $groupId)) {
array_push($nodes, $item);
}
}
}
}
return response([
2019-12-02 17:10:31 +08:00
'data' => [
'nodes' => $nodes,
'u' => $user->u,
'd' => $user->d,
'transfer_enable' => $user->transfer_enable,
2019-12-10 11:59:11 +08:00
'expired_at' => $user->expired_at,
2019-12-25 14:50:04 +08:00
'plan' => isset($user['plan']) ? $user['plan'] : false,
'notice' => Notice::orderBy('created_at', 'DESC')->first()
2019-12-02 17:10:31 +08:00
]
2019-12-02 17:07:44 +08:00
]);
}
2019-12-10 09:53:34 +08:00
2020-01-11 13:36:52 +08:00
public function config(Request $request)
{
2019-12-10 09:53:34 +08:00
if (empty($request->input('server_id'))) {
abort(500, '参数错误');
}
$user = $request->user;
if ($user->expired_at < time()) {
abort(500, '订阅计划已过期');
}
$server = Server::where('show', 1)
->where('id', $request->input('server_id'))
->first();
if (!$server) {
abort(500, '服务器不存在');
}
$json = json_decode(self::CLIENT_CONFIG);
//socks
$json->inbound->port = (int)self::SOCKS_PORT;
//http
$json->inboundDetour[0]->port = (int)self::HTTP_PORT;
//other
$json->outbound->settings->vnext[0]->address = (string)$server->host;
$json->outbound->settings->vnext[0]->port = (int)$server->port;
$json->outbound->settings->vnext[0]->users[0]->id = (string)$user->v2ray_uuid;
$json->outbound->settings->vnext[0]->users[0]->alterId = (int)$user->v2ray_alter_id;
$json->outbound->settings->vnext[0]->remark = (string)$server->name;
$json->outbound->streamSettings->network = $server->network;
2020-03-10 21:13:41 +08:00
if ($server->networkSettings) {
2019-12-10 09:53:34 +08:00
switch ($server->network) {
2020-01-11 13:36:52 +08:00
case 'tcp':
2020-03-10 21:13:41 +08:00
$json->outbound->streamSettings->tcpSettings = json_decode($server->networkSettings);
2019-12-10 09:53:34 +08:00
break;
2020-01-11 13:36:52 +08:00
case 'kcp':
2020-03-10 21:13:41 +08:00
$json->outbound->streamSettings->kcpSettings = json_decode($server->networkSettings);
2019-12-10 09:53:34 +08:00
break;
2020-01-11 13:36:52 +08:00
case 'ws':
2020-03-10 21:13:41 +08:00
$json->outbound->streamSettings->wsSettings = json_decode($server->networkSettings);
2019-12-10 09:53:34 +08:00
break;
2020-01-11 13:36:52 +08:00
case 'http':
2020-03-10 21:13:41 +08:00
$json->outbound->streamSettings->httpSettings = json_decode($server->networkSettings);
2019-12-10 09:53:34 +08:00
break;
2020-01-11 13:36:52 +08:00
case 'domainsocket':
2020-03-10 21:13:41 +08:00
$json->outbound->streamSettings->dsSettings = json_decode($server->networkSettings);
2019-12-10 09:53:34 +08:00
break;
2020-01-11 13:36:52 +08:00
case 'quic':
2020-03-10 21:13:41 +08:00
$json->outbound->streamSettings->quicSettings = json_decode($server->networkSettings);
2019-12-10 09:53:34 +08:00
break;
}
}
2019-12-24 15:33:42 +08:00
if ($request->input('is_global')) {
2020-01-20 13:57:30 +08:00
$json->routing->settings->rules[0]->outboundTag = 'proxy';
2019-12-24 15:33:42 +08:00
}
2019-12-10 09:53:34 +08:00
if ($server->tls) {
$json->outbound->streamSettings->security = "tls";
}
die(json_encode($json, JSON_UNESCAPED_UNICODE));
}
2019-12-02 17:07:44 +08:00
}