v2board/app/Http/Controllers/Client/Protocols/Surge.php

163 lines
5.7 KiB
PHP
Raw Normal View History

2020-06-13 18:33:42 +08:00
<?php
2021-07-02 22:34:42 +08:00
namespace App\Http\Controllers\Client\Protocols;
2020-06-13 18:33:42 +08:00
use App\Utils\Helper;
2020-06-13 18:33:42 +08:00
class Surge
{
2021-07-02 22:34:42 +08:00
public $flag = 'surge';
private $servers;
private $user;
public function __construct($user, $servers)
{
$this->user = $user;
$this->servers = $servers;
}
public function handle()
{
$servers = $this->servers;
$user = $this->user;
$appName = config('v2board.app_name', 'V2Board');
header("content-disposition:attachment;filename*=UTF-8''".rawurlencode($appName).".conf");
2021-07-02 22:34:42 +08:00
$proxies = '';
$proxyGroup = '';
foreach ($servers as $item) {
2022-11-18 02:39:28 +08:00
if ($item['type'] === 'shadowsocks'
2022-11-20 15:24:03 +08:00
&& in_array($item['cipher'], [
'aes-128-gcm',
'aes-192-gcm',
'aes-256-gcm',
'chacha20-ietf-poly1305'
])
2022-11-18 02:39:28 +08:00
) {
2021-07-02 22:34:42 +08:00
// [Proxy]
$proxies .= self::buildShadowsocks($user['uuid'], $item);
// [Proxy Group]
$proxyGroup .= $item['name'] . ', ';
}
if ($item['type'] === 'v2ray') {
// [Proxy]
$proxies .= self::buildVmess($user['uuid'], $item);
// [Proxy Group]
$proxyGroup .= $item['name'] . ', ';
}
if ($item['type'] === 'trojan') {
// [Proxy]
$proxies .= self::buildTrojan($user['uuid'], $item);
// [Proxy Group]
$proxyGroup .= $item['name'] . ', ';
}
}
$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
2022-05-11 01:01:55 +08:00
$subsURL = Helper::getSubscribeUrl("/api/v1/client/subscribe?token={$user['token']}");
$subsDomain = $_SERVER['SERVER_NAME'];
2021-12-11 21:24:27 +08:00
$subsURL = 'https://' . $subsDomain . '/api/v1/client/subscribe?token=' . $user['token'];
2021-07-02 22:34:42 +08:00
$config = str_replace('$subs_link', $subsURL, $config);
$config = str_replace('$subs_domain', $subsDomain, $config);
2021-07-02 22:34:42 +08:00
$config = str_replace('$proxies', $proxies, $config);
$config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
$upload = round($user['u'] / (1024*1024*1024), 2);
$download = round($user['d'] / (1024*1024*1024), 2);
$useTraffic = $upload + $download;
$totalTraffic = round($user['transfer_enable'] / (1024*1024*1024), 2);
$expireDate = $user['expired_at'] === NULL ? '长期有效' : date('Y-m-d H:i:s', $user['expired_at']);
$subscribeInfo = "title={$appName}订阅信息, content=上传流量:{$upload}GB\\n下载流量{$download}GB\\n剩余流量{$useTraffic}GB\\n套餐流量{$totalTraffic}GB\\n到期时间{$expireDate}";
$config = str_replace('$subscribe_info', $subscribeInfo, $config);
2021-07-02 22:34:42 +08:00
return $config;
}
public static function buildShadowsocks($password, $server)
{
$config = [
2020-11-15 17:10:32 +08:00
"{$server['name']}=ss",
"{$server['host']}",
"{$server['port']}",
"encrypt-method={$server['cipher']}",
"password={$password}",
'tfo=true',
'udp-relay=true'
];
$config = array_filter($config);
$uri = implode(',', $config);
$uri .= "\r\n";
return $uri;
}
2020-06-13 18:33:42 +08:00
public static function buildVmess($uuid, $server)
{
$config = [
2020-11-15 17:10:32 +08:00
"{$server['name']}=vmess",
"{$server['host']}",
"{$server['port']}",
"username={$uuid}",
"vmess-aead=true",
'tfo=true',
'udp-relay=true'
];
2020-11-20 00:26:39 +08:00
if ($server['tls']) {
array_push($config, 'tls=true');
if ($server['tlsSettings']) {
2021-08-06 00:43:01 +08:00
$tlsSettings = $server['tlsSettings'];
2020-11-20 00:26:39 +08:00
if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
2020-12-11 01:11:26 +08:00
array_push($config, 'skip-cert-verify=' . ($tlsSettings['allowInsecure'] ? 'true' : 'false'));
2020-11-20 00:26:39 +08:00
if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
2020-11-15 17:10:32 +08:00
array_push($config, "sni={$tlsSettings['serverName']}");
2020-06-13 18:33:42 +08:00
}
}
2020-11-15 17:10:32 +08:00
if ($server['network'] === 'ws') {
array_push($config, 'ws=true');
2020-11-15 17:10:32 +08:00
if ($server['networkSettings']) {
2021-08-06 00:43:01 +08:00
$wsSettings = $server['networkSettings'];
2020-11-20 00:26:39 +08:00
if (isset($wsSettings['path']) && !empty($wsSettings['path']))
array_push($config, "ws-path={$wsSettings['path']}");
if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
array_push($config, "ws-headers=Host:{$wsSettings['headers']['Host']}");
2020-06-13 18:33:42 +08:00
}
}
$uri = implode(',', $config);
$uri .= "\r\n";
return $uri;
2020-06-13 18:33:42 +08:00
}
public static function buildTrojan($password, $server)
{
2020-07-01 18:31:44 +08:00
$config = [
2020-11-15 17:10:32 +08:00
"{$server['name']}=trojan",
"{$server['host']}",
"{$server['port']}",
2020-07-01 18:31:44 +08:00
"password={$password}",
2020-11-15 17:10:32 +08:00
$server['server_name'] ? "sni={$server['server_name']}" : "",
'tfo=true',
'udp-relay=true'
2020-07-01 18:31:44 +08:00
];
2020-11-15 17:10:32 +08:00
if (!empty($server['allow_insecure'])) {
array_push($config, $server['allow_insecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
}
2020-07-01 18:31:44 +08:00
$config = array_filter($config);
$uri = implode(',', $config);
2020-06-13 18:33:42 +08:00
$uri .= "\r\n";
return $uri;
}
}