Loon.php 4.7 KB

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