Xboard/app/Protocols/ClashMeta.php

333 lines
13 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Protocols;
2025-01-06 12:20:11 -05:00
use App\Contracts\ProtocolInterface;
2023-11-17 01:44:01 -05:00
use App\Models\ServerHysteria;
use App\Utils\Helper;
use Symfony\Component\Yaml\Yaml;
2025-01-06 12:20:11 -05:00
class ClashMeta implements ProtocolInterface
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
public $flags = ['meta', 'verge'];
2023-11-17 01:44:01 -05:00
private $servers;
private $user;
public function __construct($user, $servers, array $options = null)
{
$this->user = $user;
$this->servers = $servers;
}
2025-01-06 12:20:11 -05:00
public function getFlags(): array
{
return $this->flags;
}
2023-11-17 01:44:01 -05:00
public function handle()
{
$servers = $this->servers;
$user = $this->user;
$appName = admin_setting('app_name', 'XBoard');
$defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
$customClashConfig = base_path() . '/resources/rules/custom.clash.yaml';
$customConfig = base_path() . '/resources/rules/custom.clashmeta.yaml';
2023-11-17 01:44:01 -05:00
if (\File::exists($customConfig)) {
$config = Yaml::parseFile($customConfig);
2025-01-06 12:20:11 -05:00
} elseif (\File::exists($customClashConfig)) {
$config = Yaml::parseFile($customClashConfig);
2025-01-06 12:20:11 -05:00
} else {
2023-11-17 01:44:01 -05:00
$config = Yaml::parseFile($defaultConfig);
}
$proxy = [];
$proxies = [];
foreach ($servers as $item) {
if ($item['type'] === 'shadowsocks') {
array_push($proxy, self::buildShadowsocks($item['password'], $item));
2023-11-17 01:44:01 -05:00
array_push($proxies, $item['name']);
}
if ($item['type'] === 'vmess') {
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']);
}
if ($item['type'] === 'vless') {
array_push($proxy, self::buildVless($user['uuid'], $item));
array_push($proxies, $item['name']);
}
if ($item['type'] === 'hysteria') {
array_push($proxy, self::buildHysteria($user['uuid'], $item, $user));
array_push($proxies, $item['name']);
}
}
$config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
foreach ($config['proxy-groups'] as $k => $v) {
2025-01-06 12:20:11 -05:00
if (!is_array($config['proxy-groups'][$k]['proxies']))
$config['proxy-groups'][$k]['proxies'] = [];
2023-11-17 01:44:01 -05:00
$isFilter = false;
foreach ($config['proxy-groups'][$k]['proxies'] as $src) {
foreach ($proxies as $dst) {
2025-01-06 12:20:11 -05:00
if (!$this->isRegex($src))
continue;
2023-11-17 01:44:01 -05:00
$isFilter = true;
$config['proxy-groups'][$k]['proxies'] = array_values(array_diff($config['proxy-groups'][$k]['proxies'], [$src]));
if ($this->isMatch($src, $dst)) {
array_push($config['proxy-groups'][$k]['proxies'], $dst);
}
}
2025-01-06 12:20:11 -05:00
if ($isFilter)
continue;
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
if ($isFilter)
continue;
2023-11-17 01:44:01 -05:00
$config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
}
2025-01-06 12:20:11 -05:00
$config['proxy-groups'] = array_filter($config['proxy-groups'], function ($group) {
2023-11-17 01:44:01 -05:00
return $group['proxies'];
});
$config['proxy-groups'] = array_values($config['proxy-groups']);
$config = $this->buildRules($config);
2023-11-17 01:44:01 -05:00
$yaml = Yaml::dump($config, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
$yaml = str_replace('$app_name', admin_setting('app_name', 'XBoard'), $yaml);
return response($yaml, 200)
->header('subscription-userinfo', "upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}")
->header('profile-update-interval', '24')
->header('content-disposition', 'attachment;filename*=UTF-8\'\'' . rawurlencode($appName));
}
/**
* Build the rules for Clash.
*/
public function buildRules($config)
{
// Force the current subscription domain to be a direct rule
$subsDomain = request()->header('Host');
if ($subsDomain) {
array_unshift($config['rules'], "DOMAIN,{$subsDomain},DIRECT");
}
// Force the nodes ip to be a direct rule
2025-01-06 12:20:11 -05:00
collect($this->servers)->pluck('host')->map(function ($host) {
$host = trim($host);
return filter_var($host, FILTER_VALIDATE_IP) ? [$host] : Helper::getIpByDomainName($host);
2025-01-06 12:20:11 -05:00
})->flatten()->unique()->each(function ($nodeIP) use (&$config) {
array_unshift($config['rules'], "IP-CIDR,{$nodeIP}/32,DIRECT,no-resolve");
});
return $config;
}
2023-11-17 01:44:01 -05:00
public static function buildShadowsocks($password, $server)
{
$array = [];
$array['name'] = $server['name'];
$array['type'] = 'ss';
$array['server'] = $server['host'];
$array['port'] = $server['port'];
2025-01-06 12:20:11 -05:00
$array['cipher'] = data_get($server['protocol_settings'], 'cipher');
2025-01-09 02:58:32 -05:00
$array['password'] = data_get($server, 'password', $password);
2023-11-17 01:44:01 -05:00
$array['udp'] = true;
return $array;
}
public static function buildVmess($uuid, $server)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = data_get($server, 'protocol_settings', []);
$array = [
'name' => $server['name'],
'type' => 'vmess',
'server' => $server['host'],
'port' => $server['port'],
'uuid' => $uuid,
'alterId' => 0,
'cipher' => 'auto',
'udp' => true
];
2023-11-17 01:44:01 -05:00
2025-01-06 12:20:11 -05:00
if (data_get($protocol_settings, 'tls')) {
2023-11-17 01:44:01 -05:00
$array['tls'] = true;
2025-01-07 22:07:13 -05:00
$array['skip-cert-verify'] = (bool) data_get($protocol_settings, 'tls_settings.allow_insecure', false);
2025-01-06 12:20:11 -05:00
$array['servername'] = data_get($protocol_settings, 'tls_settings.server_name');
2023-11-17 01:44:01 -05:00
}
2023-12-17 20:40:35 -05:00
2025-01-06 12:20:11 -05:00
switch (data_get($protocol_settings, 'network')) {
case 'tcp':
$array['network'] = data_get($protocol_settings, 'network_settings.header.type', 'tcp');
$array['http-opts'] = [
'headers' => data_get($protocol_settings, 'network_settings.header.request.headers'),
'path' => \Arr::random(data_get($protocol_settings, 'network_settings.header.request.path', ['/']), 1)
];
break;
case 'ws':
$array['network'] = 'ws';
$array['ws-opts'] = [
'path' => data_get($protocol_settings, 'network_settings.path'),
'headers' => ['Host' => data_get($protocol_settings, 'network_settings.headers.Host')]
];
break;
case 'grpc':
$array['network'] = 'grpc';
$array['grpc-opts'] = [
'grpc-service-name' => data_get($protocol_settings, 'network_settings.serviceName')
];
break;
default:
break;
2023-11-17 01:44:01 -05:00
}
return $array;
}
2025-01-06 12:20:11 -05:00
public static function buildVless($password, $server)
{
$protocol_settings = data_get($server, 'protocol_settings', []);
$array = [
'name' => $server['name'],
'type' => 'vless',
'server' => $server['host'],
'port' => $server['port'],
'uuid' => $password,
'alterId' => 0,
'cipher' => 'auto',
'udp' => true,
2025-01-07 22:07:13 -05:00
'flow' => data_get($protocol_settings, 'flow')
2025-01-06 12:20:11 -05:00
];
2023-11-17 01:44:01 -05:00
2025-01-06 12:20:11 -05:00
switch (data_get($protocol_settings, 'tls')) {
case 1:
$array['tls'] = true;
2025-01-07 22:07:13 -05:00
$array['skip-cert-verify'] = (bool) data_get($protocol_settings, 'tls_settings.allow_insecure', false);
2025-01-06 12:20:11 -05:00
$array['servername'] = data_get($protocol_settings, 'tls_settings.server_name');
break;
case 2:
$array['tls'] = true;
2025-01-07 22:07:13 -05:00
$array['skip-cert-verify'] = (bool) data_get($protocol_settings, 'reality_settings.allow_insecure', false);
2025-01-06 12:20:11 -05:00
$array['servername'] = data_get($protocol_settings, 'reality_settings.server_name');
$array['reality-opts'] = [
'public-key' => data_get($protocol_settings, 'reality_settings.public_key'),
'short-id' => data_get($protocol_settings, 'reality_settings.short_id')
];
$array['client-fingerprint'] = Helper::getRandFingerprint();
break;
default:
break;
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
switch (data_get($protocol_settings, 'network')) {
case 'ws':
$array['network'] = 'ws';
$array['ws-opts'] = [
'path' => data_get($protocol_settings, 'network_settings.path'),
'headers' => ['Host' => data_get($protocol_settings, 'network_settings.headers.Host')]
];
break;
case 'grpc':
$array['network'] = 'grpc';
$array['grpc-opts'] = [
'grpc-service-name' => data_get($protocol_settings, 'network_settings.serviceName')
];
break;
case 'h2':
$array['network'] = 'h2';
$array['h2-opts'] = [
'path' => data_get($protocol_settings, 'network_settings.path', '/'),
'host' => data_get($protocol_settings, 'network_settings.host')
];
break;
default:
break;
2023-11-17 01:44:01 -05:00
}
return $array;
}
public static function buildTrojan($password, $server)
{
2025-01-06 12:20:11 -05:00
$settings = data_get($server, 'protocol_settings', []);
$array = [
'name' => $server['name'],
'type' => 'trojan',
'server' => $server['host'],
'port' => $server['port'],
'password' => $password,
'udp' => true,
'sni' => data_get($settings, 'server_name'),
2025-01-07 22:07:13 -05:00
'skip-cert-verify' => (bool) data_get($settings, 'allow_insecure', false)
2025-01-06 12:20:11 -05:00
];
switch (data_get($settings, 'network')) {
case 'grpc':
$array['network'] = 'grpc';
$array['grpc-opts'] = [
'grpc-service-name' => data_get($settings, 'network_settings.serviceName')
];
break;
case 'ws':
$array['network'] = 'ws';
$array['ws-opts'] = [
'path' => data_get($settings, 'network_settings.path'),
'headers' => ['Host' => data_get($settings, 'network_settings.headers.Host')]
];
break;
default:
break;
}
2023-11-17 01:44:01 -05:00
return $array;
}
public static function buildHysteria($password, $server, $user)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = data_get($server, 'protocol_settings', []);
$array = [
'name' => $server['name'],
'server' => $server['host'],
'port' => $server['port'],
'sni' => data_get($protocol_settings, 'tls.server_name'),
'up' => data_get($protocol_settings, 'bandwidth.up'),
'down' => data_get($protocol_settings, 'bandwidth.down'),
2025-01-07 22:07:13 -05:00
'skip-cert-verify' => (bool) data_get($protocol_settings, 'tls.allow_insecure', false),
2025-01-06 12:20:11 -05:00
];
if (isset($server['ports'])) {
$array['ports'] = $server['ports'];
}
switch (data_get($protocol_settings, 'version')) {
case 1:
2023-11-17 01:44:01 -05:00
$array['type'] = 'hysteria';
$array['auth_str'] = $password;
2025-01-06 12:20:11 -05:00
$array['protocol'] = 'udp'; // 支持 udp/wechat-video/faketcp
if (data_get($protocol_settings, 'obfs.open')) {
$array['obfs'] = data_get($protocol_settings, 'obfs.password');
}
2023-11-17 01:44:01 -05:00
$array['fast-open'] = true;
2025-01-06 12:20:11 -05:00
$array['disable_mtu_discovery'] = true;
2023-11-17 01:44:01 -05:00
break;
2025-01-06 12:20:11 -05:00
case 2:
2023-11-17 01:44:01 -05:00
$array['type'] = 'hysteria2';
$array['password'] = $password;
2025-01-06 12:20:11 -05:00
if (data_get($protocol_settings, 'obfs.open')) {
$array['obfs'] = data_get($protocol_settings, 'obfs.type');
$array['obfs-password'] = data_get($protocol_settings, 'obfs.password');
2023-11-17 01:44:01 -05:00
}
break;
}
2025-01-06 12:20:11 -05:00
2023-11-17 01:44:01 -05:00
return $array;
}
private function isMatch($exp, $str)
{
return @preg_match($exp, $str);
}
private function isRegex($exp)
{
return @preg_match($exp, null) !== false;
}
}