|
@@ -5,7 +5,7 @@ namespace App\Services;
|
|
use App\Models\ServerLog;
|
|
use App\Models\ServerLog;
|
|
use App\Models\ServerShadowsocks;
|
|
use App\Models\ServerShadowsocks;
|
|
use App\Models\User;
|
|
use App\Models\User;
|
|
-use App\Models\Server;
|
|
|
|
|
|
+use App\Models\ServerV2ray;
|
|
use App\Models\ServerTrojan;
|
|
use App\Models\ServerTrojan;
|
|
use App\Utils\CacheKey;
|
|
use App\Utils\CacheKey;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Cache;
|
|
@@ -18,14 +18,14 @@ class ServerService
|
|
public function getV2ray(User $user, $all = false):array
|
|
public function getV2ray(User $user, $all = false):array
|
|
{
|
|
{
|
|
$servers = [];
|
|
$servers = [];
|
|
- $model = Server::orderBy('sort', 'ASC');
|
|
|
|
|
|
+ $model = ServerV2ray::orderBy('sort', 'ASC');
|
|
if (!$all) {
|
|
if (!$all) {
|
|
$model->where('show', 1);
|
|
$model->where('show', 1);
|
|
}
|
|
}
|
|
$v2ray = $model->get();
|
|
$v2ray = $model->get();
|
|
for ($i = 0; $i < count($v2ray); $i++) {
|
|
for ($i = 0; $i < count($v2ray); $i++) {
|
|
$v2ray[$i]['type'] = 'v2ray';
|
|
$v2ray[$i]['type'] = 'v2ray';
|
|
- $groupId = json_decode($v2ray[$i]['group_id']);
|
|
|
|
|
|
+ $groupId = $v2ray[$i]['group_id'];
|
|
if (in_array($user->group_id, $groupId)) {
|
|
if (in_array($user->group_id, $groupId)) {
|
|
if ($v2ray[$i]['parent_id']) {
|
|
if ($v2ray[$i]['parent_id']) {
|
|
$v2ray[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_V2RAY_LAST_CHECK_AT', $v2ray[$i]['parent_id']));
|
|
$v2ray[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_V2RAY_LAST_CHECK_AT', $v2ray[$i]['parent_id']));
|
|
@@ -50,7 +50,7 @@ class ServerService
|
|
$trojan = $model->get();
|
|
$trojan = $model->get();
|
|
for ($i = 0; $i < count($trojan); $i++) {
|
|
for ($i = 0; $i < count($trojan); $i++) {
|
|
$trojan[$i]['type'] = 'trojan';
|
|
$trojan[$i]['type'] = 'trojan';
|
|
- $groupId = json_decode($trojan[$i]['group_id']);
|
|
|
|
|
|
+ $groupId = $trojan[$i]['group_id'];
|
|
if (in_array($user->group_id, $groupId)) {
|
|
if (in_array($user->group_id, $groupId)) {
|
|
if ($trojan[$i]['parent_id']) {
|
|
if ($trojan[$i]['parent_id']) {
|
|
$trojan[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojan[$i]['parent_id']));
|
|
$trojan[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $trojan[$i]['parent_id']));
|
|
@@ -73,7 +73,7 @@ class ServerService
|
|
$shadowsocks = $model->get();
|
|
$shadowsocks = $model->get();
|
|
for ($i = 0; $i < count($shadowsocks); $i++) {
|
|
for ($i = 0; $i < count($shadowsocks); $i++) {
|
|
$shadowsocks[$i]['type'] = 'shadowsocks';
|
|
$shadowsocks[$i]['type'] = 'shadowsocks';
|
|
- $groupId = json_decode($shadowsocks[$i]['group_id']);
|
|
|
|
|
|
+ $groupId = $shadowsocks[$i]['group_id'];
|
|
if (in_array($user->group_id, $groupId)) {
|
|
if (in_array($user->group_id, $groupId)) {
|
|
if ($shadowsocks[$i]['parent_id']) {
|
|
if ($shadowsocks[$i]['parent_id']) {
|
|
$shadowsocks[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $shadowsocks[$i]['parent_id']));
|
|
$shadowsocks[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $shadowsocks[$i]['parent_id']));
|
|
@@ -123,7 +123,7 @@ class ServerService
|
|
|
|
|
|
public function getV2RayConfig(int $nodeId, int $localPort)
|
|
public function getV2RayConfig(int $nodeId, int $localPort)
|
|
{
|
|
{
|
|
- $server = Server::find($nodeId);
|
|
|
|
|
|
+ $server = ServerV2ray::find($nodeId);
|
|
if (!$server) {
|
|
if (!$server) {
|
|
abort(500, '节点不存在');
|
|
abort(500, '节点不存在');
|
|
}
|
|
}
|
|
@@ -156,10 +156,10 @@ class ServerService
|
|
return $json;
|
|
return $json;
|
|
}
|
|
}
|
|
|
|
|
|
- private function setDns(Server $server, object $json)
|
|
|
|
|
|
+ private function setDns(ServerV2ray $server, object $json)
|
|
{
|
|
{
|
|
if ($server->dnsSettings) {
|
|
if ($server->dnsSettings) {
|
|
- $dns = json_decode($server->dnsSettings);
|
|
|
|
|
|
+ $dns = $server->dnsSettings;
|
|
if (isset($dns->servers)) {
|
|
if (isset($dns->servers)) {
|
|
array_push($dns->servers, '1.1.1.1');
|
|
array_push($dns->servers, '1.1.1.1');
|
|
array_push($dns->servers, 'localhost');
|
|
array_push($dns->servers, 'localhost');
|
|
@@ -169,41 +169,41 @@ class ServerService
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private function setNetwork(Server $server, object $json)
|
|
|
|
|
|
+ private function setNetwork(ServerV2ray $server, object $json)
|
|
{
|
|
{
|
|
if ($server->networkSettings) {
|
|
if ($server->networkSettings) {
|
|
switch ($server->network) {
|
|
switch ($server->network) {
|
|
case 'tcp':
|
|
case 'tcp':
|
|
- $json->inbound->streamSettings->tcpSettings = json_decode($server->networkSettings);
|
|
|
|
|
|
+ $json->inbound->streamSettings->tcpSettings = $server->networkSettings;
|
|
break;
|
|
break;
|
|
case 'kcp':
|
|
case 'kcp':
|
|
- $json->inbound->streamSettings->kcpSettings = json_decode($server->networkSettings);
|
|
|
|
|
|
+ $json->inbound->streamSettings->kcpSettings = $server->networkSettings;
|
|
break;
|
|
break;
|
|
case 'ws':
|
|
case 'ws':
|
|
- $json->inbound->streamSettings->wsSettings = json_decode($server->networkSettings);
|
|
|
|
|
|
+ $json->inbound->streamSettings->wsSettings = $server->networkSettings;
|
|
break;
|
|
break;
|
|
case 'http':
|
|
case 'http':
|
|
- $json->inbound->streamSettings->httpSettings = json_decode($server->networkSettings);
|
|
|
|
|
|
+ $json->inbound->streamSettings->httpSettings = $server->networkSettings;
|
|
break;
|
|
break;
|
|
case 'domainsocket':
|
|
case 'domainsocket':
|
|
- $json->inbound->streamSettings->dsSettings = json_decode($server->networkSettings);
|
|
|
|
|
|
+ $json->inbound->streamSettings->dsSettings = $server->networkSettings;
|
|
break;
|
|
break;
|
|
case 'quic':
|
|
case 'quic':
|
|
- $json->inbound->streamSettings->quicSettings = json_decode($server->networkSettings);
|
|
|
|
|
|
+ $json->inbound->streamSettings->quicSettings = $server->networkSettings;
|
|
break;
|
|
break;
|
|
case 'grpc':
|
|
case 'grpc':
|
|
- $json->inbound->streamSettings->grpcSettings = json_decode($server->networkSettings);
|
|
|
|
|
|
+ $json->inbound->streamSettings->grpcSettings = $server->networkSettings;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private function setRule(Server $server, object $json)
|
|
|
|
|
|
+ private function setRule(ServerV2ray $server, object $json)
|
|
{
|
|
{
|
|
$domainRules = array_filter(explode(PHP_EOL, config('v2board.server_v2ray_domain')));
|
|
$domainRules = array_filter(explode(PHP_EOL, config('v2board.server_v2ray_domain')));
|
|
$protocolRules = array_filter(explode(PHP_EOL, config('v2board.server_v2ray_protocol')));
|
|
$protocolRules = array_filter(explode(PHP_EOL, config('v2board.server_v2ray_protocol')));
|
|
if ($server->ruleSettings) {
|
|
if ($server->ruleSettings) {
|
|
- $ruleSettings = json_decode($server->ruleSettings);
|
|
|
|
|
|
+ $ruleSettings = $server->ruleSettings;
|
|
// domain
|
|
// domain
|
|
if (isset($ruleSettings->domain)) {
|
|
if (isset($ruleSettings->domain)) {
|
|
$ruleSettings->domain = array_filter($ruleSettings->domain);
|
|
$ruleSettings->domain = array_filter($ruleSettings->domain);
|
|
@@ -238,10 +238,10 @@ class ServerService
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private function setTls(Server $server, object $json)
|
|
|
|
|
|
+ private function setTls(ServerV2ray $server, object $json)
|
|
{
|
|
{
|
|
if ((int)$server->tls) {
|
|
if ((int)$server->tls) {
|
|
- $tlsSettings = json_decode($server->tlsSettings);
|
|
|
|
|
|
+ $tlsSettings = $server->tlsSettings;
|
|
$json->inbound->streamSettings->security = 'tls';
|
|
$json->inbound->streamSettings->security = 'tls';
|
|
$tls = (object)[
|
|
$tls = (object)[
|
|
'certificateFile' => '/root/.cert/server.crt',
|
|
'certificateFile' => '/root/.cert/server.crt',
|
|
@@ -260,20 +260,23 @@ class ServerService
|
|
|
|
|
|
public function log(int $userId, int $serverId, int $u, int $d, float $rate, string $method)
|
|
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'));
|
|
|
|
|
|
+ if (($u + $d) < 10240) return true;
|
|
|
|
+ $timestamp = strtotime(date('Y-m-d'));
|
|
$serverLog = ServerLog::where('log_at', '>=', $timestamp)
|
|
$serverLog = ServerLog::where('log_at', '>=', $timestamp)
|
|
->where('log_at', '<', $timestamp + 3600)
|
|
->where('log_at', '<', $timestamp + 3600)
|
|
->where('server_id', $serverId)
|
|
->where('server_id', $serverId)
|
|
->where('user_id', $userId)
|
|
->where('user_id', $userId)
|
|
->where('rate', $rate)
|
|
->where('rate', $rate)
|
|
->where('method', $method)
|
|
->where('method', $method)
|
|
- ->lockForUpdate()
|
|
|
|
->first();
|
|
->first();
|
|
if ($serverLog) {
|
|
if ($serverLog) {
|
|
- $serverLog->u = $serverLog->u + $u;
|
|
|
|
- $serverLog->d = $serverLog->d + $d;
|
|
|
|
- $serverLog->save();
|
|
|
|
|
|
+ try {
|
|
|
|
+ $serverLog->increment('u', $u);
|
|
|
|
+ $serverLog->increment('d', $d);
|
|
|
|
+ return true;
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
$serverLog = new ServerLog();
|
|
$serverLog = new ServerLog();
|
|
$serverLog->user_id = $userId;
|
|
$serverLog->user_id = $userId;
|
|
@@ -283,7 +286,7 @@ class ServerService
|
|
$serverLog->rate = $rate;
|
|
$serverLog->rate = $rate;
|
|
$serverLog->log_at = $timestamp;
|
|
$serverLog->log_at = $timestamp;
|
|
$serverLog->method = $method;
|
|
$serverLog->method = $method;
|
|
- $serverLog->save();
|
|
|
|
|
|
+ return $serverLog->save();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -292,32 +295,15 @@ class ServerService
|
|
$server = ServerShadowsocks::orderBy('sort', 'ASC')->get();
|
|
$server = ServerShadowsocks::orderBy('sort', 'ASC')->get();
|
|
for ($i = 0; $i < count($server); $i++) {
|
|
for ($i = 0; $i < count($server); $i++) {
|
|
$server[$i]['type'] = 'shadowsocks';
|
|
$server[$i]['type'] = 'shadowsocks';
|
|
- if (!empty($server[$i]['tags'])) {
|
|
|
|
- $server[$i]['tags'] = json_decode($server[$i]['tags']);
|
|
|
|
- }
|
|
|
|
- $server[$i]['group_id'] = json_decode($server[$i]['group_id']);
|
|
|
|
}
|
|
}
|
|
return $server->toArray();
|
|
return $server->toArray();
|
|
}
|
|
}
|
|
|
|
|
|
public function getV2rayServers()
|
|
public function getV2rayServers()
|
|
{
|
|
{
|
|
- $server = Server::orderBy('sort', 'ASC')->get();
|
|
|
|
|
|
+ $server = ServerV2ray::orderBy('sort', 'ASC')->get();
|
|
for ($i = 0; $i < count($server); $i++) {
|
|
for ($i = 0; $i < count($server); $i++) {
|
|
$server[$i]['type'] = 'v2ray';
|
|
$server[$i]['type'] = 'v2ray';
|
|
- if (!empty($server[$i]['tags'])) {
|
|
|
|
- $server[$i]['tags'] = json_decode($server[$i]['tags']);
|
|
|
|
- }
|
|
|
|
- if (!empty($server[$i]['dnsSettings'])) {
|
|
|
|
- $server[$i]['dnsSettings'] = json_decode($server[$i]['dnsSettings']);
|
|
|
|
- }
|
|
|
|
- if (!empty($server[$i]['tlsSettings'])) {
|
|
|
|
- $server[$i]['tlsSettings'] = json_decode($server[$i]['tlsSettings']);
|
|
|
|
- }
|
|
|
|
- if (!empty($server[$i]['ruleSettings'])) {
|
|
|
|
- $server[$i]['ruleSettings'] = json_decode($server[$i]['ruleSettings']);
|
|
|
|
- }
|
|
|
|
- $server[$i]['group_id'] = json_decode($server[$i]['group_id']);
|
|
|
|
}
|
|
}
|
|
return $server->toArray();
|
|
return $server->toArray();
|
|
}
|
|
}
|
|
@@ -327,10 +313,6 @@ class ServerService
|
|
$server = ServerTrojan::orderBy('sort', 'ASC')->get();
|
|
$server = ServerTrojan::orderBy('sort', 'ASC')->get();
|
|
for ($i = 0; $i < count($server); $i++) {
|
|
for ($i = 0; $i < count($server); $i++) {
|
|
$server[$i]['type'] = 'trojan';
|
|
$server[$i]['type'] = 'trojan';
|
|
- if (!empty($server[$i]['tags'])) {
|
|
|
|
- $server[$i]['tags'] = json_decode($server[$i]['tags']);
|
|
|
|
- }
|
|
|
|
- $server[$i]['group_id'] = json_decode($server[$i]['group_id']);
|
|
|
|
}
|
|
}
|
|
return $server->toArray();
|
|
return $server->toArray();
|
|
}
|
|
}
|