Просмотр исходного кода

:memo: 内核参数增加 `--port` https://github.com/siyuan-note/siyuan/issues/6344

Liang Ding 2 лет назад
Родитель
Сommit
a20a19182b
4 измененных файлов с 7 добавлено и 76 удалено
  1. 0 1
      kernel/util/path.go
  2. 0 20
      kernel/util/web.go
  3. 6 55
      kernel/util/working.go
  4. 1 0
      kernel/util/working_mobile.go

+ 0 - 1
kernel/util/path.go

@@ -34,7 +34,6 @@ var (
 )
 )
 
 
 const (
 const (
-	ServerPort       = "0"                                 // HTTP/WebSocket 端口
 	AliyunServer     = "https://siyuan-sync.b3logfile.com" // 云端服务地址,阿里云负载均衡,用于接口,数据同步文件上传、下载会走七牛云 OSS http://siyuan-data.b3logfile.com
 	AliyunServer     = "https://siyuan-sync.b3logfile.com" // 云端服务地址,阿里云负载均衡,用于接口,数据同步文件上传、下载会走七牛云 OSS http://siyuan-data.b3logfile.com
 	BazaarStatServer = "http://bazaar.b3logfile.com"       // 集市包统计服务地址,直接对接 Bucket 没有 CDN 缓存
 	BazaarStatServer = "http://bazaar.b3logfile.com"       // 集市包统计服务地址,直接对接 Bucket 没有 CDN 缓存
 	BazaarOSSServer  = "https://oss.b3logfile.com"         // 云端对象存储地址,七牛云,仅用于读取集市包
 	BazaarOSSServer  = "https://oss.b3logfile.com"         // 云端对象存储地址,七牛云,仅用于读取集市包

+ 0 - 20
kernel/util/web.go

@@ -24,7 +24,6 @@ import (
 	"github.com/88250/gulu"
 	"github.com/88250/gulu"
 	"github.com/gin-gonic/gin"
 	"github.com/gin-gonic/gin"
 	"github.com/olahol/melody"
 	"github.com/olahol/melody"
-	"github.com/siyuan-note/logging"
 )
 )
 
 
 func GetRemoteAddr(session *melody.Session) string {
 func GetRemoteAddr(session *melody.Session) string {
@@ -64,22 +63,3 @@ func isPortOpen(port string) bool {
 	}
 	}
 	return false
 	return false
 }
 }
-
-func tryToListenPort() bool {
-	listener, err := net.Listen("tcp", "127.0.0.1:"+ServerPort)
-	if nil != err {
-		time.Sleep(time.Second * 3)
-		listener, err = net.Listen("tcp", "127.0.0.1:"+ServerPort)
-		if nil != err {
-			logging.LogErrorf("try to listen port [%s] failed: %s", ServerPort, err)
-			return false
-		}
-	}
-	if err = listener.Close(); nil != err {
-		time.Sleep(time.Second * 1)
-		if err = listener.Close(); nil != err {
-			logging.LogErrorf("close listen port [%s] failed: %s", ServerPort, err)
-		}
-	}
-	return true
-}

+ 6 - 55
kernel/util/working.go

@@ -60,6 +60,7 @@ func Boot() {
 	wdPath := flag.String("wd", WorkingDir, "working directory of SiYuan")
 	wdPath := flag.String("wd", WorkingDir, "working directory of SiYuan")
 	servePath := flag.String("servePath", "", "obsoleted https://github.com/siyuan-note/siyuan/issues/4647")
 	servePath := flag.String("servePath", "", "obsoleted https://github.com/siyuan-note/siyuan/issues/4647")
 	_ = servePath
 	_ = servePath
+	port := flag.String("port", "0", "port of the HTTP server")
 	resident := flag.Bool("resident", true, "resident memory even if no active session")
 	resident := flag.Bool("resident", true, "resident memory even if no active session")
 	readOnly := flag.Bool("readonly", false, "read-only mode")
 	readOnly := flag.Bool("readonly", false, "read-only mode")
 	accessAuthCode := flag.String("accessAuthCode", "", "access auth code")
 	accessAuthCode := flag.String("accessAuthCode", "", "access auth code")
@@ -76,6 +77,10 @@ func Boot() {
 	}
 	}
 	Mode = *mode
 	Mode = *mode
 	Resident = *resident
 	Resident = *resident
