v2board/app/Services/ServerService.php

165 lines
6.5 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-02-28 01:03:05 +08:00
use App\Models\User;
2020-03-10 13:11:31 +08:00
use App\Models\Server;
2020-02-28 01:03:05 +08:00
class ServerService
{
2020-03-10 13:11:31 +08:00
2020-03-31 00:34:25 +08:00
CONST SERVER_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}}}}';
2020-03-10 13:11:31 +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',
'v2ray_uuid',
'v2ray_alter_id',
'v2ray_level'
])
->get();
}
2020-03-10 13:11:31 +08:00
public function getConfig(int $nodeId, int $localPort)
{
$server = Server::find($nodeId);
if (!$server) {
abort(500, '节点不存在');
}
$json = json_decode(self::SERVER_CONFIG);
$json->inboundDetour[0]->port = (int)$localPort;
$json->inbound->port = (int)$server->server_port;
$json->inbound->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;
}
private function setDns(Server $server, object $json)
{
if ($server->dnsSettings) {
2020-03-31 00:34:25 +08:00
$dns = json_decode($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;
2020-04-06 18:05:58 +08:00
$json->outbound->settings->domainStrategy = 'UseIP';
2020-03-30 23:40:55 +08:00
}
}
private function setNetwork(Server $server, object $json)
{
2020-03-10 13:11:31 +08:00
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;
}
}
2020-03-30 23:40:55 +08:00
}
2020-03-10 13:11:31 +08:00
2020-03-31 00:22:07 +08:00
private function setRule(Server $server, object $json)
2020-03-30 23:40:55 +08:00
{
2020-03-10 13:11:31 +08:00
if ($server->ruleSettings) {
$rules = json_decode($server->ruleSettings);
// domain
if (isset($rules->domain) && !empty($rules->domain)) {
2020-04-13 17:28:59 +08:00
$rules->domain = array_filter($rules->domain);
2020-03-10 13:11:31 +08:00
$domainObj = new \StdClass();
$domainObj->type = 'field';
$domainObj->domain = $rules->domain;
$domainObj->outboundTag = 'block';
array_push($json->routing->rules, $domainObj);
}
// protocol
if (isset($rules->protocol) && !empty($rules->protocol)) {
2020-04-13 17:28:59 +08:00
$rules->protocol = array_filter($rules->protocol);
2020-03-10 13:11:31 +08:00
$protocolObj = new \StdClass();
$protocolObj->type = 'field';
$protocolObj->protocol = $rules->protocol;
$protocolObj->outboundTag = 'block';
array_push($json->routing->rules, $protocolObj);
}
}
2020-03-30 23:40:55 +08:00
}
2020-03-10 13:11:31 +08:00
2020-03-30 23:40:55 +08:00
private function setTls(Server $server, object $json)
{
2020-03-10 13:11:31 +08:00
if ((int)$server->tls) {
2020-03-10 13:30:30 +08:00
$tlsSettings = json_decode($server->tlsSettings);
2020-03-10 13:11:31 +08:00
$json->inbound->streamSettings->security = 'tls';
$tls = (object)[
'certificateFile' => '/home/v2ray.crt',
'keyFile' => '/home/v2ray.key'
];
$json->inbound->streamSettings->tlsSettings = new \StdClass();
2020-03-10 13:30:30 +08:00
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;
}
2020-03-10 13:11:31 +08:00
$json->inbound->streamSettings->tlsSettings->certificates[0] = $tls;
}
}
2020-04-25 19:44:47 +08:00
public function log(int $userId, int $serverId, int $u, int $d, float $rate)
{
2020-05-11 17:19:58 +08:00
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)
->first();
if ($serverLog) {
$serverLog->u = $serverLog->u + $u;
2020-05-11 18:27:36 +08:00
$serverLog->d = $serverLog->d + $d;
2020-05-11 17:19:58 +08:00
$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->save();
}
2020-04-25 19:44:47 +08:00
}
2020-02-28 01:03:05 +08:00
}