path.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "path"
  22. "strings"
  23. "time"
  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 = "0" // 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" // 云端对象存储地址,七牛云,仅用于读取集市包
  36. )
  37. func ShortPathForBootingDisplay(p string) string {
  38. if 25 > len(p) {
  39. return p
  40. }
  41. p = strings.TrimSuffix(p, ".sy")
  42. p = path.Base(p)
  43. return p
  44. }
  45. func IsIDPattern(str string) bool {
  46. if len("20060102150405-1a2b3c4") != len(str) {
  47. return false
  48. }
  49. if 1 != strings.Count(str, "-") {
  50. return false
  51. }
  52. parts := strings.Split(str, "-")
  53. idPart := parts[0]
  54. if 14 != len(idPart) {
  55. return false
  56. }
  57. for _, c := range idPart {
  58. if !('0' <= c && '9' >= c) {
  59. return false
  60. }
  61. }
  62. randPart := parts[1]
  63. if 7 != len(randPart) {
  64. return false
  65. }
  66. for _, c := range randPart {
  67. if !('a' <= c && 'z' >= c) && !('0' <= c && '9' >= c) {
  68. return false
  69. }
  70. }
  71. return true
  72. }
  73. var LocalIPs []string
  74. func GetLocalIPs() (ret []string) {
  75. if ContainerAndroid == Container {
  76. // Android 上用不了 net.InterfaceAddrs() https://github.com/golang/go/issues/40569,所以前面使用启动内核传入的参数 localIPs
  77. LocalIPs = append(LocalIPs, "127.0.0.1")
  78. LocalIPs = gulu.Str.RemoveDuplicatedElem(LocalIPs)
  79. return LocalIPs
  80. }
  81. ret = []string{}
  82. addrs, err := net.InterfaceAddrs()
  83. if nil != err {
  84. logging.LogWarnf("get interface addresses failed: %s", err)
  85. return
  86. }
  87. for _, addr := range addrs {
  88. if networkIp, ok := addr.(*net.IPNet); ok && !networkIp.IP.IsLoopback() && networkIp.IP.To4() != nil &&
  89. bytes.Equal([]byte{255, 255, 255, 0}, networkIp.Mask) {
  90. ret = append(ret, networkIp.IP.String())
  91. }
  92. }
  93. ret = append(ret, "127.0.0.1")
  94. ret = gulu.Str.RemoveDuplicatedElem(ret)
  95. return
  96. }
  97. func isRunningInDockerContainer() bool {
  98. if _, runInContainer := os.LookupEnv("RUN_IN_CONTAINER"); runInContainer {
  99. return true
  100. }
  101. if _, err := os.Stat("/.dockerenv"); err == nil {
  102. return true
  103. }
  104. return false
  105. }
  106. func IsRelativePath(dest string) bool {
  107. if 1 > len(dest) {
  108. return true
  109. }
  110. if '/' == dest[0] {
  111. return false
  112. }
  113. return !strings.Contains(dest, ":/") && !strings.Contains(dest, ":\\")
  114. }
  115. func TimeFromID(id string) (ret string) {
  116. if 14 > len(id) {
  117. logging.LogWarnf("invalid id [%s], stack [\n%s]", id, logging.ShortStack())
  118. return time.Now().Format("20060102150405")
  119. }
  120. ret = id[:14]
  121. return
  122. }