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

258 lines
9.6 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-06-12 00:18:35 +08:00
use App\Services\ServerService;
use App\Utils\Clash;
2021-07-02 21:14:07 +08:00
use App\Utils\Origin;
2020-06-12 00:35:35 +08:00
use App\Utils\QuantumultX;
use App\Utils\Shadowrocket;
2021-07-02 20:25:26 +08:00
use App\Utils\Shadowsocks;
2020-06-13 18:33:42 +08:00
use App\Utils\Surge;
use App\Utils\Surfboard;
2020-01-29 16:08:50 +08:00
use Illuminate\Http\Request;
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)
{
2020-10-24 23:08:14 +08:00
$flag = $request->input('flag')
2020-10-24 23:43:45 +08:00
?? (isset($_SERVER['HTTP_USER_AGENT'])
2020-10-24 23:08:14 +08:00
? $_SERVER['HTTP_USER_AGENT']
2020-10-24 23:43:45 +08:00
: '');
2020-10-24 23:08:14 +08:00
$flag = strtolower($flag);
2019-10-29 15:33:36 +08:00
$user = $request->user;
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-06-12 00:18:35 +08:00
$serverService = new ServerService();
2020-11-14 17:26:17 +08:00
$servers = $serverService->getAvailableServers($user);
2020-10-24 23:08:14 +08:00
if ($flag) {
if (strpos($flag, 'quantumult%20x') !== false) {
2020-11-14 17:26:17 +08:00
die($this->quantumultX($user, $servers));
2020-06-14 12:33:47 +08:00
}
2020-10-24 23:08:14 +08:00
if (strpos($flag, 'clash') !== false) {
2020-11-14 17:26:17 +08:00
die($this->clash($user, $servers));
2020-06-14 12:33:47 +08:00
}
2020-10-24 23:08:14 +08:00
if (strpos($flag, 'surfboard') !== false) {
2020-11-14 17:26:17 +08:00
die($this->surfboard($user, $servers));
2020-06-14 12:33:47 +08:00
}
2020-10-24 23:08:14 +08:00
if (strpos($flag, 'surge') !== false) {
2020-11-14 17:26:17 +08:00
die($this->surge($user, $servers));
2020-06-14 12:33:47 +08:00
}
2020-10-24 23:08:14 +08:00
if (strpos($flag, 'shadowrocket') !== false) {
2020-11-14 17:26:17 +08:00
die($this->shadowrocket($user, $servers));
}
if (strpos($flag, 'shadowsocks') !== false) {
die($this->shaodowsocksSIP008($user, $servers));
}
}
2021-07-02 21:14:07 +08:00
die($this->origin($user, $servers));
}
}
2019-10-29 15:33:36 +08:00
2021-07-02 21:14:07 +08:00
private function origin($user, $servers = [])
{
$uri = '';
foreach ($servers as $item) {
if ($item['type'] === 'shadowsocks') {
$uri .= Origin::buildShadowsocks($item, $user);
}
if ($item['type'] === 'v2ray') {
$uri .= Origin::buildVmess($item, $user);
}
if ($item['type'] === 'trojan') {
$uri .= Origin::buildTrojan($item, $user);
}
}
return base64_encode($uri);
}
2020-11-14 17:26:17 +08:00
private function shadowrocket($user, $servers = [])
{
$uri = '';
//display remaining traffic and expire date
$upload = round($user['u'] / (1024*1024*1024), 2);
$download = round($user['d'] / (1024*1024*1024), 2);
$totalTraffic = round($user['transfer_enable'] / (1024*1024*1024), 2);
$expiredDate = date('Y-m-d', $user['expired_at']);
$uri .= "STATUS=🚀↑:{$upload}GB,↓:{$download}GB,TOT:{$totalTraffic}GB💡Expires:{$expiredDate}\r\n";
2020-11-14 17:26:17 +08:00
foreach ($servers as $item) {
if ($item['type'] === 'shadowsocks') {
2020-11-15 17:10:32 +08:00
$uri .= Shadowrocket::buildShadowsocks($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
}
if ($item['type'] === 'v2ray') {
2020-11-15 17:10:32 +08:00
$uri .= Shadowrocket::buildVmess($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
}
if ($item['type'] === 'trojan') {
2020-11-15 17:10:32 +08:00
$uri .= Shadowrocket::buildTrojan($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
}
}
return base64_encode($uri);
}
2020-11-14 17:26:17 +08:00
private function quantumultX($user, $servers = [])
2020-01-11 13:36:52 +08:00
{
$uri = '';
header("subscription-userinfo: upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}");
2020-11-14 17:26:17 +08:00
foreach ($servers as $item) {
if ($item['type'] === 'shadowsocks') {
2020-11-15 17:10:32 +08:00
$uri .= QuantumultX::buildShadowsocks($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
}
if ($item['type'] === 'v2ray') {
2020-11-15 17:10:32 +08:00
$uri .= QuantumultX::buildVmess($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
}
if ($item['type'] === 'trojan') {
2020-11-15 17:10:32 +08:00
$uri .= QuantumultX::buildTrojan($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
}
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
}
private function shaodowsocksSIP008($user, $servers = [])
{
$configs = [];
$subs = [];
$subs['servers'] = [];
$subs['bytes_used'] = '';
$subs['bytes_remaining'] = '';
$bytesUsed = $user['u'] + $user['d'];
$bytesRemaining = $user['transfer_enable'] - $bytesUsed;
foreach ($servers as $item) {
if ($item['type'] === 'shadowsocks') {
2021-07-02 20:25:26 +08:00
array_push($configs, Shadowsocks::SIP008($item, $user));
}
}
$subs['version'] = 1;
$subs['bytes_used'] = $bytesUsed;
$subs['bytes_remaining'] = $bytesRemaining;
$subs['servers'] = array_merge($subs['servers'] ? $subs['servers'] : [], $configs);
return json_encode($subs, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
}
2020-11-14 17:26:17 +08:00
private function surge($user, $servers = [])
{
$proxies = '';
$proxyGroup = '';
2020-11-14 17:26:17 +08:00
foreach ($servers as $item) {
if ($item['type'] === 'shadowsocks') {
// [Proxy]
2020-11-15 17:10:32 +08:00
$proxies .= Surge::buildShadowsocks($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
// [Proxy Group]
2020-11-15 17:10:32 +08:00
$proxyGroup .= $item['name'] . ', ';
2020-11-14 17:26:17 +08:00
}
if ($item['type'] === 'v2ray') {
// [Proxy]
2020-11-15 17:10:32 +08:00
$proxies .= Surge::buildVmess($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
// [Proxy Group]
2020-11-15 17:10:32 +08:00
$proxyGroup .= $item['name'] . ', ';
2020-11-14 17:26:17 +08:00
}
if ($item['type'] === 'trojan') {
// [Proxy]
2020-11-15 17:10:32 +08:00
$proxies .= Surge::buildTrojan($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
// [Proxy Group]
2020-11-15 17:10:32 +08:00
$proxyGroup .= $item['name'] . ', ';
2020-11-14 17:26:17 +08:00
}
}
$defaultConfig = base_path() . '/resources/rules/default.surge.conf';
$customConfig = base_path() . '/resources/rules/custom.surge.conf';
if (\File::exists($customConfig)) {
$config = file_get_contents("$customConfig");
} else {
$config = file_get_contents("$defaultConfig");
}
// Subscription link
$subsURL = config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL'))) . '/api/v1/client/subscribe?token=' . $user['token'];
2020-06-13 18:33:42 +08:00
$config = str_replace('$subs_link', $subsURL, $config);
$config = str_replace('$proxies', $proxies, $config);
$config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
return $config;
}
2020-11-14 17:26:17 +08:00
private function surfboard($user, $servers = [])
{
$proxies = '';
$proxyGroup = '';
2020-11-14 17:26:17 +08:00
foreach ($servers as $item) {
if ($item['type'] === 'shadowsocks') {
// [Proxy]
2020-11-15 17:10:32 +08:00
$proxies .= Surfboard::buildShadowsocks($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
// [Proxy Group]
2020-11-15 17:10:32 +08:00
$proxyGroup .= $item['name'] . ', ';
2020-11-14 17:26:17 +08:00
}
if ($item['type'] === 'v2ray') {
// [Proxy]
2020-11-15 17:10:32 +08:00
$proxies .= Surfboard::buildVmess($user['uuid'], $item);
2020-11-14 17:26:17 +08:00
// [Proxy Group]
2020-11-15 17:10:32 +08:00
$proxyGroup .= $item['name'] . ', ';
2020-11-14 17:26:17 +08:00
}
}
$defaultConfig = base_path() . '/resources/rules/default.surfboard.conf';
$customConfig = base_path() . '/resources/rules/custom.surfboard.conf';
if (\File::exists($customConfig)) {
$config = file_get_contents("$customConfig");
} else {
$config = file_get_contents("$defaultConfig");
}
// Subscription link
$subsURL = config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL'))) . '/api/v1/client/subscribe?token=' . $user['token'];
2020-06-11 20:47:02 +08:00
$config = str_replace('$subs_link', $subsURL, $config);
$config = str_replace('$proxies', $proxies, $config);
$config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
return $config;
}
2020-11-14 17:26:17 +08:00
private function clash($user, $servers = [])
2020-01-11 13:36:52 +08:00
{
header("subscription-userinfo: upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}");
2020-05-26 15:34:29 +08:00
$defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
$customConfig = base_path() . '/resources/rules/custom.clash.yaml';
if (\File::exists($customConfig)) {
$config = Yaml::parseFile($customConfig);
} else {
$config = Yaml::parseFile($defaultConfig);
}
2020-01-11 13:36:52 +08:00
$proxy = [];
$proxies = [];
2020-10-04 14:21:09 +08:00
2020-11-14 17:26:17 +08:00
foreach ($servers as $item) {
if ($item['type'] === 'shadowsocks') {
2020-11-15 17:10:32 +08:00
array_push($proxy, Clash::buildShadowsocks($user['uuid'], $item));
array_push($proxies, $item['name']);
2020-11-14 17:26:17 +08:00
}
if ($item['type'] === 'v2ray') {
2020-11-15 17:10:32 +08:00
array_push($proxy, Clash::buildVmess($user['uuid'], $item));
array_push($proxies, $item['name']);
2020-11-14 17:26:17 +08:00
}
if ($item['type'] === 'trojan') {
2020-11-15 17:10:32 +08:00
array_push($proxy, Clash::buildTrojan($user['uuid'], $item));
array_push($proxies, $item['name']);
2020-11-14 17:26:17 +08:00
}
2019-11-11 20:37:56 +08:00
}
2020-02-02 21:50:58 +08:00
2020-07-05 13:18:43 +08:00
$config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
foreach ($config['proxy-groups'] as $k => $v) {
2020-09-01 10:08:42 +08:00
if (!is_array($config['proxy-groups'][$k]['proxies'])) continue;
2020-07-05 13:18:43 +08:00
$config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
2020-05-26 15:34:29 +08:00
}
$yaml = Yaml::dump($config);
$yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
return $yaml;
2019-10-29 15:33:36 +08:00
}
}