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

125 lines
4.9 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-06-11 20:47:02 +08:00
use App\Services\UserService;
2020-01-29 16:08:50 +08:00
use Illuminate\Http\Request;
2019-12-02 17:07:44 +08:00
use App\Models\Server;
2020-06-11 20:47:02 +08:00
use Symfony\Component\Yaml\Yaml;
2019-12-02 17:07:44 +08:00
class AppController extends Controller
{
2020-01-11 13:36:52 +08:00
public function data(Request $request)
{
2020-06-11 20:47:02 +08:00
$server = [];
2019-12-02 17:07:44 +08:00
$user = $request->user;
2020-06-11 20:47:02 +08:00
$userService = new UserService();
if ($userService->isAvailable($user)) {
$servers = Server::where('show', 1)
->orderBy('sort', 'ASC')
->get();
foreach ($servers as $item) {
$groupId = json_decode($item['group_id']);
if (in_array($user->group_id, $groupId)) {
array_push($server, $item);
}
}
}
$config = Yaml::parseFile(base_path() . '/resources/rules/app.clash.yaml');
$proxy = [];
$proxies = [];
foreach ($server as $item) {
$array = [];
$array['name'] = $item->name;
$array['type'] = 'vmess';
$array['server'] = $item->host;
$array['port'] = $item->port;
$array['uuid'] = $user->uuid;
$array['alterId'] = $user->v2ray_alter_id;
$array['cipher'] = 'auto';
if ($item->tls) {
$tlsSettings = json_decode($item->tlsSettings);
$array['tls'] = true;
if (isset($tlsSettings->allowInsecure)) $array['skip-cert-verify'] = ($tlsSettings->allowInsecure ? true : false );
2019-12-02 17:07:44 +08:00
}
2020-06-11 20:47:02 +08:00
if ($item->network == 'ws') {
$array['network'] = $item->network;
if ($item->networkSettings) {
$wsSettings = json_decode($item->networkSettings);
if (isset($wsSettings->path)) $array['ws-path'] = $wsSettings->path;
if (isset($wsSettings->headers->Host)) $array['ws-headers'] = [
'Host' => $wsSettings->headers->Host
];
2019-12-02 17:07:44 +08:00
}
}
2020-06-11 20:47:02 +08:00
array_push($proxy, $array);
array_push($proxies, $item->name);
}
$config['Proxy'] = array_merge($config['Proxy'] ? $config['Proxy'] : [], $proxy);
foreach ($config['Proxy Group'] as $k => $v) {
$config['Proxy Group'][$k]['proxies'] = array_merge($config['Proxy Group'][$k]['proxies'], $proxies);
2019-12-02 17:07:44 +08:00
}
2020-06-11 20:47:02 +08:00
die(Yaml::dump($config));
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;
2020-03-17 21:21:35 +08:00
if ($user->expired_at < time() && $user->expired_at !== NULL) {
2019-12-10 09:53:34 +08:00
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;
2020-06-08 01:08:07 +08:00
$json->outbound->settings->vnext[0]->users[0]->id = (string)$user->uuid;
2019-12-10 09:53:34 +08:00
$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
}