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;
|
2020-06-12 00:35:35 +08:00
|
|
|
use App\Utils\QuantumultX;
|
2020-06-13 18:33:42 +08:00
|
|
|
use App\Utils\Surge;
|
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;
|
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();
|
|
|
|
$servers = $serverService->getAllServers($user);
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
if (isset($_SERVER['HTTP_USER_AGENT'])) {
|
2020-06-13 18:33:42 +08:00
|
|
|
$_SERVER['HTTP_USER_AGENT'] = strtolower($_SERVER['HTTP_USER_AGENT']);
|
|
|
|
if (strpos($_SERVER['HTTP_USER_AGENT'], 'quantumult%20x') !== false) {
|
2020-06-12 00:35:35 +08:00
|
|
|
die($this->quantumultX($user, $servers['vmess'], $servers['trojan']));
|
2020-01-11 13:36:52 +08:00
|
|
|
}
|
2020-06-13 18:33:42 +08:00
|
|
|
if (strpos($_SERVER['HTTP_USER_AGENT'], 'clash') !== false) {
|
2020-06-12 00:18:35 +08:00
|
|
|
die($this->clash($user, $servers['vmess'], $servers['trojan']));
|
2020-01-11 13:36:52 +08:00
|
|
|
}
|
2020-06-13 18:33:42 +08:00
|
|
|
if (strpos($_SERVER['HTTP_USER_AGENT'], 'surfboard') !== false) {
|
2020-06-12 00:18:35 +08:00
|
|
|
die($this->surfboard($user, $servers['vmess']));
|
2020-03-28 10:10:30 +08:00
|
|
|
}
|
2020-06-13 18:33:42 +08:00
|
|
|
if (strpos($_SERVER['HTTP_USER_AGENT'], 'surge') !== false) {
|
|
|
|
die($this->surge($user, $servers['vmess'], $servers['trojan']));
|
2020-03-28 10:10:30 +08:00
|
|
|
}
|
2020-06-13 18:33:42 +08:00
|
|
|
die($this->origin($user, $servers['vmess'], $servers['trojan']));
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 00:35:35 +08:00
|
|
|
private function quantumultX($user, $vmess = [], $trojan = [])
|
2020-01-11 13:36:52 +08:00
|
|
|
{
|
|
|
|
$uri = '';
|
2020-06-12 00:18:35 +08:00
|
|
|
foreach ($vmess as $item) {
|
2020-06-12 00:35:35 +08:00
|
|
|
$uri .= QuantumultX::buildVmess($user->uuid, $item);
|
2019-11-06 22:52:59 +08:00
|
|
|
}
|
2020-06-12 00:35:35 +08:00
|
|
|
foreach ($trojan as $item) {
|
|
|
|
$uri .= QuantumultX::buildTrojan($user->uuid, $item);
|
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-06-12 00:35:35 +08:00
|
|
|
private function origin($user, $vmess = [], $trojan = [])
|
2020-01-11 13:36:52 +08:00
|
|
|
{
|
|
|
|
$uri = '';
|
2020-06-12 00:18:35 +08:00
|
|
|
foreach ($vmess as $item) {
|
2020-01-11 13:36:52 +08:00
|
|
|
$uri .= Helper::buildVmessLink($item, $user);
|
|
|
|
}
|
2020-06-12 00:35:35 +08:00
|
|
|
foreach ($trojan as $item) {
|
|
|
|
$uri .= Helper::buildTrojanLink($item, $user);
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
return base64_encode($uri);
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 18:33:42 +08:00
|
|
|
private function surge($user, $vmess = [], $trojan = [])
|
2020-03-28 10:10:30 +08:00
|
|
|
{
|
|
|
|
$proxies = '';
|
|
|
|
$proxyGroup = '';
|
2020-06-12 00:18:35 +08:00
|
|
|
foreach ($vmess as $item) {
|
2020-03-28 10:10:30 +08:00
|
|
|
// [Proxy]
|
2020-06-13 18:33:42 +08:00
|
|
|
$proxies .= Surge::buildVmess($user->uuid, $item);
|
|
|
|
// [Proxy Group]
|
|
|
|
$proxyGroup .= $item->name . ', ';
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($trojan as $item) {
|
|
|
|
// [Proxy]
|
|
|
|
$proxies .= Surge::buildTrojan($user->uuid, $item);
|
2020-03-28 10:10:30 +08:00
|
|
|
// [Proxy Group]
|
|
|
|
$proxyGroup .= $item->name . ', ';
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:35:24 +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");
|
|
|
|
}
|
2020-03-28 10:10:30 +08:00
|
|
|
|
|
|
|
// Subscription link
|
|
|
|
$subsURL = 'http';
|
2020-04-05 01:22:56 +08:00
|
|
|
if (isset( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) == 'on') {
|
2020-03-28 10:10:30 +08:00
|
|
|
$subsURL .= 's';
|
|
|
|
}
|
|
|
|
$subsURL .= '://';
|
|
|
|
if ($_SERVER['SERVER_PORT'] != ('80' || '443')) {
|
|
|
|
$subsURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
|
|
|
|
} else {
|
|
|
|
$subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
|
|
|
|
}
|
|
|
|
|
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);
|
2020-05-27 09:35:24 +08:00
|
|
|
return $config;
|
2020-03-28 10:10:30 +08:00
|
|
|
}
|
|
|
|
|
2020-06-12 00:35:35 +08:00
|
|
|
private function surfboard($user, $vmess = [])
|
2020-05-26 09:08:45 +08:00
|
|
|
{
|
|
|
|
$proxies = '';
|
|
|
|
$proxyGroup = '';
|
2020-06-12 00:18:35 +08:00
|
|
|
foreach ($vmess as $item) {
|
2020-05-26 09:08:45 +08:00
|
|
|
// [Proxy]
|
2020-06-08 01:08:07 +08:00
|
|
|
$proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->uuid;
|
2020-05-26 09:08:45 +08:00
|
|
|
if ($item->tls) {
|
|
|
|
$tlsSettings = json_decode($item->tlsSettings);
|
|
|
|
$proxies .= ', tls=' . ($item->tls ? "true" : "false");
|
|
|
|
if (isset($tlsSettings->allowInsecure)) {
|
|
|
|
$proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($item->network == 'ws') {
|
|
|
|
$proxies .= ', ws=true';
|
|
|
|
if ($item->networkSettings) {
|
|
|
|
$wsSettings = json_decode($item->networkSettings);
|
|
|
|
if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
|
|
|
|
if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$proxies .= "\r\n";
|
|
|
|
// [Proxy Group]
|
|
|
|
$proxyGroup .= $item->name . ', ';
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:35:24 +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");
|
|
|
|
}
|
2020-05-26 09:08:45 +08:00
|
|
|
|
|
|
|
// Subscription link
|
|
|
|
$subsURL = 'http';
|
|
|
|
if (isset( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) == 'on') {
|
|
|
|
$subsURL .= 's';
|
|
|
|
}
|
|
|
|
$subsURL .= '://';
|
2020-03-28 10:10:30 +08:00
|
|
|
if ($_SERVER['SERVER_PORT'] != ('80' || '443')) {
|
|
|
|
$subsURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
|
|
|
|
} else {
|
|
|
|
$subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
|
|
|
|
}
|
|
|
|
|
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);
|
2020-05-27 09:35:24 +08:00
|
|
|
return $config;
|
2020-03-28 10:10:30 +08:00
|
|
|
}
|
|
|
|
|
2020-06-12 00:18:35 +08:00
|
|
|
private function clash($user, $vmess = [], $trojan = [])
|
2020-01-11 13:36:52 +08:00
|
|
|
{
|
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-06-12 00:18:35 +08:00
|
|
|
foreach ($vmess as $item) {
|
|
|
|
array_push($proxy, Clash::buildVmess($user->uuid, $item));
|
|
|
|
array_push($proxies, $item->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($trojan as $item) {
|
|
|
|
array_push($proxy, Clash::buildTrojan($user->uuid, $item));
|
2020-01-11 13:36:52 +08:00
|
|
|
array_push($proxies, $item->name);
|
2019-11-11 20:37:56 +08:00
|
|
|
}
|
2020-02-02 21:50:58 +08:00
|
|
|
|
2020-05-26 15:34:29 +08:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|