ServerService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace App\Services;
  3. use App\Models\ServerLog;
  4. use App\Models\User;
  5. use App\Models\Server;
  6. use App\Models\ServerTrojan;
  7. use App\Utils\CacheKey;
  8. use App\Utils\Helper;
  9. use Illuminate\Support\Facades\Cache;
  10. class ServerService
  11. {
  12. 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}}}}';
  13. public function getVmess(User $user, $all = false):array
  14. {
  15. $vmess = [];
  16. $model = Server::orderBy('sort', 'ASC');
  17. if (!$all) {
  18. $model->where('show', 1);
  19. }
  20. $vmesss = $model->get();
  21. foreach ($vmesss as $k => $v) {
  22. $groupId = json_decode($vmesss[$k]['group_id']);
  23. if (in_array($user->group_id, $groupId)) {
  24. $vmesss[$k]['link'] = Helper::buildVmessLink($vmesss[$k], $user);
  25. if ($vmesss[$k]['parent_id']) {
  26. $vmesss[$k]['last_check_at'] = Cache::get(CacheKey::get('SERVER_LAST_CHECK_AT', $vmesss[$k]['parent_id']));
  27. } else {
  28. $vmesss[$k]['last_check_at'] = Cache::get(CacheKey::get('SERVER_LAST_CHECK_AT', $vmesss[$k]['id']));
  29. }
  30. array_push($vmess, $vmesss[$k]);
  31. }
  32. }
  33. return $vmess;
  34. }
  35. public function getTrojan(User $user, $all = false)
  36. {
  37. $trojan = [];
  38. $model = ServerTrojan::orderBy('sort', 'ASC');
  39. if (!$all) {
  40. $model->where('show', 1);
  41. }
  42. $trojans = $model->get();
  43. foreach ($trojans as $k => $v) {
  44. $groupId = json_decode($trojans[$k]['group_id']);
  45. if (in_array($user->group_id, $groupId)) {
  46. $vmesss[$k]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojans[$k]['id']));
  47. array_push($trojan, $trojans[$k]);
  48. }
  49. }
  50. return $trojan;
  51. }
  52. public function getAllServers(User $user, $all = false)
  53. {
  54. return [
  55. 'vmess' => $this->getVmess($user, $all),
  56. 'trojan' => $this->getTrojan($user, $all)
  57. ];
  58. }
  59. public function getAvailableUsers($groupId)
  60. {
  61. return User::whereIn('group_id', $groupId)
  62. ->whereRaw('u + d < transfer_enable')
  63. ->where(function ($query) {
  64. $query->where('expired_at', '>=', time())
  65. ->orWhere('expired_at', NULL);
  66. })
  67. ->where('banned', 0)
  68. ->select([
  69. 'id',
  70. 'email',
  71. 't',
  72. 'u',
  73. 'd',
  74. 'transfer_enable',
  75. 'uuid',
  76. 'v2ray_alter_id',
  77. 'v2ray_level'
  78. ])
  79. ->get();
  80. }
  81. public function getVmessConfig(int $nodeId, int $localPort)
  82. {
  83. $server = Server::find($nodeId);
  84. if (!$server) {
  85. abort(500, '节点不存在');
  86. }
  87. $json = json_decode(self::SERVER_CONFIG);
  88. $json->log->loglevel = config('v2board.server_log_level', 'none');
  89. $json->inboundDetour[0]->port = (int)$localPort;
  90. $json->inbound->port = (int)$server->server_port;
  91. $json->inbound->streamSettings->network = $server->network;
  92. $this->setDns($server, $json);
  93. $this->setNetwork($server, $json);
  94. $this->setRule($server, $json);
  95. $this->setTls($server, $json);
  96. return $json;
  97. }
  98. private function setDns(Server $server, object $json)
  99. {
  100. if ($server->dnsSettings) {
  101. $dns = json_decode($server->dnsSettings);
  102. if (isset($dns->servers)) {
  103. array_push($dns->servers, '1.1.1.1');
  104. array_push($dns->servers, 'localhost');
  105. }
  106. $json->dns = $dns;
  107. $json->outbound->settings->domainStrategy = 'UseIP';
  108. }
  109. }
  110. private function setNetwork(Server $server, object $json)
  111. {
  112. if ($server->networkSettings) {
  113. switch ($server->network) {
  114. case 'tcp':
  115. $json->inbound->streamSettings->tcpSettings = json_decode($server->networkSettings);
  116. break;
  117. case 'kcp':
  118. $json->inbound->streamSettings->kcpSettings = json_decode($server->networkSettings);
  119. break;
  120. case 'ws':
  121. $json->inbound->streamSettings->wsSettings = json_decode($server->networkSettings);
  122. break;
  123. case 'http':
  124. $json->inbound->streamSettings->httpSettings = json_decode($server->networkSettings);
  125. break;
  126. case 'domainsocket':
  127. $json->inbound->streamSettings->dsSettings = json_decode($server->networkSettings);
  128. break;
  129. case 'quic':
  130. $json->inbound->streamSettings->quicSettings = json_decode($server->networkSettings);
  131. break;
  132. }
  133. }
  134. }
  135. private function setRule(Server $server, object $json)
  136. {
  137. if ($server->ruleSettings) {
  138. $rules = json_decode($server->ruleSettings);
  139. // domain
  140. if (isset($rules->domain) && !empty($rules->domain)) {
  141. $rules->domain = array_filter($rules->domain);
  142. $domainObj = new \StdClass();
  143. $domainObj->type = 'field';
  144. $domainObj->domain = $rules->domain;
  145. $domainObj->outboundTag = 'block';
  146. array_push($json->routing->rules, $domainObj);
  147. }
  148. // protocol
  149. if (isset($rules->protocol) && !empty($rules->protocol)) {
  150. $rules->protocol = array_filter($rules->protocol);
  151. $protocolObj = new \StdClass();
  152. $protocolObj->type = 'field';
  153. $protocolObj->protocol = $rules->protocol;
  154. $protocolObj->outboundTag = 'block';
  155. array_push($json->routing->rules, $protocolObj);
  156. }
  157. }
  158. }
  159. private function setTls(Server $server, object $json)
  160. {
  161. if ((int)$server->tls) {
  162. $tlsSettings = json_decode($server->tlsSettings);
  163. $json->inbound->streamSettings->security = 'tls';
  164. $tls = (object)[
  165. 'certificateFile' => '/home/v2ray.crt',
  166. 'keyFile' => '/home/v2ray.key'
  167. ];
  168. $json->inbound->streamSettings->tlsSettings = new \StdClass();
  169. if (isset($tlsSettings->serverName)) {
  170. $json->inbound->streamSettings->tlsSettings->serverName = (string)$tlsSettings->serverName;
  171. }
  172. if (isset($tlsSettings->allowInsecure)) {
  173. $json->inbound->streamSettings->tlsSettings->allowInsecure = (int)$tlsSettings->allowInsecure ? true : false;
  174. }
  175. $json->inbound->streamSettings->tlsSettings->certificates[0] = $tls;
  176. }
  177. }
  178. public function log(int $userId, int $serverId, int $u, int $d, float $rate, string $method)
  179. {
  180. if (($u + $d) <= 10240) return;
  181. $timestamp = strtotime(date('Y-m-d H:0'));
  182. $serverLog = ServerLog::where('log_at', '>=', $timestamp)
  183. ->where('log_at', '<', $timestamp + 3600)
  184. ->where('server_id', $serverId)
  185. ->where('user_id', $userId)
  186. ->where('rate', $rate)
  187. ->where('method', $method)
  188. ->first();
  189. if ($serverLog) {
  190. $serverLog->u = $serverLog->u + $u;
  191. $serverLog->d = $serverLog->d + $d;
  192. $serverLog->save();
  193. } else {
  194. $serverLog = new ServerLog();
  195. $serverLog->user_id = $userId;
  196. $serverLog->server_id = $serverId;
  197. $serverLog->u = $u;
  198. $serverLog->d = $d;
  199. $serverLog->rate = $rate;
  200. $serverLog->log_at = $timestamp;
  201. $serverLog->method = $method;
  202. $serverLog->save();
  203. }
  204. }
  205. }