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

133 lines
4.9 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;
$app_name = 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: filename={$app_name}");
$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) {
if (!is_array($config['proxy-groups'][$k]['proxies'])) continue;
$config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
2021-07-02 22:34:42 +08:00
}
$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;
2020-11-17 23:02:12 +08:00
$array['alterId'] = $server['alter_id'];
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'];
2020-11-20 00:26:39 +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']) {
2021-08-22 00:09:37 +08:00
$grpcObject = $server['networkSettings'];
$array['grpc-opts'] = [];
$array['grpc-opts']['grpc-service-name'] = $grpcObject['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;
}
}