ServerService.php 9.6 KB

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