where('show', 1); } $vmesss = $model->get(); foreach ($vmesss as $k => $v) { $vmesss[$k]['protocol_type'] = 'vmess'; $groupId = json_decode($vmesss[$k]['group_id']); if (in_array($user->group_id, $groupId)) { $vmesss[$k]['link'] = URLSchemes::buildVmess($vmesss[$k], $user); if ($vmesss[$k]['parent_id']) { $vmesss[$k]['last_check_at'] = Cache::get(CacheKey::get('SERVER_V2RAY_LAST_CHECK_AT', $vmesss[$k]['parent_id'])); } else { $vmesss[$k]['last_check_at'] = Cache::get(CacheKey::get('SERVER_V2RAY_LAST_CHECK_AT', $vmesss[$k]['id'])); } array_push($vmess, $vmesss[$k]); } } return $vmess; } public function getTrojan(User $user, $all = false) { $trojan = []; $model = ServerTrojan::orderBy('sort', 'ASC'); if (!$all) { $model->where('show', 1); } $trojans = $model->get(); foreach ($trojans as $k => $v) { $trojans[$k]['protocol_type'] = 'trojan'; $groupId = json_decode($trojans[$k]['group_id']); $trojans[$k]['link'] = URLSchemes::buildTrojan($trojans[$k], $user); if (in_array($user->group_id, $groupId)) { if ($trojans[$k]['parent_id']) { $trojans[$k]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojans[$k]['parent_id'])); } else { $trojans[$k]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojans[$k]['id'])); } array_push($trojan, $trojans[$k]); } } return $trojan; } public function getShadowsocks(User $user, $all = false) { $shadowsocks = []; $model = ServerShadowsocks::orderBy('sort', 'ASC'); if (!$all) { $model->where('show', 1); } $shadowsockss = $model->get(); foreach ($shadowsockss as $k => $v) { $shadowsockss[$k]['protocol_type'] = 'shadowsocks'; $groupId = json_decode($shadowsockss[$k]['group_id']); $shadowsockss[$k]['link'] = URLSchemes::buildShadowsocks($shadowsockss[$k], $user); if (in_array($user->group_id, $groupId)) { if ($shadowsockss[$k]['parent_id']) { $shadowsockss[$k]['last_check_at'] = Cache::get(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $shadowsockss[$k]['parent_id'])); } else { $shadowsockss[$k]['last_check_at'] = Cache::get(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $shadowsockss[$k]['id'])); } array_push($shadowsocks, $shadowsockss[$k]); } } return $shadowsocks; } public function getAllServers(User $user, $all = false) { return [ 'shadowsocks' => $this->getShadowsocks($user, $all), 'vmess' => $this->getVmess($user, $all), 'trojan' => $this->getTrojan($user, $all) ]; } public function getAvailableUsers($groupId) { return User::whereIn('group_id', $groupId) ->whereRaw('u + d < transfer_enable') ->where(function ($query) { $query->where('expired_at', '>=', time()) ->orWhere('expired_at', NULL); }) ->where('banned', 0) ->select([ 'id', 'email', 't', 'u', 'd', 'transfer_enable', 'uuid', 'v2ray_alter_id', 'v2ray_level' ]) ->get(); } public function getVmessConfig(int $nodeId, int $localPort) { $server = Server::find($nodeId); if (!$server) { abort(500, '节点不存在'); } $json = json_decode(self::V2RAY_CONFIG); $json->log->loglevel = config('v2board.server_log_level', 'none'); $json->inboundDetour[0]->port = (int)$localPort; $json->inbound->port = (int)$server->server_port; $json->inbound->streamSettings->network = $server->network; $this->setDns($server, $json); $this->setNetwork($server, $json); $this->setRule($server, $json); $this->setTls($server, $json); return $json; } public function getTrojanConfig(int $nodeId, int $localPort) { $server = ServerTrojan::find($nodeId); if (!$server) { abort(500, '节点不存在'); } $json = json_decode(self::TROJAN_CONFIG); $json->local_port = $server->server_port; $json->ssl->sni = $server->server_name ? $server->server_name : $server->host; $json->ssl->cert = "/root/.cert/server.crt"; $json->ssl->key = "/root/.cert/server.key"; $json->api->api_port = $localPort; return $json; } private function setDns(Server $server, object $json) { if ($server->dnsSettings) { $dns = json_decode($server->dnsSettings); if (isset($dns->servers)) { array_push($dns->servers, '1.1.1.1'); array_push($dns->servers, 'localhost'); } $json->dns = $dns; $json->outbound->settings->domainStrategy = 'UseIP'; } } private function setNetwork(Server $server, object $json) { if ($server->networkSettings) { switch ($server->network) { case 'tcp': $json->inbound->streamSettings->tcpSettings = json_decode($server->networkSettings); break; case 'kcp': $json->inbound->streamSettings->kcpSettings = json_decode($server->networkSettings); break; case 'ws': $json->inbound->streamSettings->wsSettings = json_decode($server->networkSettings); break; case 'http': $json->inbound->streamSettings->httpSettings = json_decode($server->networkSettings); break; case 'domainsocket': $json->inbound->streamSettings->dsSettings = json_decode($server->networkSettings); break; case 'quic': $json->inbound->streamSettings->quicSettings = json_decode($server->networkSettings); break; } } } private function setRule(Server $server, object $json) { $domainRules = array_filter(explode(PHP_EOL, config('v2board.server_v2ray_domain'))); $protocolRules = array_filter(explode(PHP_EOL, config('v2board.server_v2ray_protocol'))); if ($server->ruleSettings) { $ruleSettings = json_decode($server->ruleSettings); // domain if (isset($ruleSettings->domain)) { $ruleSettings->domain = array_filter($ruleSettings->domain); if (!empty($ruleSettings->domain)) { $domainRules = array_merge($domainRules, $ruleSettings->domain); } } // protocol if (isset($ruleSettings->protocol)) { $ruleSettings->protocol = array_filter($ruleSettings->protocol); if (!empty($ruleSettings->protocol)) { $protocolRules = array_merge($protocolRules, $ruleSettings->protocol); } } } if (!empty($domainRules)) { $domainObj = new \StdClass(); $domainObj->type = 'field'; $domainObj->domain = $domainRules; $domainObj->outboundTag = 'block'; array_push($json->routing->rules, $domainObj); } if (!empty($protocolRules)) { $protocolObj = new \StdClass(); $protocolObj->type = 'field'; $protocolObj->protocol = $protocolRules; $protocolObj->outboundTag = 'block'; array_push($json->routing->rules, $protocolObj); } if (empty($domainRules) && empty($protocolRules)) { $json->inbound->sniffing->enabled = false; } } private function setTls(Server $server, object $json) { if ((int)$server->tls) { $tlsSettings = json_decode($server->tlsSettings); $json->inbound->streamSettings->security = 'tls'; $tls = (object)[ 'certificateFile' => '/root/.cert/server.crt', 'keyFile' => '/root/.cert/server.key' ]; $json->inbound->streamSettings->tlsSettings = new \StdClass(); if (isset($tlsSettings->serverName)) { $json->inbound->streamSettings->tlsSettings->serverName = (string)$tlsSettings->serverName; } if (isset($tlsSettings->allowInsecure)) { $json->inbound->streamSettings->tlsSettings->allowInsecure = (int)$tlsSettings->allowInsecure ? true : false; } $json->inbound->streamSettings->tlsSettings->certificates[0] = $tls; } } public function log(int $userId, int $serverId, int $u, int $d, float $rate, string $method) { if (($u + $d) <= 10240) return; $timestamp = strtotime(date('Y-m-d H:0')); $serverLog = ServerLog::where('log_at', '>=', $timestamp) ->where('log_at', '<', $timestamp + 3600) ->where('server_id', $serverId) ->where('user_id', $userId) ->where('rate', $rate) ->where('method', $method) ->first(); if ($serverLog) { $serverLog->u = $serverLog->u + $u; $serverLog->d = $serverLog->d + $d; $serverLog->save(); } else { $serverLog = new ServerLog(); $serverLog->user_id = $userId; $serverLog->server_id = $serverId; $serverLog->u = $u; $serverLog->d = $d; $serverLog->rate = $rate; $serverLog->log_at = $timestamp; $serverLog->method = $method; $serverLog->save(); } } }