From a20a19182b41cff6ea8edae2fddf3ea035fe7083 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 25 Oct 2022 10:06:15 +0800 Subject: [PATCH] =?UTF-8?q?:memo:=20=E5=86=85=E6=A0=B8=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20`--port`=20https://github.com/siyuan-note/?= =?UTF-8?q?siyuan/issues/6344?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/util/path.go | 1 - kernel/util/web.go | 20 ------------ kernel/util/working.go | 61 ++++------------------------------- kernel/util/working_mobile.go | 1 + 4 files changed, 7 insertions(+), 76 deletions(-) diff --git a/kernel/util/path.go b/kernel/util/path.go index 04dd913d6..05cab8ca7 100644 --- a/kernel/util/path.go +++ b/kernel/util/path.go @@ -34,7 +34,6 @@ var ( ) const ( - ServerPort = "0" // HTTP/WebSocket 端口 AliyunServer = "https://siyuan-sync.b3logfile.com" // 云端服务地址,阿里云负载均衡,用于接口,数据同步文件上传、下载会走七牛云 OSS http://siyuan-data.b3logfile.com BazaarStatServer = "http://bazaar.b3logfile.com" // 集市包统计服务地址,直接对接 Bucket 没有 CDN 缓存 BazaarOSSServer = "https://oss.b3logfile.com" // 云端对象存储地址,七牛云,仅用于读取集市包 diff --git a/kernel/util/web.go b/kernel/util/web.go index 616b47c25..77061df5d 100644 --- a/kernel/util/web.go +++ b/kernel/util/web.go @@ -24,7 +24,6 @@ import ( "github.com/88250/gulu" "github.com/gin-gonic/gin" "github.com/olahol/melody" - "github.com/siyuan-note/logging" ) func GetRemoteAddr(session *melody.Session) string { @@ -64,22 +63,3 @@ func isPortOpen(port string) bool { } 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 -} diff --git a/kernel/util/working.go b/kernel/util/working.go index 72dd02561..ab71d7755 100644 --- a/kernel/util/working.go +++ b/kernel/util/working.go @@ -60,6 +60,7 @@ func Boot() { wdPath := flag.String("wd", WorkingDir, "working directory of SiYuan") servePath := flag.String("servePath", "", "obsoleted https://github.com/siyuan-note/siyuan/issues/4647") _ = servePath + port := flag.String("port", "0", "port of the HTTP server") resident := flag.Bool("resident", true, "resident memory even if no active session") readOnly := flag.Bool("readonly", false, "read-only mode") accessAuthCode := flag.String("accessAuthCode", "", "access auth code") @@ -76,6 +77,10 @@ func Boot() { } Mode = *mode Resident = *resident + ServerPort = *port + if isRunningInDockerContainer() { + ServerPort = "6806" + } ReadOnly = *readOnly AccessAuthCode = *accessAuthCode Container = ContainerStd @@ -104,7 +109,6 @@ func Boot() { } initPathDir() - checkPort() go initPandoc() bootBanner := figure.NewColorFigure("SiYuan", "isometric3", "green", true) @@ -272,6 +276,7 @@ func initWorkspaceDir(workspaceArg string) { } var ( + ServerPort = "0" // HTTP/WebSocket 端口,0 为使用随机端口 Resident bool ReadOnly bool 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() { // 在某版本的 Windows 10 操作系统上界面样式异常问题 // https://github.com/siyuan-note/siyuan/issues/247 diff --git a/kernel/util/working_mobile.go b/kernel/util/working_mobile.go index e3beefc9b..82ae0c789 100644 --- a/kernel/util/working_mobile.go +++ b/kernel/util/working_mobile.go @@ -61,6 +61,7 @@ func BootMobile(container, appDir, workspaceDir, nativeLibDir, privateDataDir, l ThemesPath = filepath.Join(AppearancePath, "themes") IconsPath = filepath.Join(AppearancePath, "icons") Resident = true + ServerPort = "6806" Container = container UserAgent = UserAgent + " " + Container httpclient.SetUserAgent(UserAgent)