Origin.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Utils;
  3. use App\Models\User;
  4. class Origin
  5. {
  6. public static function buildShadowsocks($server, User $user)
  7. {
  8. $name = rawurlencode($server['name']);
  9. $str = str_replace(
  10. ['+', '/', '='],
  11. ['-', '_', ''],
  12. base64_encode("{$server['cipher']}:{$user['uuid']}")
  13. );
  14. return "ss://{$str}@{$server['host']}:{$server['port']}#{$name}\r\n";
  15. }
  16. public static function buildVmess($server, User $user)
  17. {
  18. $config = [
  19. "v" => "2",
  20. "ps" => $server['name'],
  21. "add" => $server['host'],
  22. "port" => (string)$server['port'],
  23. "id" => $user['uuid'],
  24. "aid" => (string)$server['alter_id'],
  25. "net" => $server['network'],
  26. "type" => "none",
  27. "host" => "",
  28. "path" => "",
  29. "tls" => $server['tls'] ? "tls" : "",
  30. "sni" => $server['tls'] ? json_decode($server['tlsSettings'], true)['serverName'] : ""
  31. ];
  32. if ((string)$server['network'] === 'ws') {
  33. $wsSettings = json_decode($server['networkSettings'], true);
  34. if (isset($wsSettings['path'])) $config['path'] = $wsSettings['path'];
  35. if (isset($wsSettings['headers']['Host'])) $config['host'] = $wsSettings['headers']['Host'];
  36. }
  37. if ((string)$server['network'] === 'grpc') {
  38. $grpcSettings = json_decode($server['networkSettings'], true);
  39. if (isset($grpcSettings['path'])) $config['path'] = $grpcSettings['serviceName'];
  40. }
  41. return "vmess://" . base64_encode(json_encode($config)) . "\r\n";
  42. }
  43. public static function buildTrojan($server, User $user)
  44. {
  45. $name = rawurlencode($server['name']);
  46. $query = http_build_query([
  47. 'allowInsecure' => $server['allow_insecure'],
  48. 'peer' => $server['server_name'],
  49. 'sni' => $server['server_name']
  50. ]);
  51. $uri = "trojan://{$user['uuid']}@{$server['host']}:{$server['port']}?{$query}#{$name}";
  52. $uri .= "\r\n";
  53. return $uri;
  54. }
  55. }