Loon.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers\Client\Protocols;
  3. use App\Utils\Helper;
  4. class Loon
  5. {
  6. public $flag = 'loon';
  7. private $servers;
  8. private $user;
  9. public function __construct($user, $servers)
  10. {
  11. $this->user = $user;
  12. $this->servers = $servers;
  13. }
  14. public function handle()
  15. {
  16. $servers = $this->servers;
  17. $user = $this->user;
  18. $uri = '';
  19. header("Subscription-Userinfo: upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}");
  20. foreach ($servers as $item) {
  21. if ($item['type'] === 'shadowsocks'
  22. && in_array($item['cipher'], [
  23. 'aes-128-gcm',
  24. 'aes-192-gcm',
  25. 'aes-256-gcm',
  26. 'chacha20-ietf-poly1305'
  27. ])
  28. ) {
  29. $uri .= self::buildShadowsocks($user['uuid'], $item);
  30. }
  31. if ($item['type'] === 'vmess') {
  32. $uri .= self::buildVmess($user['uuid'], $item);
  33. }
  34. if ($item['type'] === 'trojan') {
  35. $uri .= self::buildTrojan($user['uuid'], $item);
  36. }
  37. }
  38. return $uri;
  39. }
  40. public static function buildShadowsocks($password, $server)
  41. {
  42. $config = [
  43. "{$server['name']}=Shadowsocks",
  44. "{$server['host']}",
  45. "{$server['port']}",
  46. "{$server['cipher']}",
  47. "{$password}",
  48. 'fast-open=false',
  49. 'udp=true'
  50. ];
  51. $config = array_filter($config);
  52. $uri = implode(',', $config);
  53. $uri .= "\r\n";
  54. return $uri;
  55. }
  56. public static function buildVmess($uuid, $server)
  57. {
  58. $config = [
  59. "{$server['name']}=vmess",
  60. "{$server['host']}",
  61. "{$server['port']}",
  62. 'aes-128-gcm',
  63. "{$uuid}",
  64. 'fast-open=false',
  65. 'udp=true',
  66. "alterId=0"
  67. ];
  68. if ($server['network'] === 'tcp') {
  69. array_push($config, 'transport=tcp');
  70. }
  71. if ($server['tls']) {
  72. if ($server['network'] === 'tcp')
  73. array_push($config, 'over-tls=true');
  74. if ($server['tlsSettings']) {
  75. $tlsSettings = $server['tlsSettings'];
  76. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  77. array_push($config, 'skip-cert-verify=' . ($tlsSettings['allowInsecure'] ? 'true' : 'false'));
  78. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  79. array_push($config, "tls-name={$tlsSettings['serverName']}");
  80. }
  81. }
  82. if ($server['network'] === 'ws') {
  83. array_push($config, 'transport=ws');
  84. if ($server['networkSettings']) {
  85. $wsSettings = $server['networkSettings'];
  86. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  87. array_push($config, "path={$wsSettings['path']}");
  88. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  89. array_push($config, "host={$wsSettings['headers']['Host']}");
  90. }
  91. }
  92. $uri = implode(',', $config);
  93. $uri .= "\r\n";
  94. return $uri;
  95. }
  96. public static function buildTrojan($password, $server)
  97. {
  98. $config = [
  99. "{$server['name']}=trojan",
  100. "{$server['host']}",
  101. "{$server['port']}",
  102. "{$password}",
  103. $server['server_name'] ? "tls-name={$server['server_name']}" : "",
  104. 'fast-open=false',
  105. 'udp=true'
  106. ];
  107. if (!empty($server['allow_insecure'])) {
  108. array_push($config, $server['allow_insecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
  109. }
  110. $config = array_filter($config);
  111. $uri = implode(',', $config);
  112. $uri .= "\r\n";
  113. return $uri;
  114. }
  115. }