Shadowrocket.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. 'tfo' => 1,
  20. 'remark' => $server['name'],
  21. 'alterId' => $server['alter_id']
  22. ];
  23. if ($server['tls']) {
  24. $config['tls'] = 1;
  25. if ($server['tlsSettings']) {
  26. $tlsSettings = json_decode($server['tlsSettings'], true);
  27. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  28. $config['allowInsecure'] = (int)$tlsSettings['allowInsecure'];
  29. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  30. $config['peer'] = $tlsSettings['serverName'];
  31. }
  32. }
  33. if ($server['network'] === 'ws') {
  34. $config['obfs'] = "websocket";
  35. if ($server['networkSettings']) {
  36. $wsSettings = json_decode($server['networkSettings'], true);
  37. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  38. $config['path'] = $wsSettings['path'];
  39. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  40. $config['obfsParam'] = $wsSettings['headers']['Host'];
  41. }
  42. }
  43. $query = http_build_query($config, '', '&', PHP_QUERY_RFC3986);
  44. $uri = "vmess://{$userinfo}?{$query}";
  45. $uri .= "\r\n";
  46. return $uri;
  47. }
  48. public static function buildTrojan($password, $server)
  49. {
  50. $name = rawurlencode($server['name']);
  51. $query = http_build_query([
  52. 'allowInsecure' => $server['allow_insecure'],
  53. 'peer' => $server['server_name']
  54. ]);
  55. $uri = "trojan://{$password}@{$server['host']}:{$server['port']}?{$query}&tfo=1#{$name}";
  56. $uri .= "\r\n";
  57. return $uri;
  58. }
  59. }