Shadowrocket.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Utils;
  3. class Shadowrocket
  4. {
  5. public static function buildShadowsocks($password, $server)
  6. {
  7. $name = rawurlencode($server->name);
  8. $str = str_replace(
  9. ['+', '/', '='],
  10. ['-', '_', ''],
  11. base64_encode("{$server->cipher}:{$password}")
  12. );
  13. return "ss://{$str}@{$server->host}:{$server->port}#{$name}\r\n";
  14. }
  15. public static function buildVmess($uuid, $server)
  16. {
  17. $userinfo = base64_encode('auto:' . $uuid . '@' . $server->host . ':' . $server->port);
  18. $config = [
  19. 'remark' => $server->name
  20. ];
  21. if ($server->tls) {
  22. $tlsSettings = json_decode($server->tlsSettings);
  23. $config['tls'] = 1;
  24. if (isset($tlsSettings->serverName)) $config['peer'] = $tlsSettings->serverName;
  25. if (isset($tlsSettings->allowInsecure)) $config['allowInsecure'] = 1;
  26. }
  27. if ($server->network === 'ws') {
  28. $wsSettings = json_decode($server->networkSettings);
  29. $config['obfs'] = "websocket";
  30. if (isset($wsSettings->path)) $config['path'] = $wsSettings->path;
  31. if (isset($wsSettings->headers->Host)) $config['obfsParam'] = $wsSettings->headers->Host;
  32. }
  33. $query = http_build_query($config, null, '&', PHP_QUERY_RFC3986);
  34. $uri = "vmess://{$userinfo}?{$query}&tfo=1";
  35. $uri .= "\r\n";
  36. return $uri;
  37. }
  38. public static function buildTrojan($password, $server)
  39. {
  40. $name = rawurlencode($server->name);
  41. $query = http_build_query([
  42. 'allowInsecure' => $server->allow_insecure,
  43. 'peer' => $server->server_name
  44. ]);
  45. $uri = "trojan://{$password}@{$server->host}:{$server->port}?{$query}&tfo=1#{$name}";
  46. $uri .= "\r\n";
  47. return $uri;
  48. }
  49. }