+	ServerPort = *port
+	if isRunningInDockerContainer() {
+		ServerPort = "6806"
+	}
 	ReadOnly = *readOnly
 	ReadOnly = *readOnly
 	AccessAuthCode = *accessAuthCode
 	AccessAuthCode = *accessAuthCode
 	Container = ContainerStd
 	Container = ContainerStd
@@ -104,7 +109,6 @@ func Boot() {
 	}
 	}
 
 
 	initPathDir()
 	initPathDir()
-	checkPort()
 	go initPandoc()
 	go initPandoc()
 
 
 	bootBanner := figure.NewColorFigure("SiYuan", "isometric3", "green", true)
 	bootBanner := figure.NewColorFigure("SiYuan", "isometric3", "green", true)
@@ -272,6 +276,7 @@ func initWorkspaceDir(workspaceArg string) {
 }
 }
 
 
 var (
 var (
+	ServerPort     = "0" // HTTP/WebSocket 端口,0 为使用随机端口
 	Resident       bool
 	Resident       bool
 	ReadOnly       bool
 	ReadOnly       bool
 	AccessAuthCode string
 	AccessAuthCode string
@@ -320,60 +325,6 @@ func initPathDir() {
 	}
 	}
 }
 }
 
 
-func checkPort() {
-	portOpened := isPortOpen(ServerPort)
-	if !portOpened {
-		return
-	}
-
-	logging.LogInfof("port [%s] is opened, try to check version of running kernel", ServerPort)
-	result := NewResult()
-	_, err := httpclient.NewBrowserRequest().
-		SetResult(result).
-		SetHeader("User-Agent", UserAgent).
-		Get("http://127.0.0.1:" + ServerPort + "/api/system/version")
-	if nil != err || 0 != result.Code {
-		logging.LogErrorf("connect to port [%s] for checking running kernel failed", ServerPort)
-		KillByPort(ServerPort)
-		return
-	}
-
-	if nil == result.Data {
-		logging.LogErrorf("connect ot port [%s] for checking running kernel failed", ServerPort)
-		os.Exit(ExitCodeUnavailablePort)
-	}
-
-	runningVer := result.Data.(string)
-	if runningVer == Ver {
-		logging.LogInfof("version of the running kernel is the same as this boot [%s], exit this boot", runningVer)
-		os.Exit(ExitCodeOk)
-	}
-
-	logging.LogInfof("found kernel [%s] is running, try to exit it", runningVer)
-	processes, err := goPS.Processes()
-	if nil != err {
-		logging.LogErrorf("close kernel [%s] failed: %s", runningVer, err)
-		os.Exit(ExitCodeUnavailablePort)
-	}
-
-	currentPid := os.Getpid()
-	for _, p := range processes {
-		name := p.Executable()
-		if strings.Contains(strings.ToLower(name), "siyuan-kernel") || strings.Contains(strings.ToLower(name), "siyuan kernel") {
-			kernelPid := p.Pid()
-			if currentPid != kernelPid {
-				pid := strconv.Itoa(kernelPid)
-				Kill(pid)
-				logging.LogInfof("killed kernel [name=%s, pid=%s, ver=%s], continue to boot", name, pid, runningVer)
-			}
-		}
-	}
-
-	if !tryToListenPort() {
-		os.Exit(ExitCodeUnavailablePort)
-	}
-}
-
 func initMime() {
 func initMime() {
 	// 在某版本的 Windows 10 操作系统上界面样式异常问题
 	// 在某版本的 Windows 10 操作系统上界面样式异常问题
 	// https://github.com/siyuan-note/siyuan/issues/247
 	// https://github.com/siyuan-note/siyuan/issues/247

+ 1 - 0
kernel/util/working_mobile.go

@@ -61,6 +61,7 @@ func BootMobile(container, appDir, workspaceDir, nativeLibDir, privateDataDir, l
 	ThemesPath = filepath.Join(AppearancePath, "themes")
 	ThemesPath = filepath.Join(AppearancePath, "themes")
 	IconsPath = filepath.Join(AppearancePath, "icons")
 	IconsPath = filepath.Join(AppearancePath, "icons")
 	Resident = true
 	Resident = true
+	ServerPort = "6806"
 	Container = container
 	Container = container
 	UserAgent = UserAgent + " " + Container
 	UserAgent = UserAgent + " " + Container
 	httpclient.SetUserAgent(UserAgent)
 	httpclient.SetUserAgent(UserAgent)