2021-12-27 00:05:28 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Client\Protocols;
|
|
|
|
|
|
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
|
|
|
|
class Stash
|
|
|
|
|
{
|
|
|
|
|
public $flag = 'stash';
|
|
|
|
|
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("subscription-userinfo: upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}");
|
|
|
|
|
header('profile-update-interval: 24');
|
2022-05-19 23:01:11 +08:00
|
|
|
|
header("content-disposition: filename*=UTF-8''".rawurlencode($appName));
|
2021-12-27 00:05:28 +08:00
|
|
|
|
// 暂时使用clash配置文件,后续根据Stash更新情况更新
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
$proxy = [];
|
|
|
|
|
$proxies = [];
|
|
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
])
|
|
|
|
|
) {
|
2021-12-27 00:05:28 +08:00
|
|
|
|
array_push($proxy, self::buildShadowsocks($user['uuid'], $item));
|
|
|
|
|
array_push($proxies, $item['name']);
|
|
|
|
|
}
|
2023-02-15 14:53:13 +08:00
|
|
|
|
if ($item['type'] === 'vmess') {
|
2021-12-27 00:05:28 +08:00
|
|
|
|
array_push($proxy, self::buildVmess($user['uuid'], $item));
|
|
|
|
|
array_push($proxies, $item['name']);
|
|
|
|
|
}
|
|
|
|
|
if ($item['type'] === 'trojan') {
|
|
|
|
|
array_push($proxy, self::buildTrojan($user['uuid'], $item));
|
|
|
|
|
array_push($proxies, $item['name']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
|
|
|
|
|
foreach ($config['proxy-groups'] as $k => $v) {
|
|
|
|
|
if (!is_array($config['proxy-groups'][$k]['proxies'])) continue;
|
|
|
|
|
$isFilter = false;
|
2021-12-27 00:13:27 +08:00
|
|
|
|
foreach ($config['proxy-groups'][$k]['proxies'] as $src) {
|
|
|
|
|
foreach ($proxies as $dst) {
|
2022-01-11 23:39:17 +08:00
|
|
|
|
if (!$this->isRegex($src)) continue;
|
|
|
|
|
$isFilter = true;
|
|
|
|
|
$config['proxy-groups'][$k]['proxies'] = array_values(array_diff($config['proxy-groups'][$k]['proxies'], [$src]));
|
2021-12-27 00:13:27 +08:00
|
|
|
|
if ($this->isMatch($src, $dst)) {
|
|
|
|
|
array_push($config['proxy-groups'][$k]['proxies'], $dst);
|
2021-12-27 00:05:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-11 23:39:17 +08:00
|
|
|
|
if ($isFilter) continue;
|
2021-12-27 00:05:28 +08:00
|
|
|
|
}
|
2022-01-11 23:39:17 +08:00
|
|
|
|
if ($isFilter) continue;
|
|
|
|
|
$config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
|
2021-12-27 00:05:28 +08:00
|
|
|
|
}
|
|
|
|
|
// Force the current subscription domain to be a direct rule
|
2023-01-20 23:34:19 +08:00
|
|
|
|
$subsDomain = $_SERVER['HTTP_HOST'];
|
|
|
|
|
if ($subsDomain) {
|
|
|
|
|
array_unshift($config['rules'], "DOMAIN,{$subsDomain},DIRECT");
|
|
|
|
|
}
|
2021-12-27 00:05:28 +08:00
|
|
|
|
|
2023-02-11 15:48:24 +08:00
|
|
|
|
$yaml = Yaml::dump($config, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
|
2021-12-27 00:05:28 +08:00
|
|
|
|
$yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
|
|
|
|
|
return $yaml;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function buildShadowsocks($uuid, $server)
|
|
|
|
|
{
|
|
|
|
|
$array = [];
|
|
|
|
|
$array['name'] = $server['name'];
|
|
|
|
|
$array['type'] = 'ss';
|
|
|
|
|
$array['server'] = $server['host'];
|
|
|
|
|
$array['port'] = $server['port'];
|
|
|
|
|
$array['cipher'] = $server['cipher'];
|
|
|
|
|
$array['password'] = $uuid;
|
|
|
|
|
$array['udp'] = true;
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function buildVmess($uuid, $server)
|
|
|
|
|
{
|
|
|
|
|
$array = [];
|
|
|
|
|
$array['name'] = $server['name'];
|
|
|
|
|
$array['type'] = 'vmess';
|
|
|
|
|
$array['server'] = $server['host'];
|
|
|
|
|
$array['port'] = $server['port'];
|
|
|
|
|
$array['uuid'] = $uuid;
|
2022-01-04 13:40:35 +08:00
|
|
|
|
$array['alterId'] = 0;
|
2021-12-27 00:05:28 +08:00
|
|
|
|
$array['cipher'] = 'auto';
|
|
|
|
|
$array['udp'] = true;
|
|
|
|
|
|
|
|
|
|
if ($server['tls']) {
|
|
|
|
|
$array['tls'] = true;
|
|
|
|
|
if ($server['tlsSettings']) {
|
|
|
|
|
$tlsSettings = $server['tlsSettings'];
|
|
|
|
|
if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
|
|
|
|
|
$array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
|
|
|
|
|
if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
|
|
|
|
|
$array['servername'] = $tlsSettings['serverName'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($server['network'] === 'ws') {
|
|
|
|
|
$array['network'] = 'ws';
|
|
|
|
|
if ($server['networkSettings']) {
|
|
|
|
|
$wsSettings = $server['networkSettings'];
|
2022-01-03 14:44:13 +08:00
|
|
|
|
$array['ws-opts'] = [];
|
2021-12-27 00:05:28 +08:00
|
|
|
|
if (isset($wsSettings['path']) && !empty($wsSettings['path']))
|
2022-01-03 14:44:13 +08:00
|
|
|
|
$array['ws-opts']['path'] = $wsSettings['path'];
|
2021-12-27 00:05:28 +08:00
|
|
|
|
if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
|
2022-01-03 14:44:13 +08:00
|
|
|
|
$array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']];
|
2022-01-10 01:53:14 +08:00
|
|
|
|
if (isset($wsSettings['path']) && !empty($wsSettings['path']))
|
|
|
|
|
$array['ws-path'] = $wsSettings['path'];
|
|
|
|
|
if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
|
|
|
|
|
$array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
|
2021-12-27 00:05:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($server['network'] === 'grpc') {
|
|
|
|
|
$array['network'] = 'grpc';
|
|
|
|
|
if ($server['networkSettings']) {
|
2022-01-03 14:44:13 +08:00
|
|
|
|
$grpcSettings = $server['networkSettings'];
|
2021-12-27 00:05:28 +08:00
|
|
|
|
$array['grpc-opts'] = [];
|
2022-05-12 03:38:28 +08:00
|
|
|
|
if (isset($grpcSettings['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName'];
|
2021-12-27 00:05:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function buildTrojan($password, $server)
|
|
|
|
|
{
|
|
|
|
|
$array = [];
|
|
|
|
|
$array['name'] = $server['name'];
|
|
|
|
|
$array['type'] = 'trojan';
|
|
|
|
|
$array['server'] = $server['host'];
|
|
|
|
|
$array['port'] = $server['port'];
|
|
|
|
|
$array['password'] = $password;
|
|
|
|
|
$array['udp'] = true;
|
|
|
|
|
if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
|
|
|
|
|
if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 15:11:08 +08:00
|
|
|
|
private function isRegex($exp)
|
|
|
|
|
{
|
|
|
|
|
return @preg_match($exp, null) !== false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 00:05:28 +08:00
|
|
|
|
private function isMatch($exp, $str)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return preg_match($exp, $str);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|