mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-24 12:07:28 +08:00
feat: new xboard
This commit is contained in:
+180
-179
@@ -2,13 +2,14 @@
|
||||
|
||||
namespace App\Protocols;
|
||||
|
||||
use App\Contracts\ProtocolInterface;
|
||||
use App\Models\ServerHysteria;
|
||||
use App\Utils\Helper;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
class ClashMeta
|
||||
class ClashMeta implements ProtocolInterface
|
||||
{
|
||||
public $flag = 'meta,verge';
|
||||
public $flags = ['meta', 'verge', 'flclash'];
|
||||
private $servers;
|
||||
private $user;
|
||||
|
||||
@@ -18,6 +19,11 @@ class ClashMeta
|
||||
$this->servers = $servers;
|
||||
}
|
||||
|
||||
public function getFlags(): array
|
||||
{
|
||||
return $this->flags;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$servers = $this->servers;
|
||||
@@ -28,15 +34,16 @@ class ClashMeta
|
||||
$customConfig = base_path() . '/resources/rules/custom.clashmeta.yaml';
|
||||
if (\File::exists($customConfig)) {
|
||||
$config = Yaml::parseFile($customConfig);
|
||||
} elseif(\File::exists($customClashConfig)) {
|
||||
} elseif (\File::exists($customClashConfig)) {
|
||||
$config = Yaml::parseFile($customClashConfig);
|
||||
} else{
|
||||
} else {
|
||||
$config = Yaml::parseFile($defaultConfig);
|
||||
}
|
||||
$proxy = [];
|
||||
$proxies = [];
|
||||
|
||||
foreach ($servers as $item) {
|
||||
$protocol_settings = $item['protocol_settings'];
|
||||
if ($item['type'] === 'shadowsocks') {
|
||||
array_push($proxy, self::buildShadowsocks($item['password'], $item));
|
||||
array_push($proxies, $item['name']);
|
||||
@@ -49,7 +56,10 @@ class ClashMeta
|
||||
array_push($proxy, self::buildTrojan($user['uuid'], $item));
|
||||
array_push($proxies, $item['name']);
|
||||
}
|
||||
if ($item['type'] === 'vless') {
|
||||
if (
|
||||
$item['type'] === 'vless'
|
||||
&& in_array(data_get($protocol_settings, 'network'), ['tcp', 'ws', 'grpc', 'http', 'h2'])
|
||||
) {
|
||||
array_push($proxy, self::buildVless($user['uuid'], $item));
|
||||
array_push($proxies, $item['name']);
|
||||
}
|
||||
@@ -61,23 +71,27 @@ class ClashMeta
|
||||
|
||||
$config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
|
||||
foreach ($config['proxy-groups'] as $k => $v) {
|
||||
if (!is_array($config['proxy-groups'][$k]['proxies'])) $config['proxy-groups'][$k]['proxies'] = [];
|
||||
if (!is_array($config['proxy-groups'][$k]['proxies']))
|
||||
$config['proxy-groups'][$k]['proxies'] = [];
|
||||
$isFilter = false;
|
||||
foreach ($config['proxy-groups'][$k]['proxies'] as $src) {
|
||||
foreach ($proxies as $dst) {
|
||||
if (!$this->isRegex($src)) continue;
|
||||
if (!$this->isRegex($src))
|
||||
continue;
|
||||
$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);
|
||||
}
|
||||
}
|
||||
if ($isFilter) continue;
|
||||
if ($isFilter)
|
||||
continue;
|
||||
}
|
||||
if ($isFilter) continue;
|
||||
if ($isFilter)
|
||||
continue;
|
||||
$config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
|
||||
}
|
||||
$config['proxy-groups'] = array_filter($config['proxy-groups'], function($group) {
|
||||
$config['proxy-groups'] = array_filter($config['proxy-groups'], function ($group) {
|
||||
return $group['proxies'];
|
||||
});
|
||||
$config['proxy-groups'] = array_values($config['proxy-groups']);
|
||||
@@ -102,10 +116,10 @@ class ClashMeta
|
||||
array_unshift($config['rules'], "DOMAIN,{$subsDomain},DIRECT");
|
||||
}
|
||||
// Force the nodes ip to be a direct rule
|
||||
collect($this->servers)->pluck('host')->map(function($host){
|
||||
collect($this->servers)->pluck('host')->map(function ($host) {
|
||||
$host = trim($host);
|
||||
return filter_var($host, FILTER_VALIDATE_IP) ? [$host] : Helper::getIpByDomainName($host);
|
||||
})->flatten()->unique()->each(function($nodeIP) use ( &$config ) {
|
||||
})->flatten()->unique()->each(function ($nodeIP) use (&$config) {
|
||||
array_unshift($config['rules'], "IP-CIDR,{$nodeIP}/32,DIRECT,no-resolve");
|
||||
});
|
||||
|
||||
@@ -119,144 +133,114 @@ class ClashMeta
|
||||
$array['type'] = 'ss';
|
||||
$array['server'] = $server['host'];
|
||||
$array['port'] = $server['port'];
|
||||
$array['cipher'] = $server['cipher'];
|
||||
$array['password'] = $password;
|
||||
$array['cipher'] = data_get($server['protocol_settings'], 'cipher');
|
||||
$array['password'] = data_get($server, 'password', $password);
|
||||
$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;
|
||||
$array['alterId'] = 0;
|
||||
$array['cipher'] = 'auto';
|
||||
$array['udp'] = true;
|
||||
$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
|
||||
];
|
||||
|
||||
if ($server['tls']) {
|
||||
if (data_get($protocol_settings, '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'];
|
||||
}
|
||||
$array['skip-cert-verify'] = (bool) data_get($protocol_settings, 'tls_settings.allow_insecure', false);
|
||||
$array['servername'] = data_get($protocol_settings, 'tls_settings.server_name');
|
||||
}
|
||||
if ($server['network'] === 'tcp') {
|
||||
$tcpSettings = $server['networkSettings'];
|
||||
if (isset($tcpSettings['header']['type'])) $array['network'] = $tcpSettings['header']['type'];
|
||||
|
||||
if (isset($tcpSettings['header']['request']['headers'])){
|
||||
$headers = $$tcpSettings['header']['request']['headers'];
|
||||
$array['http-opts']['headers'] = $headers;
|
||||
}
|
||||
if (isset($tcpSettings['header']['request']['path'][0])){
|
||||
$paths = $tcpSettings['header']['request']['path'];
|
||||
$array['http-opts']['path'] = $paths;
|
||||
}
|
||||
}
|
||||
if ($server['network'] === 'ws') {
|
||||
$array['network'] = 'ws';
|
||||
if ($server['networkSettings']) {
|
||||
$wsSettings = $server['networkSettings'];
|
||||
$array['ws-opts'] = [];
|
||||
if (isset($wsSettings['path']) && !empty($wsSettings['path']))
|
||||
$array['ws-opts']['path'] = $wsSettings['path'];
|
||||
if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
|
||||
$array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']];
|
||||
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']];
|
||||
}
|
||||
}
|
||||
if ($server['network'] === 'grpc') {
|
||||
$array['network'] = 'grpc';
|
||||
if ($server['networkSettings']) {
|
||||
$grpcSettings = $server['networkSettings'];
|
||||
$array['grpc-opts'] = [];
|
||||
if (isset($grpcSettings['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName'];
|
||||
}
|
||||
switch (data_get($protocol_settings, 'network')) {
|
||||
case 'tcp':
|
||||
$array['network'] = data_get($protocol_settings, 'network_settings.header.type', 'tcp');
|
||||
if (data_get($protocol_settings, 'network_settings.header.type', 'none') !== 'none') {
|
||||
$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', ['/']))
|
||||
];
|
||||
}
|
||||
break;
|
||||
case 'ws':
|
||||
$array['network'] = 'ws';
|
||||
if ($path = data_get($protocol_settings, 'network_settings.path'))
|
||||
$array['ws-opts']['path'] = $path;
|
||||
if ($host = data_get($protocol_settings, 'network_settings.headers.Host'))
|
||||
$array['ws-opts']['headers'] = ['Host' => $host];
|
||||
break;
|
||||
case 'grpc':
|
||||
$array['network'] = 'grpc';
|
||||
if ($serviceName = data_get($protocol_settings, 'network_settings.serviceName'))
|
||||
$array['grpc-opts']['grpc-service-name'] = $serviceName;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function buildVless($password, $server){
|
||||
$array = [];
|
||||
$array['name'] = $server['name'];
|
||||
$array['type'] = 'vless';
|
||||
$array['server'] = $server['host'];
|
||||
$array['port'] = $server['port'];
|
||||
$array['uuid'] = $password;
|
||||
$array['alterId'] = 0;
|
||||
$array['cipher'] = 'auto';
|
||||
$array['udp'] = true;
|
||||
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,
|
||||
'flow' => data_get($protocol_settings, 'flow'),
|
||||
'tls' => false
|
||||
];
|
||||
|
||||
// XTLS流控算法
|
||||
if($server['flow']) ($array['flow'] = $server['flow']);
|
||||
|
||||
if ($server['tls']) {
|
||||
switch($server['tls']){
|
||||
case 1: //开启TLS
|
||||
$array['tls'] = true;
|
||||
if ($server['tls_settings']) {
|
||||
$tlsSettings = $server['tls_settings'];
|
||||
if (isset($tlsSettings['allow_insecure']) && !empty($tlsSettings['allow_insecure']))
|
||||
$array['skip-cert-verify'] = ($tlsSettings['allow_insecure'] ? true : false);
|
||||
if (isset($tlsSettings['server_name']) && !empty($tlsSettings['server_name']))
|
||||
$array['servername'] = $tlsSettings['server_name'];
|
||||
}
|
||||
break;
|
||||
case 2: //开启reality
|
||||
$array['tls'] = true;
|
||||
$tls_settings = $server['tls_settings'];
|
||||
if (!empty($tls_settings['allowInsecure'])) $array['skip-cert-verify'] = (bool)$tls_settings['allowInsecure'];
|
||||
|
||||
if(($tls_settings['public_key'] ?? null)
|
||||
&& ($tls_settings['short_id'] ?? null)
|
||||
&& ($tls_settings['server_name'] ?? null)){
|
||||
$array['servername'] = $tls_settings['server_name'];
|
||||
$array['reality-opts'] = [
|
||||
'public-key' => $tls_settings['public_key'],
|
||||
'short-id' => $tls_settings['short_id']
|
||||
];
|
||||
$fingerprints = ['chrome', 'firefox', 'safari', 'ios', 'edge', 'qq']; //随机客户端指纹
|
||||
$array['client-fingerprint'] = $fingerprints[rand(0,count($fingerprints) - 1)];
|
||||
};
|
||||
break;
|
||||
}
|
||||
switch (data_get($protocol_settings, 'tls')) {
|
||||
case 1:
|
||||
$array['tls'] = true;
|
||||
$array['skip-cert-verify'] = (bool) data_get($protocol_settings, 'tls_settings.allow_insecure', false);
|
||||
if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) {
|
||||
$array['servername'] = $serverName;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
$array['tls'] = true;
|
||||
$array['skip-cert-verify'] = (bool) data_get($protocol_settings, 'reality_settings.allow_insecure', false);
|
||||
$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;
|
||||
}
|
||||
|
||||
if ($server['network'] === 'ws') {
|
||||
$array['network'] = 'ws';
|
||||
if ($server['network_settings']) {
|
||||
$wsSettings = $server['network_settings'];
|
||||
$array['ws-opts'] = [];
|
||||
if (isset($wsSettings['path']) && !empty($wsSettings['path']))
|
||||
$array['ws-opts']['path'] = $wsSettings['path'];
|
||||
if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
|
||||
$array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']];
|
||||
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']];
|
||||
}
|
||||
}
|
||||
if ($server['network'] === 'grpc') {
|
||||
$array['network'] = 'grpc';
|
||||
if ($server['network_settings']) {
|
||||
$grpcSettings = $server['network_settings'];
|
||||
$array['grpc-opts'] = [];
|
||||
if (isset($grpcSettings['serviceName'])) {
|
||||
$array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName'];
|
||||
};
|
||||
}
|
||||
switch (data_get($protocol_settings, 'network')) {
|
||||
case 'ws':
|
||||
$array['network'] = 'ws';
|
||||
if ($path = data_get($protocol_settings, 'network_settings.path'))
|
||||
$array['ws-opts']['path'] = $path;
|
||||
if ($host = data_get($protocol_settings, 'network_settings.headers.Host'))
|
||||
$array['ws-opts']['headers'] = ['Host' => $host];
|
||||
break;
|
||||
case 'grpc':
|
||||
$array['network'] = 'grpc';
|
||||
if ($serviceName = data_get($protocol_settings, 'network_settings.serviceName'))
|
||||
$array['grpc-opts']['grpc-service-name'] = $serviceName;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return $array;
|
||||
@@ -264,66 +248,80 @@ class ClashMeta
|
||||
|
||||
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);
|
||||
// trojan-go配置
|
||||
if(in_array($server['network'], ["grpc", "ws"])){
|
||||
$array['network'] = $server['network'];
|
||||
// grpc配置
|
||||
if($server['network'] === "grpc" && isset($server['networkSettings']['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $server['networkSettings']['serviceName'];
|
||||
// ws配置
|
||||
if($server['network'] === "ws") {
|
||||
if(isset($server['networkSettings']['path'])) {
|
||||
$array['ws-opts']['path'] = $server['networkSettings']['path'];
|
||||
}
|
||||
if(isset($server['networkSettings']['headers']['Host'])){
|
||||
$array['ws-opts']['headers']['Host'] = $server['networkSettings']['headers']['Host'];
|
||||
}
|
||||
}
|
||||
};
|
||||
$protocol_settings = data_get($server, 'protocol_settings', []);
|
||||
$array = [
|
||||
'name' => $server['name'],
|
||||
'type' => 'trojan',
|
||||
'server' => $server['host'],
|
||||
'port' => $server['port'],
|
||||
'password' => $password,
|
||||
'udp' => true,
|
||||
'skip-cert-verify' => (bool) data_get($protocol_settings, 'allow_insecure', false)
|
||||
];
|
||||
if ($serverName = data_get($protocol_settings, 'server_name')) {
|
||||
$array['sni'] = $serverName;
|
||||
}
|
||||
|
||||
switch (data_get($protocol_settings, 'network')) {
|
||||
case 'tcp':
|
||||
$array['network'] = 'tcp';
|
||||
break;
|
||||
case 'ws':
|
||||
$array['network'] = 'ws';
|
||||
if ($path = data_get($protocol_settings, 'network_settings.path'))
|
||||
$array['ws-opts']['path'] = $path;
|
||||
if ($host = data_get($protocol_settings, 'network_settings.headers.Host'))
|
||||
$array['ws-opts']['headers'] = ['Host' => $host];
|
||||
break;
|
||||
case 'grpc':
|
||||
$array['network'] = 'grpc';
|
||||
if ($serviceName = data_get($protocol_settings, 'network_settings.serviceName'))
|
||||
$array['grpc-opts']['grpc-service-name'] = $serviceName;
|
||||
break;
|
||||
default:
|
||||
$array['network'] = 'tcp';
|
||||
break;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function buildHysteria($password, $server, $user)
|
||||
{
|
||||
$array = [];
|
||||
$array['name'] = $server['name'];
|
||||
$array['server'] = $server['host'];
|
||||
$array['port'] = $server['port'];
|
||||
if($server['server_name']) $array['sni'] = $server['server_name'];
|
||||
$array['up'] = $user->speed_limit ? min($server['up_mbps'], $user->speed_limit) : $server['up_mbps'];
|
||||
$array['down'] = $user->speed_limit ? min($server['down_mbps'], $user->speed_limit) : $server['down_mbps'];
|
||||
$array['skip-cert-verify'] = $server['insecure'] ? true : false;
|
||||
switch($server['version']){
|
||||
case 1:
|
||||
$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'),
|
||||
'skip-cert-verify' => (bool) data_get($protocol_settings, 'tls.allow_insecure', false),
|
||||
];
|
||||
if (isset($server['ports'])) {
|
||||
$array['ports'] = $server['ports'];
|
||||
}
|
||||
switch (data_get($protocol_settings, 'version')) {
|
||||
case 1:
|
||||
$array['type'] = 'hysteria';
|
||||
// 判断是否开启动态端口
|
||||
if(isset($server['ports'])) $array['ports'] = $server['ports'];
|
||||
$array['auth_str'] = $password;
|
||||
$array['protocol'] = 'udp';
|
||||
if($server['is_obfs']) $array['obfs'] = $server['server_key'];
|
||||
$array['protocol'] = 'udp'; // 支持 udp/wechat-video/faketcp
|
||||
if (data_get($protocol_settings, 'obfs.open')) {
|
||||
$array['obfs'] = data_get($protocol_settings, 'obfs.password');
|
||||
}
|
||||
$array['fast-open'] = true;
|
||||
$array['disable_mtu_discovery'] = true; //禁止路径最大传输单元发现
|
||||
$array['alpn'] = [ServerHysteria::$alpnMap[$server['alpn']]];
|
||||
$array['disable_mtu_discovery'] = true;
|
||||
break;
|
||||
case 2:
|
||||
case 2:
|
||||
$array['type'] = 'hysteria2';
|
||||
$array['password'] = $password;
|
||||
if($server['is_obfs']) {
|
||||
$array['obfs'] = 'salamander';
|
||||
$array['obfs-password'] = $server['server_key'];
|
||||
if (data_get($protocol_settings, 'obfs.open')) {
|
||||
$array['obfs'] = data_get($protocol_settings, 'obfs.type');
|
||||
$array['obfs-password'] = data_get($protocol_settings, 'obfs.password');
|
||||
}
|
||||
if(isset($server['ports'])) $array['ports'] = $server['ports'];
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
@@ -334,6 +332,9 @@ class ClashMeta
|
||||
|
||||
private function isRegex($exp)
|
||||
{
|
||||
return @preg_match($exp, null) !== false;
|
||||
if (empty($exp)) {
|
||||
return false;
|
||||
}
|
||||
return @preg_match($exp, '') !== false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user