ServerService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. $json->dns = $dns;
  51. }
  52. }
  53. private function setNetwork(Server $server, object $json)
  54. {
  55. if ($server->networkSettings) {
  56. switch ($server->network) {
  57. case 'tcp':
  58. $json->inbound->streamSettings->tcpSettings = json_decode($server->networkSettings);
  59. break;
  60. case 'kcp':
  61. $json->inbound->streamSettings->kcpSettings = json_decode($server->networkSettings);
  62. break;
  63. case 'ws':
  64. $json->inbound->streamSettings->wsSettings = json_decode($server->networkSettings);
  65. break;
  66. case 'http':
  67. $json->inbound->streamSettings->httpSettings = json_decode($server->networkSettings);
  68. break;
  69. case 'domainsocket':
  70. $json->inbound->streamSettings->dsSettings = json_decode($server->networkSettings);
  71. break;
  72. case 'quic':
  73. $json->inbound->streamSettings->quicSettings = json_decode($server->networkSettings);
  74. break;
  75. }
  76. }
  77. }
  78. private function setRule(Server $server, object $json)
  79. {
  80. if ($server->ruleSettings) {
  81. $rules = json_decode($server->ruleSettings);
  82. // domain
  83. if (isset($rules->domain) && !empty($rules->domain)) {
  84. $domainObj = new \StdClass();
  85. $domainObj->type = 'field';
  86. $domainObj->domain = $rules->domain;
  87. $domainObj->outboundTag = 'block';
  88. array_push($json->routing->rules, $domainObj);
  89. }
  90. // protocol
  91. if (isset($rules->protocol) && !empty($rules->protocol)) {
  92. $protocolObj = new \StdClass();
  93. $protocolObj->type = 'field';
  94. $protocolObj->protocol = $rules->protocol;
  95. $protocolObj->outboundTag = 'block';
  96. array_push($json->routing->rules, $protocolObj);
  97. }
  98. }
  99. }
  100. private function setTls(Server $server, object $json)
  101. {
  102. if ((int)$server->tls) {
  103. $tlsSettings = json_decode($server->tlsSettings);
  104. $json->inbound->streamSettings->security = 'tls';
  105. $tls = (object)[
  106. 'certificateFile' => '/home/v2ray.crt',
  107. 'keyFile' => '/home/v2ray.key'
  108. ];
  109. $json->inbound->streamSettings->tlsSettings = new \StdClass();
  110. if (isset($tlsSettings->serverName)) {
  111. $json->inbound->streamSettings->tlsSettings->serverName = (string)$tlsSettings->serverName;
  112. }
  113. if (isset($tlsSettings->allowInsecure)) {
  114. $json->inbound->streamSettings->tlsSettings->allowInsecure = (int)$tlsSettings->allowInsecure ? true : false;
  115. }
  116. $json->inbound->streamSettings->tlsSettings->certificates[0] = $tls;
  117. }
  118. }
  119. }