update
This commit is contained in:
64
common.go
64
common.go
@@ -1,8 +1,13 @@
|
||||
package mycommon
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.makemake.in/kzkzzzz/mycommon/mylog"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func SafeFn(fn func()) {
|
||||
@@ -20,3 +25,62 @@ func SafeGo(fn func()) {
|
||||
SafeFn(fn)
|
||||
}()
|
||||
}
|
||||
|
||||
var (
|
||||
getIpOnce = &sync.Once{}
|
||||
outBoundIp = "127.0.0.1"
|
||||
)
|
||||
|
||||
// GetOutboundIP 获取本机ip
|
||||
func GetOutboundIP() string {
|
||||
getIpOnce.Do(func() {
|
||||
conn, err := net.Dial("udp", "223.5.5.5:53")
|
||||
if err != nil {
|
||||
log.Printf("get outbound ip err: %s", err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||
|
||||
if localAddr.IP != nil {
|
||||
outBoundIp = localAddr.IP.String()
|
||||
}
|
||||
})
|
||||
|
||||
return outBoundIp
|
||||
}
|
||||
|
||||
var (
|
||||
getHostNameOnce = &sync.Once{}
|
||||
hostName = "unknown"
|
||||
)
|
||||
|
||||
// GetHostName 获取hostname
|
||||
func GetHostName() string {
|
||||
getHostNameOnce.Do(func() {
|
||||
v, err := os.Hostname()
|
||||
if err != nil {
|
||||
log.Printf("get host name err: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
hostName = v
|
||||
})
|
||||
|
||||
return hostName
|
||||
}
|
||||
|
||||
func ExistFile(file string) bool {
|
||||
_, err := os.Stat(file)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return false
|
||||
}
|
||||
|
||||
// 其他错误
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user