Surge.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. $proxies = $server->name . ' = vmess, ' . $server->host . ', ' . $server->port . ', username=' . $uuid . ', tfo=true';
  24. if ($server->tls) {
  25. $tlsSettings = json_decode($server->tlsSettings);
  26. $proxies .= ', tls=' . ($server->tls ? "true" : "false");
  27. if (isset($tlsSettings->allowInsecure)) {
  28. $proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false");
  29. }
  30. }
  31. if ($server->network == 'ws') {
  32. $proxies .= ', ws=true';
  33. if ($server->networkSettings) {
  34. $wsSettings = json_decode($server->networkSettings);
  35. if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
  36. if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
  37. }
  38. }
  39. $proxies .= "\r\n";
  40. return $proxies;
  41. }
  42. public static function buildTrojan($password, $server)
  43. {
  44. $config = [
  45. "{$server->name}=trojan",
  46. "{$server->host}",
  47. "{$server->port}",
  48. "password={$password}",
  49. $server->allow_insecure ? 'skip-cert-verify=true' : 'skip-cert-verify=false',
  50. $server->server_name ? "sni={$server->server_name}" : "",
  51. "tfo=true"
  52. ];
  53. $config = array_filter($config);
  54. $uri = implode(',', $config);
  55. $uri .= "\r\n";
  56. return $uri;
  57. }
  58. }