ServerService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Services;
  3. use App\Models\User;
  4. use App\Models\Server;
  5. class ServerService
  6. {
  7. CONST SERVER_CONFIG = '{"api":{"services":["HandlerService","StatsService"],"tag":"api"},"dns":{},"stats":{},"inbound":{"port":443,"protocol":"vmess","settings":{"clients":[]},"sniffing":{"enabled":true,"destOverride":["http","tls"]},"streamSettings":{"network":"tcp"},"tag":"proxy"},"inboundDetour":[{"listen":"0.0.0.0","port":23333,"protocol":"dokodemo-door","settings":{"address":"0.0.0.0"},"tag":"api"}],"log":{"loglevel":"debug","access":"access.log","error":"error.log"},"outbound":{"protocol":"freedom","settings":{}},"outboundDetour":[{"protocol":"blackhole","settings":{},"tag":"block"}],"routing":{"rules":[{"inboundTag":"api","outboundTag":"api","type":"field"}]},"policy":{"levels":{"0":{"handshake":4,"connIdle":300,"uplinkOnly":5,"downlinkOnly":30,"statsUserUplink":true,"statsUserDownlink":true}}}}';
  8. public function getAvailableUsers($groupId)
  9. {
  10. return User::whereIn('group_id', $groupId)
  11. ->whereRaw('u + d < transfer_enable')
  12. ->where(function ($query) {
  13. $query->where('expired_at', '>=', time())
  14. ->orWhere('expired_at', NULL);
  15. })
  16. ->where('banned', 0)
  17. ->select([
  18. 'id',
  19. 'email',
  20. 't',
  21. 'u',
  22. 'd',
  23. 'transfer_enable',
  24. 'v2ray_uuid',
  25. 'v2ray_alter_id',
  26. 'v2ray_level'
  27. ])
  28. ->get();
  29. }
  30. public function getConfig(int $nodeId, int $localPort)
  31. {
  32. $server = Server::find($nodeId);
  33. if (!$server) {
  34. abort(500, '节点不存在');
  35. }
  36. $json = json_decode(self::SERVER_CONFIG);
  37. $json->inboundDetour[0]->port = (int)$localPort;
  38. $json->inbound->port = (int)$server->server_port;
  39. $json->inbound->streamSettings->network = $server->network;
  40. $this->setDns($server, $json);
  41. $this->setNetwork($server, $json);
  42. $this->setRule($server, $json);
  43. $this->setTls($server, $json);
  44. return $json;
  45. }
  46. private function setDns(Server $server, object $json)
  47. {
  48. if ($server->dnsSettings) {
  49. $dns = json_decode($server->dnsSettings);
  50. if (isset($dns->servers)) {
  51. array_push($dns->servers, '1.1.1.1');
  52. array_push($dns->servers, 'localhost');
  53. }
  54. $json->dns = $dns;
  55. $json->outbound->settings->domainStrategy = 'UseIP';
  56. }
  57. }
  58. private function setNetwork(Server $server, object $json)
  59. {
  60. if ($server->networkSettings) {
  61. switch ($server->network) {
  62. case 'tcp':
  63. $json->inbound->streamSettings->tcpSettings = json_decode($server->networkSettings);
  64. break;
  65. case 'kcp':
  66. $json->inbound->streamSettings->kcpSettings = json_decode($server->networkSettings);
  67. break;
  68. case 'ws':
  69. $json->inbound->streamSettings->wsSettings = json_decode($server->networkSettings);
  70. break;
  71. case 'http':
  72. $json->inbound->streamSettings->httpSettings = json_decode($server->networkSettings);
  73. break;
  74. case 'domainsocket':
  75. $json->inbound->streamSettings->dsSettings = json_decode($server->networkSettings);
  76. break;
  77. case 'quic':
  78. $json->inbound->streamSettings->quicSettings = json_decode($server->networkSettings);
  79. break;
  80. }
  81. }
  82. }
  83. private function setRule(Server $server, object $json)
  84. {
  85. if ($server->ruleSettings) {
  86. $rules = json_decode($server->ruleSettings);
  87. // domain
  88. if (isset($rules->domain) && !empty($rules->domain)) {
  89. $rules->domain = array_filter($rules->domain);
  90. $domainObj = new \StdClass();
  91. $domainObj->type = 'field';
  92. $domainObj->domain = $rules->domain;
  93. $domainObj->outboundTag = 'block';
  94. array_push($json->routing->rules, $domainObj);
  95. }
  96. // protocol
  97. if (isset($rules->protocol) && !empty($rules->protocol)) {
  98. $rules->protocol = array_filter($rules->protocol);
  99. $protocolObj = new \StdClass();
  100. $protocolObj->type = 'field';
  101. $protocolObj->protocol = $rules->protocol;
  102. $protocolObj->outboundTag = 'block';
  103. array_push($json->routing->rules, $protocolObj);
  104. }
  105. }
  106. }
  107. private function setTls(Server $server, object $json)
  108. {
  109. if ((int)$server->tls) {
  110. $tlsSettings = json_decode($server->tlsSettings);
  111. $json->inbound->streamSettings->security = 'tls';
  112. $tls = (object)[
  113. 'certificateFile' => '/home/v2ray.crt',
  114. 'keyFile' => '/home/v2ray.key'
  115. ];
  116. $json->inbound->streamSettings->tlsSettings = new \StdClass();
  117. if (isset($tlsSettings->serverName)) {
  118. $json->inbound->streamSettings->tlsSettings->serverName = (string)$tlsSettings->serverName;
  119. }
  120. if (isset($tlsSettings->allowInsecure)) {
  121. $json->inbound->streamSettings->tlsSettings->allowInsecure = (int)$tlsSettings->allowInsecure ? true : false;
  122. }
  123. $json->inbound->streamSettings->tlsSettings->certificates[0] = $tls;
  124. }
  125. }
  126. }