path.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SiYuan - Build Your Eternal Digital Garden
  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 util
  17. import (
  18. "bytes"
  19. "net"
  20. "os"
  21. "os/exec"
  22. "path"
  23. "strings"
  24. "github.com/88250/gulu"
  25. "github.com/siyuan-note/logging"
  26. )
  27. var (
  28. SSL = false
  29. UserAgent = "SiYuan/" + Ver
  30. )
  31. const (
  32. ServerPort = "6806" // HTTP/WebSocket 端口
  33. AliyunServer = "https://siyuan-sync.b3logfile.com" // 云端服务地址,阿里云负载均衡,用于接口,数据同步文件上传、下载会走七牛云 OSS http://siyuan-data.b3logfile.com
  34. BazaarStatServer = "http://bazaar.b3logfile.com" // 集市包统计服务地址,直接对接 Bucket 没有 CDN 缓存
  35. BazaarOSSServer = "https://oss.b3logfile.com" // 云端对象存储地址,七牛云,仅用于读取小文件(比如配置 json),不用于读取包内容(如果是订阅会员则用于读取包内容)
  36. BazaarOSSFileServer = "https://oss0.b3logfile.com" // 云端对象存储文件服务地址,Cloudflare,用于读取包内容
  37. )
  38. func ShortPathForBootingDisplay(p string) string {
  39. if 25 > len(p) {
  40. return p
  41. }
  42. p = strings.TrimSuffix(p, ".sy")
  43. p = path.Base(p)
  44. return p
  45. }
  46. func IsIDPattern(str string) bool {
  47. if len("20060102150405-1a2b3c4") != len(str) {
  48. return false
  49. }
  50. if 1 != strings.Count(str, "-") {
  51. return false
  52. }
  53. parts := strings.Split(str, "-")
  54. idPart := parts[0]
  55. if 14 != len(idPart) {
  56. return false
  57. }
  58. for _, c := range idPart {
  59. if !('0' <= c && '9' >= c) {
  60. return false
  61. }
  62. }
  63. randPart := parts[1]
  64. if 7 != len(randPart) {
  65. return false
  66. }
  67. for _, c := range randPart {
  68. if !('a' <= c && 'z' >= c) && !('0' <= c && '9' >= c) {
  69. return false
  70. }
  71. }
  72. return true
  73. }
  74. var LocalIPs []string
  75. func GetLocalIPs() (ret []string) {
  76. if "android" == Container {
  77. // Android 上用不了 net.InterfaceAddrs() https://github.com/golang/go/issues/40569,所以前面使用启动内核传入的参数 localIPs
  78. LocalIPs = append(LocalIPs, "127.0.0.1")
  79. LocalIPs = gulu.Str.RemoveDuplicatedElem(LocalIPs)
  80. return LocalIPs
  81. }
  82. ret = []string{}
  83. addrs, err := net.InterfaceAddrs()
  84. if nil != err {
  85. logging.LogWarnf("get interface addresses failed: %s", err)
  86. return
  87. }
  88. for _, addr := range addrs {
  89. if networkIp, ok := addr.(*net.IPNet); ok && !networkIp.IP.IsLoopback() && networkIp.IP.To4() != nil &&
  90. bytes.Equal([]byte{255, 255, 255, 0}, networkIp.Mask) {
  91. ret = append(ret, networkIp.IP.String())
  92. }
  93. }
  94. ret = append(ret, "127.0.0.1")
  95. ret = gulu.Str.RemoveDuplicatedElem(ret)
  96. return
  97. }
  98. func isRunningInDockerContainer() bool {
  99. if _, runInContainer := os.LookupEnv("RUN_IN_CONTAINER"); runInContainer {
  100. return true
  101. }
  102. if _, err := os.Stat("/.dockerenv"); err == nil {
  103. return true
  104. }
  105. return false
  106. }
  107. func IsRelativePath(dest string) bool {
  108. if 1 > len(dest) {
  109. return true
  110. }
  111. if '/' == dest[0] {
  112. return false
  113. }
  114. return !strings.Contains(dest, ":/") && !strings.Contains(dest, ":\\")
  115. }
  116. func TimeFromID(id string) (ret string) {
  117. ret = id[:14]
  118. return
  119. }
  120. func IsValidPandocBin(binPath string) bool {
  121. if "" == binPath {
  122. return false
  123. }
  124. cmd := exec.Command(binPath, "--version")
  125. CmdAttr(cmd)
  126. data, err := cmd.CombinedOutput()
  127. if nil == err && strings.HasPrefix(string(data), "pandoc") {
  128. return true
  129. }
  130. return false
  131. }