Surge.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Utils\Clients\Protocols;
  3. use App\Utils\Library\Templates\Protocol;
  4. class Surge implements Protocol
  5. {
  6. public static function build(array $servers): array
  7. {
  8. $validTypes = ['shadowsocks', 'vmess', 'trojan'];
  9. $names = '';
  10. $proxies = '';
  11. foreach ($servers as $server) {
  12. if (in_array($server['type'], $validTypes, true)) {
  13. $names .= $server['name'].', ';
  14. $proxies .= call_user_func([self::class, 'build'.ucfirst($server['type'])], $server);
  15. }
  16. }
  17. return ['name' => rtrim($names, ', '), 'proxies' => $proxies];
  18. }
  19. public static function buildShadowsocks(array $server): string
  20. {
  21. $config = array_filter([
  22. "{$server['name']}=ss",
  23. $server['host'],
  24. $server['port'],
  25. "encrypt-method={$server['method']}",
  26. "password={$server['passwd']}",
  27. 'tfo=true',
  28. "udp-relay={$server['udp']}",
  29. ]);
  30. return implode(',', $config).PHP_EOL;
  31. }
  32. public static function buildVmess(array $server): string
  33. {
  34. $config = [
  35. "{$server['name']}=vmess",
  36. $server['host'],
  37. $server['port'],
  38. "username={$server['uuid']}",
  39. 'vmess-aead=true',
  40. 'tfo=true',
  41. "udp-relay={$server['udp']}",
  42. ];
  43. if (isset($server['v2_tls']) && $server['v2_tls']) {
  44. array_push($config, 'tls=true', "sni={$server['v2_host']}");
  45. }
  46. if (isset($server['v2_net']) && $server['v2_net'] === 'ws') {
  47. array_push($config, 'ws=true', "ws-path={$server['v2_path']}", "ws-headers=Host:{$server['v2_host']}");
  48. }
  49. return implode(',', $config).PHP_EOL;
  50. }
  51. public static function buildTrojan(array $server): string
  52. {
  53. $config = array_filter([
  54. "{$server['name']}=trojan",
  55. $server['host'],
  56. $server['port'],
  57. "password={$server['passwd']}",
  58. isset($server['sni']) ? "sni={$server['sni']}" : '',
  59. 'tfo=true',
  60. "udp-relay={$server['udp']}",
  61. // "skip-cert-verify={$server['allow_insecure']}"
  62. ]);
  63. return implode(',', $config).PHP_EOL;
  64. }
  65. public static function buildShadowsocksr(array $server): array|string
  66. {
  67. return '';
  68. }
  69. }