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

171 lines
6.0 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
2020-01-29 16:08:50 +08:00
namespace App\Http\Controllers\Client;
2019-10-29 15:33:36 +08:00
use App\Http\Controllers\Controller;
2020-01-29 16:08:50 +08:00
use Illuminate\Http\Request;
2019-10-29 15:33:36 +08:00
use App\Models\Server;
2019-11-24 00:41:37 +08:00
use App\Utils\Helper;
2019-10-29 15:33:36 +08:00
use Symfony\Component\Yaml\Yaml;
2020-02-28 01:12:55 +08:00
use App\Services\UserService;
2019-10-29 15:33:36 +08:00
class ClientController extends Controller
{
2020-01-11 13:36:52 +08:00
public function subscribe(Request $request)
{
2019-10-29 15:33:36 +08:00
$user = $request->user;
$server = [];
2020-02-07 19:34:24 +08:00
// account not expired and is not banned.
2020-02-28 17:16:05 +08:00
$userService = new UserService();
if ($userService->isAvailable($user)) {
2020-01-11 13:36:52 +08:00
$servers = Server::where('show', 1)
->orderBy('name')
->get();
foreach ($servers as $item) {
$groupId = json_decode($item['group_id']);
if (in_array($user->group_id, $groupId)) {
array_push($server, $item);
}
}
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
if (isset($_SERVER['HTTP_USER_AGENT'])) {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Quantumult%20X') !== false) {
die($this->quantumultX($user, $server));
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Quantumult') !== false) {
die($this->quantumult($user, $server));
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'clash') !== false) {
die($this->clash($user, $server));
}
2019-10-29 15:33:36 +08:00
}
die($this->origin($user, $server));
}
2020-01-11 13:36:52 +08:00
private function quantumultX($user, $server)
{
$uri = '';
foreach ($server as $item) {
$uri .= "vmess=" . $item->host . ":" . $item->port . ", method=none, password=" . $user->v2ray_uuid . ", fast-open=false, udp-relay=false, tag=" . $item->name;
if ($item->network == 'ws') {
2020-03-14 15:33:31 +08:00
$uri .= ', obfs=' . ($item->tls ? 'wss' : 'ws');
2020-03-10 21:10:44 +08:00
if ($item->networkSettings) {
$wsSettings = json_decode($item->networkSettings);
2020-01-11 13:36:52 +08:00
if (isset($wsSettings->path)) $uri .= ', obfs-uri=' . $wsSettings->path;
if (isset($wsSettings->headers->Host)) $uri .= ', obfs-host=' . $wsSettings->headers->Host;
}
}
$uri .= "\r\n";
2019-11-06 22:52:59 +08:00
}
2020-01-11 13:36:52 +08:00
return base64_encode($uri);
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
private function quantumult($user, $server)
{
$uri = '';
header('subscription-userinfo: upload=' . $user->u . '; download=' . $user->d . ';total=' . $user->transfer_enable);
foreach ($server as $item) {
$str = '';
$str .= $item->name . '= vmess, ' . $item->host . ', ' . $item->port . ', chacha20-ietf-poly1305, "' . $user->v2ray_uuid . '", over-tls=' . ($item->tls ? "true" : "false") . ', certificate=0, group=' . config('v2board.app_name', 'V2Board');
if ($item->network === 'ws') {
2020-03-21 19:50:29 +08:00
$str .= ', obfs=ws';
2020-03-10 21:10:44 +08:00
if ($item->networkSettings) {
$wsSettings = json_decode($item->networkSettings);
2020-01-11 13:36:52 +08:00
if (isset($wsSettings->path)) $str .= ', obfs-path="' . $wsSettings->path . '"';
if (isset($wsSettings->headers->Host)) $str .= ', obfs-header="Host:' . $wsSettings->headers->Host . '"';
}
}
$uri .= "vmess://" . base64_encode($str) . "\r\n";
2019-11-06 22:52:59 +08:00
}
2020-01-11 13:36:52 +08:00
return base64_encode($uri);
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
private function origin($user, $server)
{
$uri = '';
foreach ($server as $item) {
$uri .= Helper::buildVmessLink($item, $user);
}
return base64_encode($uri);
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
private function clash($user, $server)
{
$proxy = [];
$proxyGroup = [];
$proxies = [];
2020-02-02 21:50:58 +08:00
$rules = [];
2020-01-11 13:36:52 +08:00
foreach ($server as $item) {
$array = [];
$array['name'] = $item->name;
$array['type'] = 'vmess';
$array['server'] = $item->host;
$array['port'] = $item->port;
$array['uuid'] = $user->v2ray_uuid;
$array['alterId'] = $user->v2ray_alter_id;
$array['cipher'] = 'auto';
if ($item->tls) {
$array['tls'] = true;
$array['skip-cert-verify'] = true;
2020-01-11 13:36:52 +08:00
}
if ($item->network == 'ws') {
$array['network'] = $item->network;
2020-03-10 21:10:44 +08:00
if ($item->networkSettings) {
$wsSettings = json_decode($item->networkSettings);
2020-01-11 13:36:52 +08:00
if (isset($wsSettings->path)) $array['ws-path'] = $wsSettings->path;
if (isset($wsSettings->headers->Host)) $array['ws-headers'] = [
'Host' => $wsSettings->headers->Host
];
}
}
array_push($proxy, $array);
array_push($proxies, $item->name);
2019-11-11 20:37:56 +08:00
}
2020-02-02 21:50:58 +08:00
array_push($proxyGroup, [
'name' => 'auto',
'type' => 'url-test',
'proxies' => $proxies,
'url' => 'https://www.bing.com',
'interval' => 300
]);
2020-01-11 13:36:52 +08:00
array_push($proxyGroup, [
2020-02-02 21:50:58 +08:00
'name' => 'fallback-auto',
'type' => 'fallback',
'proxies' => $proxies,
'url' => 'https://www.bing.com',
'interval' => 300
]);
array_push($proxyGroup, [
'name' => 'select',
2020-01-11 13:36:52 +08:00
'type' => 'select',
2020-03-04 19:33:33 +08:00
'proxies' => array_merge($proxies, [
'auto',
'fallback-auto'
])
2020-01-11 13:36:52 +08:00
]);
2020-02-02 21:50:58 +08:00
try {
2020-03-04 19:33:33 +08:00
$rules = [];
2020-03-04 19:34:47 +08:00
foreach (glob(base_path() . '/resources/rules/' . '*.clash.yaml') as $file) {
2020-03-04 19:33:33 +08:00
$rules = array_merge($rules, Yaml::parseFile($file)['Rule']);
}
2020-02-02 21:50:58 +08:00
} catch (\Exception $e) {}
2020-01-11 13:36:52 +08:00
$config = [
'port' => 7890,
2020-02-02 21:50:58 +08:00
'socks-port' => 7891,
2020-01-11 13:36:52 +08:00
'allow-lan' => false,
'mode' => 'Rule',
'log-level' => 'info',
'external-controller' => '0.0.0.0:9090',
'secret' => '',
'Proxy' => $proxy,
'Proxy Group' => $proxyGroup,
2020-02-02 21:50:58 +08:00
'Rule' => $rules
2020-01-11 13:36:52 +08:00
];
2020-02-02 21:51:49 +08:00
2020-01-11 13:36:52 +08:00
return Yaml::dump($config);
2019-10-29 15:33:36 +08:00
}
}