v2board/app/Utils/Surge.php

82 lines
2.7 KiB
PHP
Raw Normal View History

2020-06-13 18:33:42 +08:00
<?php
namespace App\Utils;
class Surge
{
public static function buildShadowsocks($password, $server)
{
$config = [
2020-11-15 17:10:32 +08:00
"{$server['name']}=ss",
"{$server['host']}",
"{$server['port']}",
"encrypt-method={$server['cipher']}",
"password={$password}",
'tfo=true',
'udp-relay=true'
];
$config = array_filter($config);
$uri = implode(',', $config);
$uri .= "\r\n";
return $uri;
}
2020-06-13 18:33:42 +08:00
public static function buildVmess($uuid, $server)
{
$config = [
2020-11-15 17:10:32 +08:00
"{$server['name']}=vmess",
"{$server['host']}",
"{$server['port']}",
"username={$uuid}",
'tfo=true',
'udp-relay=true'
];
2020-11-20 00:26:39 +08:00
if ($server['tls']) {
array_push($config, 'tls=true');
if ($server['tlsSettings']) {
2020-11-15 17:10:32 +08:00
$tlsSettings = json_decode($server['tlsSettings'], true);
2020-11-20 00:26:39 +08:00
if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
array_push($config, 'skip-cert-verify=' . $tlsSettings['allowInsecure'] ? 'true' : 'false');
if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
2020-11-15 17:10:32 +08:00
array_push($config, "sni={$tlsSettings['serverName']}");
2020-06-13 18:33:42 +08:00
}
}
2020-11-15 17:10:32 +08:00
if ($server['network'] === 'ws') {
array_push($config, 'ws=true');
2020-11-15 17:10:32 +08:00
if ($server['networkSettings']) {
$wsSettings = json_decode($server['networkSettings'], true);
2020-11-20 00:26:39 +08:00
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']}");
2020-06-13 18:33:42 +08:00
}
}
$uri = implode(',', $config);
$uri .= "\r\n";
return $uri;
2020-06-13 18:33:42 +08:00
}
public static function buildTrojan($password, $server)
{
2020-07-01 18:31:44 +08:00
$config = [
2020-11-15 17:10:32 +08:00
"{$server['name']}=trojan",
"{$server['host']}",
"{$server['port']}",
2020-07-01 18:31:44 +08:00
"password={$password}",
2020-11-15 17:10:32 +08:00
$server['server_name'] ? "sni={$server['server_name']}" : "",
'tfo=true',
'udp-relay=true'
2020-07-01 18:31:44 +08:00
];
2020-11-15 17:10:32 +08:00
if (!empty($server['allow_insecure'])) {
array_push($config, $server['allow_insecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
}
2020-07-01 18:31:44 +08:00
$config = array_filter($config);
$uri = implode(',', $config);
2020-06-13 18:33:42 +08:00
$uri .= "\r\n";
return $uri;
}
}