| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace App\Utils\Clients\Formatters;
- use App\Utils\Library\Templates\Formatter;
- class Clash implements Formatter
- {
- // https://clash.wiki/configuration/outbound.html
- public static function build(array $servers): array
- {
- $validTypes = ['shadowsocks', 'shadowsocksr', 'vmess', 'trojan'];
- foreach ($servers as $server) {
- if (in_array($server['type'], $validTypes, true)) {
- $proxy = call_user_func([self::class, 'build'.ucfirst($server['type'])], $server);
- if ($proxy) {
- $ids[] = $server['id'];
- $proxies[] = $proxy;
- }
- }
- }
- return ['ids' => $ids ?? [], 'proxies' => $proxies ?? []];
- }
- public static function buildShadowsocks(array $server): ?array
- {
- $supportedMethods = ['aes-128-gcm', 'aes-192-gcm', 'aes-256-gcm', 'chacha20-ietf-poly1305', 'xchacha20-ietf-poly1305', 'aes-128-cfb', 'aes-192-cfb', 'aes-256-cfb', 'rc4-md5', 'chacha20-ietf', 'xchacha20', 'aes-128-ctr', 'aes-192-ctr', 'aes-256-ctr'];
- if (! in_array($server['method'], $supportedMethods, true)) {
- return null;
- }
- return [
- 'name' => $server['name'],
- 'type' => 'ss',
- 'server' => $server['host'],
- 'port' => $server['port'],
- 'cipher' => $server['method'],
- 'password' => $server['passwd'],
- 'udp' => $server['udp'],
- ];
- }
- public static function buildShadowsocksr(array $server): ?array
- {
- $supportedMethods = ['aes-128-cfb', 'aes-192-cfb', 'aes-256-cfb', 'rc4-md5', 'chacha20-ietf', 'xchacha20', 'none'];
- $supportedObfuscations = ['plain', 'http_simple', 'http_post', 'random_head', 'tls1.2_ticket_auth', 'tls1.2_ticket_fastauth'];
- $supportedProtocols = ['origin', 'auth_sha1_v4', 'auth_aes128_md5', 'auth_aes128_sha1', 'auth_chain_a', 'auth_chain_b'];
- if (! in_array($server['method'], $supportedMethods, true) || ! in_array($server['obfs'], $supportedObfuscations, true) || ! in_array($server['protocol'], $supportedProtocols, true)) {
- return null;
- }
- return [
- 'name' => $server['name'],
- 'type' => 'ssr',
- 'server' => $server['host'],
- 'port' => $server['port'],
- 'password' => $server['passwd'],
- 'cipher' => $server['method'],
- 'obfs' => $server['obfs'],
- 'obfs-param' => $server['obfs_param'] ?? '',
- 'protocol' => $server['protocol'],
- 'protocol-param' => $server['protocol_param'] ?? '',
- 'udp' => $server['udp'],
- ];
- }
- public static function buildVmess(array $server): ?array
- {
- $supportedMethods = ['auto', 'aes-128-gcm', 'chacha20-poly1305', 'none'];
- $supportedNetworks = ['ws', 'h2', 'http', 'grpc'];
- if (! in_array($server['method'], $supportedMethods, true) || ($server['v2_net'] && ! in_array($server['v2_net'], $supportedNetworks, true))) {
- return null;
- }
- $array = [
- 'name' => $server['name'],
- 'type' => 'vmess',
- 'server' => $server['host'],
- 'port' => $server['port'],
- 'uuid' => $server['uuid'],
- 'alterId' => $server['v2_alter_id'],
- 'cipher' => $server['method'],
- 'udp' => $server['udp'],
- ];
- if (isset($server['v2_tls']) && $server['v2_tls']) {
- $array['tls'] = true;
- $array['servername'] = $server['v2_host'];
- }
- $array['network'] = $server['v2_net'];
- if (isset($server['v2_net']) && $server['v2_net'] === 'ws') {
- $array['ws-opts'] = [];
- $array['ws-opts']['path'] = $server['v2_path'];
- if ($server['v2_host']) {
- $array['ws-opts']['headers'] = ['Host' => $server['v2_host']];
- }
- $array['ws-path'] = $server['v2_path'];
- if ($server['v2_host']) {
- $array['ws-headers'] = ['Host' => $server['v2_host']];
- }
- }
- return $array;
- }
- public static function buildTrojan(array $server): array
- {
- $array = [
- 'name' => $server['name'],
- 'type' => 'trojan',
- 'server' => $server['host'],
- 'port' => $server['port'],
- 'password' => $server['passwd'],
- 'udp' => $server['udp'],
- ];
- if (isset($server['sni'])) {
- $array['sni'] = $server['sni'];
- }
- if (isset($server['allow_insecure'])) {
- $array['skip-cert-verify'] = $server['allow_insecure'];
- }
- return $array;
- }
- public static function buildHysteria2(array $server): array|string|null
- {
- return null;
- }
- }
|