Loon.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. 'auto',
  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. if ($server['networkSettings']) {
  71. $tcpSettings = $server['networkSettings'];
  72. if (isset($tcpSettings['header']['type']) && !empty($tcpSettings['header']['type']))
  73. $config = str_replace('transport=tcp', "transport={$tcpSettings['header']['type']}", $config);
  74. if (isset($tcpSettings['header']['request']['path'][0]) && !empty($tcpSettings['header']['request']['path'][0]))
  75. array_push($config, "path={$tcpSettings['header']['request']['path'][0]}");
  76. if (isset($tcpSettings['header']['Host']) && !empty($tcpSettings['header']['Host']))
  77. array_push($config, "host={$tcpSettings['header']['Host']}");
  78. }
  79. }
  80. if ($server['tls']) {
  81. if ($server['network'] === 'tcp')
  82. array_push($config, 'over-tls=true');
  83. if ($server['tlsSettings']) {
  84. $tlsSettings = $server['tlsSettings'];
  85. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  86. array_push($config, 'skip-cert-verify=' . ($tlsSettings['allowInsecure'] ? 'true' : 'false'));
  87. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  88. array_push($config, "tls-name={$tlsSettings['serverName']}");
  89. }
  90. }
  91. if ($server['network'] === 'ws') {
  92. array_push($config, 'transport=ws');
  93. if ($server['networkSettings']) {
  94. $wsSettings = $server['networkSettings'];
  95. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  96. array_push($config, "path={$wsSettings['path']}");
  97. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  98. array_push($config, "host={$wsSettings['headers']['Host']}");
  99. }
  100. }
  101. $uri = implode(',', $config);
  102. $uri .= "\r\n";
  103. return $uri;
  104. }
  105. public static function buildTrojan($password, $server)
  106. {
  107. $config = [
  108. "{$server['name']}=trojan",
  109. "{$server['host']}",
  110. "{$server['port']}",
  111. "{$password}",
  112. $server['server_name'] ? "tls-name={$server['server_name']}" : "",
  113. 'fast-open=false',
  114. 'udp=true'
  115. ];
  116. if (!empty($server['allow_insecure'])) {
  117. array_push($config, $server['allow_insecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
  118. }
  119. $config = array_filter($config);
  120. $uri = implode(',', $config);
  121. $uri .= "\r\n";
  122. return $uri;
  123. }
  124. }