123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- // SiYuan - Build Your Eternal Digital Garden
- // Copyright (c) 2020-present, b3log.org
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- package util
- import (
- "bytes"
- "net"
- "os"
- "path"
- "strings"
- "time"
- "github.com/88250/gulu"
- "github.com/siyuan-note/logging"
- )
- var (
- SSL = false
- UserAgent = "SiYuan/" + Ver
- )
- const (
- AliyunServer = "https://siyuan-sync.b3logfile.com" // 云端服务地址,阿里云负载均衡,用于接口,数据同步文件上传、下载会走七牛云 OSS http://siyuan-data.b3logfile.com
- BazaarStatServer = "http://bazaar.b3logfile.com" // 集市包统计服务地址,直接对接 Bucket 没有 CDN 缓存
- BazaarOSSServer = "https://oss.b3logfile.com" // 云端对象存储地址,七牛云,仅用于读取集市包
- )
- func ShortPathForBootingDisplay(p string) string {
- if 25 > len(p) {
- return p
- }
- p = strings.TrimSuffix(p, ".sy")
- p = path.Base(p)
- return p
- }
- func IsIDPattern(str string) bool {
- if len("20060102150405-1a2b3c4") != len(str) {
- return false
- }
- if 1 != strings.Count(str, "-") {
- return false
- }
- parts := strings.Split(str, "-")
- idPart := parts[0]
- if 14 != len(idPart) {
- return false
- }
- for _, c := range idPart {
- if !('0' <= c && '9' >= c) {
- return false
- }
- }
- randPart := parts[1]
- if 7 != len(randPart) {
- return false
- }
- for _, c := range randPart {
- if !('a' <= c && 'z' >= c) && !('0' <= c && '9' >= c) {
- return false
- }
- }
- return true
- }
- var LocalIPs []string
- func GetLocalIPs() (ret []string) {
- if ContainerAndroid == Container {
- // Android 上用不了 net.InterfaceAddrs() https://github.com/golang/go/issues/40569,所以前面使用启动内核传入的参数 localIPs
- LocalIPs = append(LocalIPs, "127.0.0.1")
- LocalIPs = gulu.Str.RemoveDuplicatedElem(LocalIPs)
- return LocalIPs
- }
- ret = []string{}
- addrs, err := net.InterfaceAddrs()
- if nil != err {
- logging.LogWarnf("get interface addresses failed: %s", err)
- return
- }
- for _, addr := range addrs {
- if networkIp, ok := addr.(*net.IPNet); ok && !networkIp.IP.IsLoopback() && networkIp.IP.To4() != nil &&
- bytes.Equal([]byte{255, 255, 255, 0}, networkIp.Mask) {
- ret = append(ret, networkIp.IP.String())
- }
- }
- ret = append(ret, "127.0.0.1")
- ret = gulu.Str.RemoveDuplicatedElem(ret)
- return
- }
- func isRunningInDockerContainer() bool {
- if _, runInContainer := os.LookupEnv("RUN_IN_CONTAINER"); runInContainer {
- return true
- }
- if _, err := os.Stat("/.dockerenv"); err == nil {
- return true
- }
- return false
- }
- func IsRelativePath(dest string) bool {
- if 1 > len(dest) {
- return true
- }
- if '/' == dest[0] {
- return false
- }
- return !strings.Contains(dest, ":/") && !strings.Contains(dest, ":\\")
- }
- func TimeFromID(id string) (ret string) {
- if 14 > len(id) {
- logging.LogWarnf("invalid id [%s], stack [\n%s]", id, logging.ShortStack())
- return time.Now().Format("20060102150405")
- }
- ret = id[:14]
- return
- }
|