Browse Source

opt 1.2.3

Tokumeikoi 5 years ago
parent
commit
5b317478c6

+ 5 - 59
app/Http/Controllers/Server/DeepbworkController.php

@@ -101,65 +101,11 @@ class DeepbworkController extends Controller
         if (empty($nodeId) || empty($localPort)) {
             abort(500, '参数错误');
         }
-        $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;
-        if ($server->settings) {
-            switch ($server->network) {
-                case 'tcp':
-                    $json->inbound->streamSettings->tcpSettings = json_decode($server->settings);
-                    break;
-                case 'kcp':
-                    $json->inbound->streamSettings->kcpSettings = json_decode($server->settings);
-                    break;
-                case 'ws':
-                    $json->inbound->streamSettings->wsSettings = json_decode($server->settings);
-                    break;
-                case 'http':
-                    $json->inbound->streamSettings->httpSettings = json_decode($server->settings);
-                    break;
-                case 'domainsocket':
-                    $json->inbound->streamSettings->dsSettings = json_decode($server->settings);
-                    break;
-                case 'quic':
-                    $json->inbound->streamSettings->quicSettings = json_decode($server->settings);
-                    break;
-            }
-        }
-
-        if ($server->rules) {
-            $rules = json_decode($server->rules);
-            // domain
-            if (isset($rules->domain) && !empty($rules->domain)) {
-                $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)) {
-                $protocolObj = new \StdClass();
-                $protocolObj->type = 'field';
-                $protocolObj->protocol = $rules->protocol;
-                $protocolObj->outboundTag = 'block';
-                array_push($json->routing->rules, $protocolObj);
-            }
-        }
-
-        if ((int)$server->tls) {
-            $json->inbound->streamSettings->security = 'tls';
-            $tls = (object)[
-                'certificateFile' => '/home/v2ray.crt',
-                'keyFile' => '/home/v2ray.key'
-            ];
-            $json->inbound->streamSettings->tlsSettings = new \StdClass();
-            $json->inbound->streamSettings->tlsSettings->certificates[0] = $tls;
+        $serverService = new ServerService();
+        try {
+            $json = $serverService->getConfig($nodeId, $localPort);
+        } catch (\Exception $e) {
+            abort(500, $e->getMessage());
         }
 
         die(json_encode($json, JSON_UNESCAPED_UNICODE));

+ 2 - 2
app/Http/Requests/Admin/ServerSave.php

@@ -7,7 +7,6 @@ use Illuminate\Foundation\Http\FormRequest;
 class ServerSave extends FormRequest
 {
     CONST RULES = [
-        'rules' => '',
         'show' => '',
         'name' => 'required',
         'group_id' => 'required|array',
@@ -19,7 +18,8 @@ class ServerSave extends FormRequest
         'tags' => 'nullable|array',
         'rate' => 'required|numeric',
         'network' => 'required|in:tcp,kcp,ws,http,domainsocket,quic',
-        'settings' => ''
+        'networkSettings' => '',
+        'ruleSettings' => ''
     ];
     /**
      * Get the validation rules that apply to the request.

+ 70 - 0
app/Services/ServerService.php

@@ -3,9 +3,13 @@
 namespace App\Services;
 
 use App\Models\User;
+use App\Models\Server;
 
 class ServerService
 {
+
+    CONST SERVER_CONFIG = '{"api":{"services":["HandlerService","StatsService"],"tag":"api"},"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}}}}';
+
     public function getAvailableUsers($groupId)
     {
         return User::whereIn('group_id', $groupId)
@@ -28,4 +32,70 @@ class ServerService
             ])
             ->get();
     }
+
+    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;
+        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;
+            }
+        }
+
+        if ($server->ruleSettings) {
+            $rules = json_decode($server->ruleSettings);
+            // domain
+            if (isset($rules->domain) && !empty($rules->domain)) {
+                $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)) {
+                $protocolObj = new \StdClass();
+                $protocolObj->type = 'field';
+                $protocolObj->protocol = $rules->protocol;
+                $protocolObj->outboundTag = 'block';
+                array_push($json->routing->rules, $protocolObj);
+            }
+        }
+
+        if ((int)$server->tls) {
+            $json->inbound->streamSettings->security = 'tls';
+            $tls = (object)[
+                'certificateFile' => '/home/v2ray.crt',
+                'keyFile' => '/home/v2ray.key'
+            ];
+            $json->inbound->streamSettings->tlsSettings = new \StdClass();
+            $json->inbound->streamSettings->tlsSettings->certificates[0] = $tls;
+        }
+
+        return $json;
+    }
 }

+ 12 - 0
database/update.sql

@@ -185,3 +185,15 @@ CHANGE `expired_at` `expired_at` bigint(20) NULL DEFAULT '0' AFTER `token`;
 
 ALTER TABLE `v2_tutorial`
 DROP `icon`;
+
+ALTER TABLE `v2_server`
+CHANGE `settings` `networkSettings` text COLLATE 'utf8_general_ci' NULL AFTER `network`,
+CHANGE `rules` `ruleSettings` text COLLATE 'utf8_general_ci' NULL AFTER `networkSettings`;
+
+ALTER TABLE `v2_server`
+CHANGE `tags` `tags` varchar(255) COLLATE 'utf8_general_ci' NULL AFTER `server_port`,
+CHANGE `rate` `rate` varchar(11) COLLATE 'utf8_general_ci' NOT NULL AFTER `tags`,
+CHANGE `network` `network` varchar(11) COLLATE 'utf8_general_ci' NOT NULL AFTER `rate`,
+CHANGE `networkSettings` `networkSettings` text COLLATE 'utf8_general_ci' NULL AFTER `network`,
+CHANGE `tls` `tls` tinyint(4) NOT NULL DEFAULT '0' AFTER `networkSettings`,
+ADD `tlsSettings` text COLLATE 'utf8_general_ci' NULL AFTER `tls`;