Surge.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Utils;
  3. class Surge
  4. {
  5. public static function buildShadowsocks($password, $server)
  6. {
  7. $config = [
  8. "{$server['name']}=ss",
  9. "{$server['host']}",
  10. "{$server['port']}",
  11. "encrypt-method={$server['cipher']}",
  12. "password={$password}",
  13. 'tfo=true',
  14. 'udp-relay=true'
  15. ];
  16. $config = array_filter($config);
  17. $uri = implode(',', $config);
  18. $uri .= "\r\n";
  19. return $uri;
  20. }
  21. public static function buildVmess($uuid, $server)
  22. {
  23. $config = [
  24. "{$server['name']}=vmess",
  25. "{$server['host']}",
  26. "{$server['port']}",
  27. "username={$uuid}",
  28. 'tfo=true',
  29. 'udp-relay=true'
  30. ];
  31. if ($server['tls']) {
  32. array_push($config, 'tls=true');
  33. if ($server['tlsSettings']) {
  34. $tlsSettings = json_decode($server['tlsSettings'], true);
  35. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  36. array_push($config, 'skip-cert-verify=' . ($tlsSettings['allowInsecure'] ? 'true' : 'false'));
  37. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  38. array_push($config, "sni={$tlsSettings['serverName']}");
  39. }
  40. }
  41. if ($server['network'] === 'ws') {
  42. array_push($config, 'ws=true');
  43. if ($server['networkSettings']) {
  44. $wsSettings = json_decode($server['networkSettings'], true);
  45. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  46. array_push($config, "ws-path={$wsSettings['path']}");
  47. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  48. array_push($config, "ws-headers=Host:{$wsSettings['headers']['Host']}");
  49. }
  50. }
  51. $uri = implode(',', $config);
  52. $uri .= "\r\n";
  53. return $uri;
  54. }
  55. public static function buildTrojan($password, $server)
  56. {
  57. $config = [
  58. "{$server['name']}=trojan",
  59. "{$server['host']}",
  60. "{$server['port']}",
  61. "password={$password}",
  62. $server['server_name'] ? "sni={$server['server_name']}" : "",
  63. 'tfo=true',
  64. 'udp-relay=true'
  65. ];
  66. if (!empty($server['allow_insecure'])) {
  67. array_push($config, $server['allow_insecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
  68. }
  69. $config = array_filter($config);
  70. $uri = implode(',', $config);
  71. $uri .= "\r\n";
  72. return $uri;
  73. }
  74. }