ServerService.php 9.3 KB

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