Surge.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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['network'] === 'tcp') {
  32. if ($server['tls']) {
  33. $tlsSettings = json_decode($server['tlsSettings'], true);
  34. array_push($config, $server['tls'] ? 'tls=true' : 'tls=false');
  35. if (!empty($tlsSettings['allowInsecure'])) {
  36. array_push($config, $tlsSettings['allowInsecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
  37. }
  38. if (!empty($tlsSettings['serverName'])) {
  39. array_push($config, "sni={$tlsSettings['serverName']}");
  40. }
  41. }
  42. }
  43. if ($server['network'] === 'ws') {
  44. array_push($config, 'ws=true');
  45. if ($server['tls']) {
  46. $tlsSettings = json_decode($server['tlsSettings'], true);
  47. array_push($config, $server['tls'] ? 'tls=true' : 'tls=false');
  48. if (!empty($tlsSettings['allowInsecure'])) {
  49. array_push($config, $tlsSettings['allowInsecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
  50. }
  51. }
  52. if ($server['networkSettings']) {
  53. $wsSettings = json_decode($server['networkSettings'], true);
  54. if (isset($wsSettings['path'])) array_push($config, "ws-path={$wsSettings['path']}");
  55. if (isset($wsSettings['headers']['Host'])) array_push($config, "ws-headers=host:{$wsSettings['headers']['Host']}");
  56. }
  57. }
  58. $uri = implode(',', $config);
  59. $uri .= "\r\n";
  60. return $uri;
  61. }
  62. public static function buildTrojan($password, $server)
  63. {
  64. $config = [
  65. "{$server['name']}=trojan",
  66. "{$server['host']}",
  67. "{$server['port']}",
  68. "password={$password}",
  69. $server['server_name'] ? "sni={$server['server_name']}" : "",
  70. 'tfo=true',
  71. 'udp-relay=true'
  72. ];
  73. if (!empty($server['allow_insecure'])) {
  74. array_push($config, $server['allow_insecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
  75. }
  76. $config = array_filter($config);
  77. $uri = implode(',', $config);
  78. $uri .= "\r\n";
  79. return $uri;
  80. }
  81. }