V2rayN.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Http\Controllers\Client\Protocols;
  3. class V2rayN
  4. {
  5. public $flag = 'v2rayn';
  6. private $servers;
  7. private $user;
  8. public function __construct($user, $servers)
  9. {
  10. $this->user = $user;
  11. $this->servers = $servers;
  12. }
  13. public function handle()
  14. {
  15. $servers = $this->servers;
  16. $user = $this->user;
  17. $uri = '';
  18. foreach ($servers as $item) {
  19. if ($item['type'] === 'v2ray') {
  20. $uri .= self::buildVmess($user['uuid'], $item);
  21. }
  22. }
  23. return base64_encode($uri);
  24. }
  25. public static function buildVmess($uuid, $server)
  26. {
  27. $config = [
  28. "v" => "2",
  29. "ps" => $server['name'],
  30. "add" => $server['host'],
  31. "port" => (string)$server['port'],
  32. "id" => $uuid,
  33. "aid" => (string)$server['alter_id'],
  34. "net" => $server['network'],
  35. "type" => "none",
  36. "host" => "",
  37. "path" => "",
  38. "tls" => $server['tls'] ? "tls" : "",
  39. "sni" => $server['tls'] ? isset(json_decode($server['tlsSettings'], true)['serverName']) : ""
  40. ];
  41. if ((string)$server['network'] === 'ws') {
  42. $wsSettings = json_decode($server['networkSettings'], true);
  43. if (isset($wsSettings['path'])) $config['path'] = $wsSettings['path'];
  44. if (isset($wsSettings['headers']['Host'])) $config['host'] = $wsSettings['headers']['Host'];
  45. }
  46. if ((string)$server['network'] === 'grpc') {
  47. $grpcSettings = json_decode($server['networkSettings'], true);
  48. if (isset($grpcSettings['path'])) $config['path'] = $grpcSettings['serviceName'];
  49. }
  50. return "vmess://" . base64_encode(json_encode($config)) . "\r\n";
  51. }
  52. }