From 589eec939277445dc2ac434aa4c556f5ad9dda67 Mon Sep 17 00:00:00 2001 From: Julian Liu Date: Fri, 20 Nov 2020 00:26:39 +0800 Subject: [PATCH 1/2] clean up buildvmess() --- app/Utils/Clash.php | 31 +++++++++++++++++------------- app/Utils/QuantumultX.php | 39 +++++++++++++++++++------------------- app/Utils/Shadowrocket.php | 25 ++++++++++++++++-------- app/Utils/Surfboard.php | 28 ++++++++++++--------------- app/Utils/Surge.php | 29 +++++++++++----------------- 5 files changed, 77 insertions(+), 75 deletions(-) diff --git a/app/Utils/Clash.php b/app/Utils/Clash.php index 8c4091df..f89a4de8 100644 --- a/app/Utils/Clash.php +++ b/app/Utils/Clash.php @@ -29,22 +29,27 @@ class Clash $array['alterId'] = $server['alter_id']; $array['cipher'] = 'auto'; $array['udp'] = true; + if ($server['tls']) { - $tlsSettings = json_decode($server['tlsSettings'], true); $array['tls'] = true; - if (!empty($tlsSettings['allowInsecure'])) $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false ); - if (!empty($tlsSettings['serverName'])) $array['servername'] = $tlsSettings['serverName']; - } - if ($server['network'] == 'ws') { - $array['network'] = $server['network']; - if ($server['networkSettings']) { - $wsSettings = json_decode($server['networkSettings'], true); - if (isset($wsSettings['path'])) $array['ws-path'] = $wsSettings['path']; - if (isset($wsSettings['headers']['Host'])) $array['ws-headers'] = [ - 'Host' => $wsSettings['headers']['Host'] - ]; + if ($server['tlsSettings']) { + $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']; } } + if ($server['network'] === 'ws') { + if ($server['networkSettings']) { + $wsSettings = json_decode($server['networkSettings'], true); + 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']]; + } + } + return $array; } @@ -58,7 +63,7 @@ class Clash $array['password'] = $password; $array['udp'] = true; if (!empty($server['server_name'])) $array['sni'] = $server['server_name']; - if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false ); + if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false); return $array; } } diff --git a/app/Utils/QuantumultX.php b/app/Utils/QuantumultX.php index ac8e6c68..7ef4d486 100644 --- a/app/Utils/QuantumultX.php +++ b/app/Utils/QuantumultX.php @@ -31,34 +31,33 @@ class QuantumultX 'udp-relay=true', "tag={$server['name']}" ]; - if ($server['network'] === 'tcp') { - if ($server['tls']) { - $tlsSettings = json_decode($server['tlsSettings'], true); + + if ($server['tls']) { + if ($server['network'] === 'tcp') { array_push($config, 'obfs=over-tls'); - if (isset($tlsSettings['allowInsecure'])) { - // Tips: allowInsecure=false = tls-verification=true - array_push($config, $tlsSettings['allowInsecure'] ? 'tls-verification=false' : 'tls-verification=true'); - } - if (!empty($tlsSettings['serverName'])) { - array_push($config, "obfs-host={$tlsSettings['serverName']}"); - } + } else { + array_push($config, 'obfs=wss'); } + } else if ($server['network'] === 'ws') { + array_push($config, 'obfs=ws'); } - if ($server['network'] === 'ws') { - if ($server['tls']) { + if ($server['tls']) { + if ($server['tlsSettings']) { $tlsSettings = json_decode($server['tlsSettings'], true); - array_push($config, 'obfs=wss'); - if (isset($tlsSettings['allowInsecure'])) { - array_push($config, $tlsSettings['allowInsecure'] ? 'tls-verification=false' : 'tls-verification=true'); - } - } else { - array_push($config, 'obfs=ws'); + if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure'])) + array_push($config, 'tls-verification=' . $tlsSettings['allowInsecure'] ? 'false' : 'true'); + if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName'])) + array_push($config, "sni={$tlsSettings['serverName']}"); } + } + if ($server['network'] === 'ws') { if ($server['networkSettings']) { $wsSettings = json_decode($server['networkSettings'], true); - if (isset($wsSettings['path'])) array_push($config, "obfs-uri={$wsSettings['path']}"); - if (isset($wsSettings['headers']['Host'])) array_push($config, "obfs-host={$wsSettings['headers']['Host']}"); + if (isset($wsSettings['path']) && !empty($wsSettings['path'])) + array_push($config, "obfs-uri={$wsSettings['path']}"); + if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host'])) + array_push($config, "obfs-host={$wsSettings['headers']['Host']}"); } } diff --git a/app/Utils/Shadowrocket.php b/app/Utils/Shadowrocket.php index fe562c93..e7fbcb3f 100644 --- a/app/Utils/Shadowrocket.php +++ b/app/Utils/Shadowrocket.php @@ -20,23 +20,32 @@ class Shadowrocket { $userinfo = base64_encode('auto:' . $uuid . '@' . $server['host'] . ':' . $server['port']); $config = [ + 'tfo' => 1, 'remark' => $server['name'], 'alterId' => $server['alter_id'] ]; if ($server['tls']) { - $tlsSettings = json_decode($server['tlsSettings'], true); $config['tls'] = 1; - if (isset($tlsSettings['serverName'])) $config['peer'] = $tlsSettings['serverName']; - if (isset($tlsSettings['allowInsecure'])) $config['allowInsecure'] = 1; + if ($server['tlsSettings']) { + $tlsSettings = json_decode($server['tlsSettings'], true); + if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure'])) + $config['allowInsecure'] = (int)$tlsSettings['allowInsecure']; + if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName'])) + $config['peer'] = $tlsSettings['serverName']; + } } if ($server['network'] === 'ws') { - $wsSettings = json_decode($server['networkSettings'], true); $config['obfs'] = "websocket"; - if (isset($wsSettings['path'])) $config['path'] = $wsSettings['path']; - if (isset($wsSettings['headers']['Host'])) $config['obfsParam'] = $wsSettings['headers']['Host']; + if ($server['networkSettings']) { + $wsSettings = json_decode($server['networkSettings'], true); + if (isset($wsSettings['path']) && !empty($wsSettings['path'])) + $config['path'] = $wsSettings['path']; + if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host'])) + $config['obfsParam'] = $wsSettings['headers']['Host']; + } } - $query = http_build_query($config, null, '&', PHP_QUERY_RFC3986); - $uri = "vmess://{$userinfo}?{$query}&tfo=1"; + $query = http_build_query($config, '', '&', PHP_QUERY_RFC3986); + $uri = "vmess://{$userinfo}?{$query}"; $uri .= "\r\n"; return $uri; } diff --git a/app/Utils/Surfboard.php b/app/Utils/Surfboard.php index 99a01c91..86ebad3a 100644 --- a/app/Utils/Surfboard.php +++ b/app/Utils/Surfboard.php @@ -33,29 +33,25 @@ class Surfboard 'tfo=true', 'udp-relay=true' ]; - if ($server['network'] === 'tcp') { - if ($server['tls']) { + + if ($server['tls']) { + array_push($config, 'tls=true'); + if ($server['tlsSettings']) { $tlsSettings = json_decode($server['tlsSettings'], true); - array_push($config, $server['tls'] ? 'tls=true' : 'tls=false'); - if (!empty($tlsSettings['allowInsecure'])) { - array_push($config, $tlsSettings['allowInsecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false'); - } + if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure'])) + array_push($config, 'skip-cert-verify=' . $tlsSettings['allowInsecure'] ? 'true' : 'false'); + if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName'])) + array_push($config, "sni={$tlsSettings['serverName']}"); } } - if ($server['network'] === 'ws') { array_push($config, 'ws=true'); - if ($server['tls']) { - $tlsSettings = json_decode($server['tlsSettings'], true); - array_push($config, $server['tls'] ? 'tls=true' : 'tls=false'); - if (!empty($tlsSettings['allowInsecure'])) { - array_push($config, $tlsSettings['allowInsecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false'); - } - } if ($server['networkSettings']) { $wsSettings = json_decode($server['networkSettings'], true); - if (isset($wsSettings['path'])) array_push($config, "ws-path={$wsSettings['path']}"); - if (isset($wsSettings['headers']['Host'])) array_push($config, "ws-headers=host:{$wsSettings['headers']['Host']}"); + if (isset($wsSettings['path']) && !empty($wsSettings['path'])) + array_push($config, "ws-path={$wsSettings['path']}"); + if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host'])) + array_push($config, "ws-headers=Host:{$wsSettings['headers']['Host']}"); } } diff --git a/app/Utils/Surge.php b/app/Utils/Surge.php index dc7926ae..743ac6dc 100644 --- a/app/Utils/Surge.php +++ b/app/Utils/Surge.php @@ -32,32 +32,25 @@ class Surge 'tfo=true', 'udp-relay=true' ]; - if ($server['network'] === 'tcp') { - if ($server['tls']) { + + if ($server['tls']) { + array_push($config, 'tls=true'); + if ($server['tlsSettings']) { $tlsSettings = json_decode($server['tlsSettings'], true); - array_push($config, $server['tls'] ? 'tls=true' : 'tls=false'); - if (!empty($tlsSettings['allowInsecure'])) { - array_push($config, $tlsSettings['allowInsecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false'); - } - if (!empty($tlsSettings['serverName'])) { + if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure'])) + array_push($config, 'skip-cert-verify=' . $tlsSettings['allowInsecure'] ? 'true' : 'false'); + if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName'])) array_push($config, "sni={$tlsSettings['serverName']}"); - } } } - if ($server['network'] === 'ws') { array_push($config, 'ws=true'); - if ($server['tls']) { - $tlsSettings = json_decode($server['tlsSettings'], true); - array_push($config, $server['tls'] ? 'tls=true' : 'tls=false'); - if (!empty($tlsSettings['allowInsecure'])) { - array_push($config, $tlsSettings['allowInsecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false'); - } - } if ($server['networkSettings']) { $wsSettings = json_decode($server['networkSettings'], true); - if (isset($wsSettings['path'])) array_push($config, "ws-path={$wsSettings['path']}"); - if (isset($wsSettings['headers']['Host'])) array_push($config, "ws-headers=host:{$wsSettings['headers']['Host']}"); + if (isset($wsSettings['path']) && !empty($wsSettings['path'])) + array_push($config, "ws-path={$wsSettings['path']}"); + if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host'])) + array_push($config, "ws-headers=Host:{$wsSettings['headers']['Host']}"); } } From 7fe8a5b2399834cc6adb9f9de5f1d2ed0caa7547 Mon Sep 17 00:00:00 2001 From: Julian Liu Date: Fri, 20 Nov 2020 00:53:53 +0800 Subject: [PATCH 2/2] clean up buildvmess() --- app/Utils/Clash.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Utils/Clash.php b/app/Utils/Clash.php index f89a4de8..8d351086 100644 --- a/app/Utils/Clash.php +++ b/app/Utils/Clash.php @@ -41,6 +41,7 @@ class Clash } } if ($server['network'] === 'ws') { + $array['network'] = 'ws'; if ($server['networkSettings']) { $wsSettings = json_decode($server['networkSettings'], true); if (isset($wsSettings['path']) && !empty($wsSettings['path']))