Browse Source

:art: 桌面端内核进程使用随机端口 https://github.com/siyuan-note/siyuan/issues/4952

Liang Ding 2 years ago
parent
commit
bb90e72eee

+ 2 - 0
app/appearance/langs/en_US.json

@@ -1,4 +1,6 @@
 {
+  "useFixedPort": "Use Fixed Port",
+  "useFixedPortTip": "After enabling, the fixed port <code class='fn__code'>6806</code> will be used, and a random port will be used when it is disabled",
   "expandDown": "Expand Down",
   "expandUp": "Expand Up",
   "goForward": "Go forward",

+ 2 - 0
app/appearance/langs/es_ES.json

@@ -1,4 +1,6 @@
 {
+  "useFixedPort": "Usar puerto fijo",
+  "useFixedPortTip": "Después de habilitar, se usará el puerto fijo <code class='fn__code'>6806</code>, y se usará un puerto aleatorio cuando esté deshabilitado",
   "expandDown": "Expandir hacia abajo",
   "expandUp": "Expandir hacia arriba",
   "goForward": "Ir hacia adelante",

+ 2 - 0
app/appearance/langs/fr_FR.json

@@ -1,4 +1,6 @@
 {
+  "useFixedPort": "Utiliser un port fixe",
+  "useFixedPortTip": "Après l'activation, le port fixe <code class='fn__code'>6806</code> sera utilisé, et un port aléatoire sera utilisé lorsqu'il est désactivé",
   "expandDown": "Développer vers le bas",
   "expandUp": "Développer vers le haut",
   "goForward": "Suivant",

+ 2 - 0
app/appearance/langs/zh_CHT.json

@@ -1,4 +1,6 @@
 {
+  "useFixedPort": "使用固定端口",
+  "useFixedPortTip": "開啟後將使用固定端口 <code class='fn__code'>6806</code>,關閉時使用隨機端口",
   "expandDown": "向下擴選",
   "expandUp": "向上擴選",
   "goForward": "前進",

+ 2 - 0
app/appearance/langs/zh_CN.json

@@ -1,4 +1,6 @@
 {
+  "useFixedPort": "使用固定端口",
+  "useFixedPortTip": "开启后将使用固定端口 <code class='fn__code'>6806</code>,关闭时使用随机端口",
   "expandDown": "向下扩选",
   "expandUp": "向上扩选",
   "goForward": "前进",

+ 16 - 0
app/src/config/about.ts

@@ -40,6 +40,14 @@ export const about = {
     <div class="fn__space"></div>
     <input class="b3-switch fn__flex-center" id="networkServe" type="checkbox"${window.siyuan.config.system.networkServe ? " checked" : ""}>
 </label>
+<label class="b3-label fn__flex">
+    <div class="fn__flex-1">
+        ${window.siyuan.languages.useFixedPort}
+        <div class="b3-label__text">${window.siyuan.languages.useFixedPortTip}</div>
+    </div>
+    <div class="fn__space"></div>
+    <input class="b3-switch fn__flex-center" id="fixedPort" type="checkbox"${window.siyuan.config.system.fixedPort ? " checked" : ""}>
+</label>
 <label class="b3-label${isBrowser() ? " fn__none" : " fn__flex"}">
     <div class="fn__flex-1">
        ${window.siyuan.languages.about2}
@@ -348,6 +356,14 @@ export const about = {
                 });
             });
         });
+        const fixedPortElement = about.element.querySelector("#fixedPort") as HTMLInputElement;
+        fixedPortElement.addEventListener("change", () => {
+            fetchPost("/api/system/setFixedPort", {networkServe: fixedPortElement.checked}, () => {
+                exportLayout(false, () => {
+                    exitSiYuan();
+                });
+            });
+        });
         const uploadErrLogElement = about.element.querySelector("#uploadErrLog") as HTMLInputElement;
         uploadErrLogElement.addEventListener("change", () => {
             fetchPost("/api/system/setUploadErrLog", {uploadErrLog: uploadErrLogElement.checked}, () => {

+ 1 - 0
app/src/types/index.d.ts

@@ -341,6 +341,7 @@ declare interface IConfig {
         uploadErrLog: boolean
         downloadInstallPkg: boolean
         networkServe: boolean
+        fixedPort: boolean
         useExistingDB: boolean
     }
     localIPs: string[]

+ 1 - 0
kernel/api/router.go

@@ -39,6 +39,7 @@ func ServeAPI(ginServer *gin.Engine) {
 	ginServer.Handle("POST", "/api/system/getEmojiConf", model.CheckAuth, getEmojiConf)
 	ginServer.Handle("POST", "/api/system/setAccessAuthCode", model.CheckAuth, setAccessAuthCode)
 	ginServer.Handle("POST", "/api/system/setNetworkServe", model.CheckAuth, setNetworkServe)
+	ginServer.Handle("POST", "/api/system/setFixedPort", model.CheckAuth, setFixedPort)
 	ginServer.Handle("POST", "/api/system/setUploadErrLog", model.CheckAuth, setUploadErrLog)
 	ginServer.Handle("POST", "/api/system/setDownloadInstallPkg", model.CheckAuth, setDownloadInstallPkg)
 	ginServer.Handle("POST", "/api/system/setNetworkProxy", model.CheckAuth, setNetworkProxy)

+ 17 - 0
kernel/api/system.go

@@ -285,6 +285,23 @@ func setNetworkServe(c *gin.Context) {
 	time.Sleep(time.Second * 3)
 }
 
+func setFixedPort(c *gin.Context) {
+	ret := gulu.Ret.NewResult()
+	defer c.JSON(http.StatusOK, ret)
+
+	arg, ok := util.JsonArg(c, ret)
+	if !ok {
+		return
+	}
+
+	fixedPort := arg["fixedPort"].(bool)
+	model.Conf.FixedPort = fixedPort
+	model.Conf.Save()
+
+	util.PushMsg(model.Conf.Language(42), 1000*15)
+	time.Sleep(time.Second * 3)
+}
+
 func setUploadErrLog(c *gin.Context) {
 	ret := gulu.Ret.NewResult()
 	defer c.JSON(http.StatusOK, ret)

+ 5 - 0
kernel/model/conf.go

@@ -60,6 +60,7 @@ type AppConf struct {
 	User           *conf.User       `json:"-"`              // 社区用户内存结构,不持久化
 	Account        *conf.Account    `json:"account"`        // 帐号配置
 	ReadOnly       bool             `json:"readonly"`       // 是否是以只读模式运行
+	FixedPort      bool             `json:"fixedPort"`      // 是否使用固定端口 6806
 	LocalIPs       []string         `json:"localIPs"`       // 本地 IP 列表
 	AccessAuthCode string           `json:"accessAuthCode"` // 访问授权码
 	System         *conf.System     `json:"system"`         // 系统配置
@@ -271,6 +272,10 @@ func InitConf() {
 		Conf.AccessAuthCode = util.AccessAuthCode
 	}
 
+	if util.ContainerStd != util.Container {
+		Conf.FixedPort = true
+	}
+
 	Conf.LocalIPs = util.GetLocalIPs()
 
 	Conf.Save()