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

185 lines
7.1 KiB
PHP
Raw Normal View History

2020-06-12 00:18:35 +08:00
<?php
2021-07-02 22:34:42 +08:00
namespace App\Http\Controllers\Client\Protocols;
2020-06-12 00:18:35 +08:00
2022-11-20 15:24:03 +08:00
use App\Utils\Dict;
use phpDocumentor\Reflection\Types\Self_;
2021-07-02 22:34:42 +08:00
use Symfony\Component\Yaml\Yaml;
2020-06-12 00:18:35 +08:00
class Clash
{
2021-07-02 22:34:42 +08:00
public $flag = 'clash';
private $servers;
private $user;
public function __construct($user, $servers)
{
$this->user = $user;
$this->servers = $servers;
}
public function handle()
{
$servers = $this->servers;
2021-07-02 22:34:42 +08:00
$user = $this->user;
2021-08-29 21:44:51 +08:00
$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');
header("content-disposition:attachment;filename*=UTF-8''".rawurlencode($appName));
2022-10-01 03:20:05 +08:00
header("profile-web-page-url:" . config('v2board.app_url'));
$defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
$customConfig = base_path() . '/resources/rules/custom.clash.yaml';
if (\File::exists($customConfig)) {
$config = Yaml::parseFile($customConfig);
2021-08-20 00:54:00 +08:00
} else {
$config = Yaml::parseFile($defaultConfig);
}
2023-03-02 23:00:58 +08:00
$this->patch($config);
$proxy = [];
$proxies = [];
2021-08-20 00:54:00 +08:00
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
) {
array_push($proxy, self::buildShadowsocks($user['uuid'], $item));
array_push($proxies, $item['name']);
2021-07-02 22:34:42 +08:00
}
2023-02-15 14:53:13 +08:00
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']);
}
}
2021-07-02 22:34:42 +08:00
$config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
foreach ($config['proxy-groups'] as $k => $v) {
2022-02-13 20:48:49 +08:00
if (!is_array($config['proxy-groups'][$k]['proxies'])) $config['proxy-groups'][$k]['proxies'] = [];
2021-12-13 14:26:05 +08:00
$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 20:33:21 +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-13 14:26:05 +08:00
}
}
2022-01-11 20:33:21 +08:00
if ($isFilter) continue;
2021-12-13 14:26:05 +08:00
}
2022-01-11 20:33:21 +08:00
if ($isFilter) continue;
$config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
2021-07-02 22:34:42 +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");
}
2023-02-11 15:48:24 +08:00
$yaml = Yaml::dump($config, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
$yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
2021-07-02 22:34:42 +08:00
return $yaml;
}
2020-10-04 14:21:09 +08:00
public static function buildShadowsocks($uuid, $server)
{
$array = [];
2020-11-15 17:10:32 +08:00
$array['name'] = $server['name'];
2020-10-04 14:21:09 +08:00
$array['type'] = 'ss';
2020-11-15 17:10:32 +08:00
$array['server'] = $server['host'];
$array['port'] = $server['port'];
$array['cipher'] = $server['cipher'];
2020-10-04 14:21:09 +08:00
$array['password'] = $uuid;
$array['udp'] = true;
return $array;
}
2020-06-12 00:18:35 +08:00
public static function buildVmess($uuid, $server)
{
$array = [];
2020-11-15 17:10:32 +08:00
$array['name'] = $server['name'];
2020-06-12 00:18:35 +08:00
$array['type'] = 'vmess';
2020-11-15 17:10:32 +08:00
$array['server'] = $server['host'];
$array['port'] = $server['port'];
2020-06-12 00:18:35 +08:00
$array['uuid'] = $uuid;
2022-01-04 13:40:35 +08:00
$array['alterId'] = 0;
2020-06-12 00:18:35 +08:00
$array['cipher'] = 'auto';
2020-07-12 11:19:38 +08:00
$array['udp'] = true;
2020-11-20 00:26:39 +08:00
2020-11-15 17:10:32 +08:00
if ($server['tls']) {
2020-06-12 00:18:35 +08:00
$array['tls'] = true;
2021-07-02 21:07:54 +08:00
if ($server['tlsSettings']) {
2021-08-22 00:09:37 +08:00
$tlsSettings = $server['tlsSettings'];
2020-11-20 00:26:39 +08:00
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'];
}
2020-06-12 00:18:35 +08:00
}
2020-11-20 00:26:39 +08:00
if ($server['network'] === 'ws') {
2020-11-20 00:53:53 +08:00
$array['network'] = 'ws';
2021-07-02 21:07:54 +08:00
if ($server['networkSettings']) {
2021-08-22 00:09:37 +08:00
$wsSettings = $server['networkSettings'];
$array['ws-opts'] = [];
2020-11-20 00:26:39 +08:00
if (isset($wsSettings['path']) && !empty($wsSettings['path']))
$array['ws-opts']['path'] = $wsSettings['path'];
2020-11-20 00:26:39 +08:00
if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
$array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']];
2022-01-10 01:52:23 +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']];
2020-06-12 00:18:35 +08:00
}
}
if ($server['network'] === 'grpc') {
$array['network'] = 'grpc';
2021-07-02 21:07:54 +08:00
if ($server['networkSettings']) {
$grpcSettings = $server['networkSettings'];
$array['grpc-opts'] = [];
2022-05-12 03:38:28 +08:00
if (isset($grpcSettings['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName'];
}
}
2020-11-20 00:26:39 +08:00
2020-06-12 00:18:35 +08:00
return $array;
}
public static function buildTrojan($password, $server)
{
$array = [];
2020-11-15 17:10:32 +08:00
$array['name'] = $server['name'];
2020-06-12 00:18:35 +08:00
$array['type'] = 'trojan';
2020-11-15 17:10:32 +08:00
$array['server'] = $server['host'];
$array['port'] = $server['port'];
2020-06-12 00:18:35 +08:00
$array['password'] = $password;
2020-07-12 11:19:38 +08:00
$array['udp'] = true;
2020-11-15 17:10:32 +08:00
if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
2020-11-20 00:26:39 +08:00
if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
2020-06-12 00:18:35 +08:00
return $array;
}
2021-12-13 14:26:05 +08:00
private function isMatch($exp, $str)
{
2022-01-11 20:33:21 +08:00
return @preg_match($exp, $str);
}
private function isRegex($exp)
{
return @preg_match($exp, null) !== false;
2021-12-13 14:26:05 +08:00
}
2023-03-02 23:00:58 +08:00
private function patch(&$config)
{
// fix clash x dns mode
preg_match('#(ClashX)[/ ]([0-9.]*)#', $_SERVER['HTTP_USER_AGENT'], $matches);
2023-03-03 14:07:16 +08:00
if (isset($matches[2]) && $matches[2] < '1.96.2') $config['dns']['enhanced-mode'] = 'redir-host';
2023-03-02 23:00:58 +08:00
}
2020-06-12 00:18:35 +08:00
}