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

166 lines
6.4 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
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));
$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);
}
$proxy = [];
$proxies = [];
2021-08-20 00:54:00 +08:00
foreach ($servers as $item) {
if ($item['type'] === 'shadowsocks') {
array_push($proxy, self::buildShadowsocks($user['uuid'], $item));
array_push($proxies, $item['name']);
2021-07-02 22:34:42 +08:00
}
if ($item['type'] === 'v2ray') {
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
$subsDomain = $_SERVER['SERVER_NAME'];
$subsDomainRule = "DOMAIN,{$subsDomain},DIRECT";
array_unshift($config['rules'], $subsDomainRule);
$yaml = Yaml::dump($config);
$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
}
2020-06-12 00:18:35 +08:00
}