ServerService.php 9.2 KB

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