port.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package server
  17. import (
  18. "fmt"
  19. "net"
  20. "os"
  21. "os/exec"
  22. "path/filepath"
  23. "strconv"
  24. "strings"
  25. "time"
  26. "github.com/88250/gulu"
  27. goPS "github.com/mitchellh/go-ps"
  28. "github.com/siyuan-note/logging"
  29. "github.com/siyuan-note/siyuan/kernel/util"
  30. )
  31. func killRunningKernel() {
  32. defer logging.Recover()
  33. now := time.Now()
  34. defer logging.LogInfof("check running kernel elapsed [%dms]", time.Since(now).Milliseconds())
  35. processes, err := goPS.Processes()
  36. if nil != err {
  37. logging.LogErrorf("get processes failed: %s", err)
  38. killByPort(util.FixedPort)
  39. return
  40. }
  41. currentPid := os.Getpid()
  42. killed := false
  43. for _, process := range processes {
  44. if process.Pid() == currentPid {
  45. continue
  46. }
  47. procName := strings.ToLower(process.Executable())
  48. if strings.Contains(procName, "siyuan-kernel") {
  49. kill(fmt.Sprintf("%d", process.Pid()))
  50. killed = true
  51. }
  52. }
  53. if killed {
  54. portJSON := filepath.Join(util.HomeDir, ".config", "siyuan", "port.json")
  55. os.RemoveAll(portJSON)
  56. }
  57. }
  58. func killByPort(port string) {
  59. if !isPortOpen(port) {
  60. return
  61. }
  62. portJSON := filepath.Join(util.HomeDir, ".config", "siyuan", "port.json")
  63. os.RemoveAll(portJSON)
  64. pid := pidByPort(port)
  65. if "" == pid {
  66. return
  67. }
  68. pidInt, _ := strconv.Atoi(pid)
  69. proc, _ := goPS.FindProcess(pidInt)
  70. var name string
  71. if nil != proc {
  72. name = proc.Executable()
  73. }
  74. kill(pid)
  75. logging.LogInfof("killed process [name=%s, pid=%s]", name, pid)
  76. }
  77. func isPortOpen(port string) bool {
  78. timeout := time.Second
  79. conn, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", port), timeout)
  80. if nil != err {
  81. return false
  82. }
  83. if nil != conn {
  84. conn.Close()
  85. return true
  86. }
  87. return false
  88. }
  89. func kill(pid string) {
  90. var killCmd *exec.Cmd
  91. if gulu.OS.IsWindows() {
  92. killCmd = exec.Command("cmd", "/c", "TASKKILL /F /PID "+pid)
  93. } else {
  94. killCmd = exec.Command("kill", "-9", pid)
  95. }
  96. gulu.CmdAttr(killCmd)
  97. killCmd.CombinedOutput()
  98. }
  99. func pidByPort(port string) (ret string) {
  100. if gulu.OS.IsWindows() {
  101. cmd := exec.Command("cmd", "/c", "netstat -ano | findstr "+port)
  102. gulu.CmdAttr(cmd)
  103. data, err := cmd.CombinedOutput()
  104. if nil != err {
  105. logging.LogErrorf("netstat failed: %s", err)
  106. return
  107. }
  108. output := string(data)
  109. lines := strings.Split(output, "\n")
  110. for _, l := range lines {
  111. if strings.Contains(l, "LISTENING") {
  112. l = l[strings.Index(l, "LISTENING")+len("LISTENING"):]
  113. l = strings.TrimSpace(l)
  114. ret = l
  115. return
  116. }
  117. }
  118. return
  119. }
  120. cmd := exec.Command("lsof", "-Fp", "-i", ":"+port)
  121. gulu.CmdAttr(cmd)
  122. data, err := cmd.CombinedOutput()
  123. if nil != err {
  124. logging.LogErrorf("lsof failed: %s", err)
  125. return
  126. }
  127. output := string(data)
  128. lines := strings.Split(output, "\n")
  129. for _, l := range lines {
  130. if strings.HasPrefix(l, "p") {
  131. l = l[1:]
  132. ret = l
  133. return
  134. }
  135. }
  136. return
  137. }