v2board/app/Services/ServerService.php

361 lines
14 KiB
PHP
Raw Normal View History

2020-02-28 01:03:05 +08:00
<?php
namespace App\Services;
2020-04-25 19:44:47 +08:00
use App\Models\ServerLog;
2020-10-04 14:21:09 +08:00
use App\Models\ServerShadowsocks;
2020-02-28 01:03:05 +08:00
use App\Models\User;
2021-09-21 18:07:53 +08:00
use App\Models\ServerV2ray;
2020-06-12 00:18:35 +08:00
use App\Models\ServerTrojan;
use App\Utils\CacheKey;
2022-02-17 02:44:49 +08:00
use App\Utils\Helper;
2020-06-12 00:18:35 +08:00
use Illuminate\Support\Facades\Cache;
2020-02-28 01:03:05 +08:00
class ServerService
{
2020-03-10 13:11:31 +08:00
CONST V2RAY_CONFIG = '{"log":{"loglevel":"debug","access":"access.log","error":"error.log"},"api":{"services":["HandlerService","StatsService"],"tag":"api"},"dns":{},"stats":{},"inbounds":[{"port":443,"protocol":"vmess","settings":{"clients":[]},"sniffing":{"enabled":true,"destOverride":["http","tls"]},"streamSettings":{"network":"tcp"},"tag":"proxy"},{"listen":"127.0.0.1","port":23333,"protocol":"dokodemo-door","settings":{"address":"0.0.0.0"},"tag":"api"}],"outbounds":[{"protocol":"freedom","settings":{}},{"protocol":"blackhole","settings":{},"tag":"block"}],"routing":{"rules":[{"type":"field","inboundTag":"api","outboundTag":"api"}]},"policy":{"levels":{"0":{"handshake":4,"connIdle":300,"uplinkOnly":5,"downlinkOnly":30,"statsUserUplink":true,"statsUserDownlink":true}}}}';
2020-06-12 02:06:56 +08:00
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}}';
2020-11-14 17:26:17 +08:00
public function getV2ray(User $user, $all = false):array
2020-06-12 00:18:35 +08:00
{
2020-11-16 11:58:34 +08:00
$servers = [];
2021-09-21 18:07:53 +08:00
$model = ServerV2ray::orderBy('sort', 'ASC');
2020-06-12 00:18:35 +08:00
if (!$all) {
$model->where('show', 1);
}
2020-11-16 11:58:34 +08:00
$v2ray = $model->get();
for ($i = 0; $i < count($v2ray); $i++) {
$v2ray[$i]['type'] = 'v2ray';
2021-08-06 00:43:01 +08:00
$groupId = $v2ray[$i]['group_id'];
2022-02-19 13:45:57 +08:00
if (!in_array($user->group_id, $groupId)) continue;
2022-02-17 03:17:05 +08:00
if (strpos($v2ray[$i]['port'], '-') !== false) {
$v2ray[$i]['port'] = Helper::randomPort($v2ray[$i]['port']);
}
2022-02-19 13:45:57 +08:00
if ($v2ray[$i]['parent_id']) {
$v2ray[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_V2RAY_LAST_CHECK_AT', $v2ray[$i]['parent_id']));
} else {
$v2ray[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_V2RAY_LAST_CHECK_AT', $v2ray[$i]['id']));
2022-02-17 02:44:49 +08:00
}
2022-02-19 13:45:57 +08:00
array_push($servers, $v2ray[$i]->toArray());
2020-06-12 00:18:35 +08:00
}
2020-11-16 11:58:34 +08:00
return $servers;
2020-06-12 00:18:35 +08:00
}
2020-11-15 15:25:32 +08:00
public function getTrojan(User $user, $all = false):array
2020-06-12 00:18:35 +08:00
{
2020-11-16 11:58:34 +08:00
$servers = [];
2020-06-12 00:18:35 +08:00
$model = ServerTrojan::orderBy('sort', 'ASC');
if (!$all) {
$model->where('show', 1);
}
2020-11-15 15:25:32 +08:00
$trojan = $model->get();
for ($i = 0; $i < count($trojan); $i++) {
$trojan[$i]['type'] = 'trojan';
2021-08-06 00:43:01 +08:00
$groupId = $trojan[$i]['group_id'];
2022-02-19 13:45:57 +08:00
if (!in_array($user->group_id, $groupId)) continue;
2022-02-17 03:17:05 +08:00
if (strpos($trojan[$i]['port'], '-') !== false) {
$trojan[$i]['port'] = Helper::randomPort($trojan[$i]['port']);
}
2022-02-19 13:45:57 +08:00
if ($trojan[$i]['parent_id']) {
$trojan[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojan[$i]['parent_id']));
} else {
$trojan[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojan[$i]['id']));
2022-02-17 02:44:49 +08:00
}
2022-02-19 13:45:57 +08:00
array_push($servers, $trojan[$i]->toArray());
2020-06-12 00:18:35 +08:00
}
2020-11-16 11:58:34 +08:00
return $servers;
2020-06-12 00:18:35 +08:00
}
2020-10-04 14:21:09 +08:00
public function getShadowsocks(User $user, $all = false)
{
2020-11-16 11:58:34 +08:00
$servers = [];
2020-10-04 14:21:09 +08:00
$model = ServerShadowsocks::orderBy('sort', 'ASC');
if (!$all) {
$model->where('show', 1);
}
2020-11-15 15:25:32 +08:00
$shadowsocks = $model->get();
for ($i = 0; $i < count($shadowsocks); $i++) {
$shadowsocks[$i]['type'] = 'shadowsocks';
2021-08-06 00:43:01 +08:00
$groupId = $shadowsocks[$i]['group_id'];
2022-02-19 13:45:57 +08:00
if (!in_array($user->group_id, $groupId)) continue;
2022-02-17 03:17:05 +08:00
if (strpos($shadowsocks[$i]['port'], '-') !== false) {
$shadowsocks[$i]['port'] = Helper::randomPort($shadowsocks[$i]['port']);
}
2022-02-19 13:45:57 +08:00
if ($shadowsocks[$i]['parent_id']) {
$shadowsocks[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $shadowsocks[$i]['parent_id']));
} else {
$shadowsocks[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $shadowsocks[$i]['id']));
2022-02-17 02:44:49 +08:00
}
2022-02-19 13:45:57 +08:00
array_push($servers, $shadowsocks[$i]->toArray());
2020-10-04 14:21:09 +08:00
}
2020-11-16 11:58:34 +08:00
return $servers;
2020-10-04 14:21:09 +08:00
}
2020-11-14 17:26:17 +08:00
public function getAvailableServers(User $user, $all = false)
2020-06-12 00:18:35 +08:00
{
2020-11-14 17:26:17 +08:00
$servers = array_merge(
$this->getShadowsocks($user, $all),
$this->getV2ray($user, $all),
$this->getTrojan($user, $all)
);
$tmp = array_column($servers, 'sort');
array_multisort($tmp, SORT_ASC, $servers);
return $servers;
2020-06-12 00:18:35 +08:00
}
2020-02-28 01:03:05 +08:00
public function getAvailableUsers($groupId)
{
return User::whereIn('group_id', $groupId)
->whereRaw('u + d < transfer_enable')
->where(function ($query) {
$query->where('expired_at', '>=', time())
2020-03-01 23:29:49 +08:00
->orWhere('expired_at', NULL);
2020-02-28 01:03:05 +08:00
})
2020-03-02 20:47:52 +08:00
->where('banned', 0)
2020-02-28 01:03:05 +08:00
->select([
'id',
'email',
't',
'u',
'd',
'transfer_enable',
2020-11-17 21:23:16 +08:00
'uuid'
2020-02-28 01:03:05 +08:00
])
->get();
}
2020-03-10 13:11:31 +08:00
2021-01-10 02:25:31 +08:00
public function getV2RayConfig(int $nodeId, int $localPort)
2020-03-10 13:11:31 +08:00
{
2021-09-21 18:07:53 +08:00
$server = ServerV2ray::find($nodeId);
2020-03-10 13:11:31 +08:00
if (!$server) {
abort(500, '节点不存在');
}
2020-06-12 01:31:00 +08:00
$json = json_decode(self::V2RAY_CONFIG);
2020-12-09 11:46:37 +08:00
$json->log->loglevel = (int)config('v2board.server_log_enable') ? 'debug' : 'none';
2022-01-07 02:20:34 +08:00
$json->inbounds[1]->port = (int)$localPort;
$json->inbounds[0]->port = (int)$server->server_port;
$json->inbounds[0]->streamSettings->network = $server->network;
2020-03-31 00:36:01 +08:00
$this->setDns($server, $json);
2020-03-31 00:22:49 +08:00
$this->setNetwork($server, $json);
$this->setRule($server, $json);
$this->setTls($server, $json);
2020-03-30 23:40:55 +08:00
return $json;
}
2020-06-12 01:31:00 +08:00
public function getTrojanConfig(int $nodeId, int $localPort)
{
$server = ServerTrojan::find($nodeId);
if (!$server) {
abort(500, '节点不存在');
}
$json = json_decode(self::TROJAN_CONFIG);
2020-06-18 20:25:53 +08:00
$json->local_port = $server->server_port;
2020-07-01 15:23:39 +08:00
$json->ssl->sni = $server->server_name ? $server->server_name : $server->host;
2020-07-02 21:41:14 +08:00
$json->ssl->cert = "/root/.cert/server.crt";
$json->ssl->key = "/root/.cert/server.key";
2020-06-12 01:31:00 +08:00
$json->api->api_port = $localPort;
return $json;
}
2021-09-21 21:30:00 +08:00
private function setDns(ServerV2ray $server, object $json)
2020-03-30 23:40:55 +08:00
{
if ($server->dnsSettings) {
2021-08-06 00:43:01 +08:00
$dns = $server->dnsSettings;
2020-04-07 02:47:12 +08:00
if (isset($dns->servers)) {
array_push($dns->servers, '1.1.1.1');
array_push($dns->servers, 'localhost');
}
2020-03-31 00:36:01 +08:00
$json->dns = $dns;
$json->outbounds[0]->settings->domainStrategy = 'UseIP';
2020-03-30 23:40:55 +08:00
}
}
2021-09-21 21:30:00 +08:00
private function setNetwork(ServerV2ray $server, object $json)
2020-03-30 23:40:55 +08:00
{
2020-03-10 13:11:31 +08:00
if ($server->networkSettings) {
switch ($server->network) {
case 'tcp':
$json->inbounds[0]->streamSettings->tcpSettings = $server->networkSettings;
2020-03-10 13:11:31 +08:00
break;
case 'kcp':
$json->inbounds[0]->streamSettings->kcpSettings = $server->networkSettings;
2020-03-10 13:11:31 +08:00
break;
case 'ws':
$json->inbounds[0]->streamSettings->wsSettings = $server->networkSettings;
2020-03-10 13:11:31 +08:00
break;
case 'http':
$json->inbounds[0]->streamSettings->httpSettings = $server->networkSettings;
2020-03-10 13:11:31 +08:00
break;
case 'domainsocket':
$json->inbounds[0]->streamSettings->dsSettings = $server->networkSettings;
2020-03-10 13:11:31 +08:00
break;
case 'quic':
$json->inbounds[0]->streamSettings->quicSettings = $server->networkSettings;
2020-03-10 13:11:31 +08:00
break;
2021-07-02 19:46:16 +08:00
case 'grpc':
$json->inbounds[0]->streamSettings->grpcSettings = $server->networkSettings;
2021-07-02 19:46:16 +08:00
break;
2020-03-10 13:11:31 +08:00
}
}
2020-03-30 23:40:55 +08:00
}
2020-03-10 13:11:31 +08:00
2021-09-21 21:30:00 +08:00
private function setRule(ServerV2ray $server, object $json)
2020-03-30 23:40:55 +08:00
{
2020-07-26 17:17:35 +08:00
$domainRules = array_filter(explode(PHP_EOL, config('v2board.server_v2ray_domain')));
$protocolRules = array_filter(explode(PHP_EOL, config('v2board.server_v2ray_protocol')));
2020-03-10 13:11:31 +08:00
if ($server->ruleSettings) {
2021-08-06 00:43:01 +08:00
$ruleSettings = $server->ruleSettings;
2020-03-10 13:11:31 +08:00
// domain
2020-07-26 17:17:35 +08:00
if (isset($ruleSettings->domain)) {
$ruleSettings->domain = array_filter($ruleSettings->domain);
if (!empty($ruleSettings->domain)) {
$domainRules = array_merge($domainRules, $ruleSettings->domain);
2020-07-22 23:45:00 +08:00
}
2020-03-10 13:11:31 +08:00
}
// protocol
2020-07-26 17:17:35 +08:00
if (isset($ruleSettings->protocol)) {
$ruleSettings->protocol = array_filter($ruleSettings->protocol);
if (!empty($ruleSettings->protocol)) {
$protocolRules = array_merge($protocolRules, $ruleSettings->protocol);
2020-07-22 23:45:00 +08:00
}
2020-03-10 13:11:31 +08:00
}
}
2020-07-26 17:17:35 +08:00
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);
}
2020-07-31 15:03:12 +08:00
if (empty($domainRules) && empty($protocolRules)) {
2022-01-07 02:20:34 +08:00
$json->inbounds[0]->sniffing->enabled = false;
2020-07-31 15:03:12 +08:00
}
2020-03-30 23:40:55 +08:00
}
2020-03-10 13:11:31 +08:00
2021-09-21 21:30:00 +08:00
private function setTls(ServerV2ray $server, object $json)
2020-03-30 23:40:55 +08:00
{
2020-03-10 13:11:31 +08:00
if ((int)$server->tls) {
2021-08-06 00:43:01 +08:00
$tlsSettings = $server->tlsSettings;
$json->inbounds[0]->streamSettings->security = 'tls';
2020-03-10 13:11:31 +08:00
$tls = (object)[
2020-07-02 21:41:14 +08:00
'certificateFile' => '/root/.cert/server.crt',
'keyFile' => '/root/.cert/server.key'
2020-03-10 13:11:31 +08:00
];
$json->inbounds[0]->streamSettings->tlsSettings = new \StdClass();
2020-03-10 13:30:30 +08:00
if (isset($tlsSettings->serverName)) {
$json->inbounds[0]->streamSettings->tlsSettings->serverName = (string)$tlsSettings->serverName;
2020-03-10 13:30:30 +08:00
}
if (isset($tlsSettings->allowInsecure)) {
$json->inbounds[0]->streamSettings->tlsSettings->allowInsecure = (int)$tlsSettings->allowInsecure ? true : false;
2020-03-10 13:30:30 +08:00
}
$json->inbounds[0]->streamSettings->tlsSettings->certificates[0] = $tls;
2020-03-10 13:11:31 +08:00
}
}
2020-04-25 19:44:47 +08:00
2020-06-11 20:47:02 +08:00
public function log(int $userId, int $serverId, int $u, int $d, float $rate, string $method)
2020-04-25 19:44:47 +08:00
{
2021-09-01 02:32:15 +08:00
if (($u + $d) < 10240) return true;
2021-09-02 20:09:04 +08:00
$timestamp = strtotime(date('Y-m-d'));
2020-05-11 17:19:58 +08:00
$serverLog = ServerLog::where('log_at', '>=', $timestamp)
->where('log_at', '<', $timestamp + 3600)
->where('server_id', $serverId)
->where('user_id', $userId)
->where('rate', $rate)
2020-06-11 20:47:02 +08:00
->where('method', $method)
2020-05-11 17:19:58 +08:00
->first();
if ($serverLog) {
2021-09-02 23:31:12 +08:00
try {
$serverLog->increment('u', $u);
$serverLog->increment('d', $d);
return true;
} catch (\Exception $e) {
return false;
}
2020-05-11 17:19:58 +08:00
} 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;
2020-06-11 20:47:02 +08:00
$serverLog->method = $method;
2021-08-31 23:55:07 +08:00
return $serverLog->save();
2020-05-11 17:19:58 +08:00
}
2020-04-25 19:44:47 +08:00
}
2020-11-14 17:26:17 +08:00
public function getShadowsocksServers()
{
$server = ServerShadowsocks::orderBy('sort', 'ASC')->get();
for ($i = 0; $i < count($server); $i++) {
$server[$i]['type'] = 'shadowsocks';
}
return $server->toArray();
}
public function getV2rayServers()
{
2021-09-21 18:07:53 +08:00
$server = ServerV2ray::orderBy('sort', 'ASC')->get();
2020-11-14 17:26:17 +08:00
for ($i = 0; $i < count($server); $i++) {
$server[$i]['type'] = 'v2ray';
}
return $server->toArray();
}
public function getTrojanServers()
{
$server = ServerTrojan::orderBy('sort', 'ASC')->get();
for ($i = 0; $i < count($server); $i++) {
$server[$i]['type'] = 'trojan';
2021-03-24 15:04:09 +08:00
}
return $server->toArray();
}
public function mergeData(&$servers)
{
foreach ($servers as $k => $v) {
$serverType = strtoupper($servers[$k]['type']);
$servers[$k]['online'] = Cache::get(CacheKey::get("SERVER_{$serverType}_ONLINE_USER", $servers[$k]['parent_id'] ? $servers[$k]['parent_id'] : $servers[$k]['id']));
if ($servers[$k]['parent_id']) {
$servers[$k]['last_check_at'] = Cache::get(CacheKey::get("SERVER_{$serverType}_LAST_CHECK_AT", $servers[$k]['parent_id']));
$servers[$k]['last_push_at'] = Cache::get(CacheKey::get("SERVER_{$serverType}_LAST_PUSH_AT", $servers[$k]['parent_id']));
2020-11-14 17:26:17 +08:00
} else {
2021-03-24 15:04:09 +08:00
$servers[$k]['last_check_at'] = Cache::get(CacheKey::get("SERVER_{$serverType}_LAST_CHECK_AT", $servers[$k]['id']));
$servers[$k]['last_push_at'] = Cache::get(CacheKey::get("SERVER_{$serverType}_LAST_PUSH_AT", $servers[$k]['id']));
}
if ((time() - 300) >= $servers[$k]['last_check_at']) {
$servers[$k]['available_status'] = 0;
} else if ((time() - 300) >= $servers[$k]['last_push_at']) {
$servers[$k]['available_status'] = 1;
} else {
$servers[$k]['available_status'] = 2;
2020-11-14 17:26:17 +08:00
}
}
}
2021-06-08 19:07:10 +08:00
public function getAllServers()
{
$servers = array_merge(
$this->getShadowsocksServers(),
$this->getV2rayServers(),
$this->getTrojanServers()
);
$this->mergeData($servers);
$tmp = array_column($servers, 'sort');
array_multisort($tmp, SORT_ASC, $servers);
return $servers;
}
2020-02-28 01:03:05 +08:00
}