Surge.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Utils;
  3. class Surge
  4. {
  5. public static function buildVmess($uuid, $server)
  6. {
  7. $proxies = $server->name . ' = vmess, ' . $server->host . ', ' . $server->port . ', username=' . $uuid . ', tfo=true';
  8. if ($server->tls) {
  9. $tlsSettings = json_decode($server->tlsSettings);
  10. $proxies .= ', tls=' . ($server->tls ? "true" : "false");
  11. if (isset($tlsSettings->allowInsecure)) {
  12. $proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false");
  13. }
  14. }
  15. if ($server->network == 'ws') {
  16. $proxies .= ', ws=true';
  17. if ($server->networkSettings) {
  18. $wsSettings = json_decode($server->networkSettings);
  19. if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
  20. if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
  21. }
  22. }
  23. $proxies .= "\r\n";
  24. return $proxies;
  25. }
  26. public static function buildTrojan($password, $server)
  27. {
  28. $skipCertVerify = (string)($server->allow_insecure ? true : false);
  29. $config = [
  30. "{$server->name}=trojan",
  31. "{$server->host}",
  32. "{$server->port}",
  33. "password={$password}",
  34. "skip-cert-verify={$skipCertVerify}",
  35. $server->server_name ? "sni={$server->server_name}" : "",
  36. "tfo=true"
  37. ];
  38. $config = array_filter($config);
  39. $uri = implode($config, ',');
  40. $uri .= "\r\n";
  41. return $uri;
  42. }
  43. }