Shadowrocket.php 1.5 KB

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