ServerService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. $trojans[$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->local_port = $server->port;
  107. $json->ssl->sni = $server->host;
  108. $json->ssl->cert = "/root/.cert/{$server->host}.crt";
  109. $json->ssl->key = "/root/.cert/{$server->host}.key";
  110. $json->api->api_port = $localPort;
  111. return $json;
  112. }
  113. private function setDns(Server $server, object $json)
  114. {
  115. if ($server->dnsSettings) {
  116. $dns = json_decode($server->dnsSettings);
  117. if (isset($dns->servers)) {
  118. array_push($dns->servers, '1.1.1.1');
  119. array_push($dns->servers, 'localhost');
  120. }
  121. $json->dns = $dns;
  122. $json->outbound->settings->domainStrategy = 'UseIP';
  123. }
  124. }
  125. private function setNetwork(Server $server, object $json)
  126. {
  127. if ($server->networkSettings) {
  128. switch ($server->network) {
  129. case 'tcp':
  130. $json->inbound->streamSettings->tcpSettings = json_decode($server->networkSettings);
  131. break;
  132. case 'kcp':
  133. $json->inbound->streamSettings->kcpSettings = json_decode($server->networkSettings);
  134. break;
  135. case 'ws':
  136. $json->inbound->streamSettings->wsSettings = json_decode($server->networkSettings);
  137. break;
  138. case 'http':
  139. $json->inbound->streamSettings->httpSettings = json_decode($server->networkSettings);
  140. break;
  141. case 'domainsocket':
  142. $json->inbound->streamSettings->dsSettings = json_decode($server->networkSettings);
  143. break;
  144. case 'quic':
  145. $json->inbound->streamSettings->quicSettings = json_decode($server->networkSettings);
  146. break;
  147. }
  148. }
  149. }
  150. private function setRule(Server $server, object $json)
  151. {
  152. if ($server->ruleSettings) {
  153. $rules = json_decode($server->ruleSettings);
  154. // domain
  155. if (isset($rules->domain) && !empty($rules->domain)) {
  156. $rules->domain = array_filter($rules->domain);
  157. $domainObj = new \StdClass();
  158. $domainObj->type = 'field';
  159. $domainObj->domain = $rules->domain;
  160. $domainObj->outboundTag = 'block';
  161. array_push($json->routing->rules, $domainObj);
  162. }
  163. // protocol
  164. if (isset($rules->protocol) && !empty($rules->protocol)) {
  165. $rules->protocol = array_filter($rules->protocol);
  166. $protocolObj = new \StdClass();
  167. $protocolObj->type = 'field';
  168. $protocolObj->protocol = $rules->protocol;
  169. $protocolObj->outboundTag = 'block';
  170. array_push($json->routing->rules, $protocolObj);
  171. }
  172. }
  173. }
  174. private function setTls(Server $server, object $json)
  175. {
  176. if ((int)$server->tls) {
  177. $tlsSettings = json_decode($server->tlsSettings);
  178. $json->inbound->streamSettings->security = 'tls';
  179. $tls = (object)[
  180. 'certificateFile' => '/home/v2ray.crt',
  181. 'keyFile' => '/home/v2ray.key'
  182. ];
  183. $json->inbound->streamSettings->tlsSettings = new \StdClass();
  184. if (isset($tlsSettings->serverName)) {
  185. $json->inbound->streamSettings->tlsSettings->serverName = (string)$tlsSettings->serverName;
  186. }
  187. if (isset($tlsSettings->allowInsecure)) {
  188. $json->inbound->streamSettings->tlsSettings->allowInsecure = (int)$tlsSettings->allowInsecure ? true : false;
  189. }
  190. $json->inbound->streamSettings->tlsSettings->certificates[0] = $tls;
  191. }
  192. }
  193. public function log(int $userId, int $serverId, int $u, int $d, float $rate, string $method)
  194. {
  195. if (($u + $d) <= 10240) return;
  196. $timestamp = strtotime(date('Y-m-d H:0'));
  197. $serverLog = ServerLog::where('log_at', '>=', $timestamp)
  198. ->where('log_at', '<', $timestamp + 3600)
  199. ->where('server_id', $serverId)
  200. ->where('user_id', $userId)
  201. ->where('rate', $rate)
  202. ->where('method', $method)
  203. ->first();
  204. if ($serverLog) {
  205. $serverLog->u = $serverLog->u + $u;
  206. $serverLog->d = $serverLog->d + $d;
  207. $serverLog->save();
  208. } else {
  209. $serverLog = new ServerLog();
  210. $serverLog->user_id = $userId;
  211. $serverLog->server_id = $serverId;
  212. $serverLog->u = $u;
  213. $serverLog->d = $d;
  214. $serverLog->rate = $rate;
  215. $serverLog->log_at = $timestamp;
  216. $serverLog->method = $method;
  217. $serverLog->save();
  218. }
  219. }
  220. }