v2board/app/Utils/Clash.php

79 lines
2.8 KiB
PHP
Raw Normal View History

2020-06-12 00:18:35 +08:00
<?php
namespace App\Utils;
class Clash
{
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;
if (isset($server['tlsSettings'])) {
2020-11-20 00:26:39 +08:00
$tlsSettings = json_decode($server['tlsSettings'], true);
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';
if (isset($server['networkSettings'])) {
2020-11-15 17:10:32 +08:00
$wsSettings = json_decode($server['networkSettings'], true);
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';
if (isset($server['networkSettings'])) {
$grpcObject = json_decode($server['networkSettings'], true);
$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;
}
}