65 lines
1022 B
Go
65 lines
1022 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/google/uuid"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type uuidUtil struct {
|
|
lock sync.Mutex
|
|
uuidint int
|
|
}
|
|
|
|
var Uuid = &uuidUtil{}
|
|
|
|
func (u *uuidUtil) CreateUUID() string {
|
|
u.lock.Lock()
|
|
defer u.lock.Unlock()
|
|
// 生成基于字符串的 UUID
|
|
key := fmt.Sprintln(IP.Get192Ip(), time.Now().Format("2006-01-02_15-04-05"), u.uuidint)
|
|
u.uuidint++
|
|
u1 := uuid.NewSHA1(uuid.Nil, []byte(key))
|
|
return u1.String()
|
|
}
|
|
|
|
type ipUtil struct {
|
|
Ip string
|
|
}
|
|
|
|
var IP = &ipUtil{}
|
|
|
|
func (i *ipUtil) Get192Ip() string {
|
|
if i.Ip == "" {
|
|
i.Ip = Get192Ip()
|
|
return i.Ip
|
|
}
|
|
return i.Ip
|
|
}
|
|
|
|
func Get192Ip() string {
|
|
interfaces, err := net.Interfaces()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
for _, iface := range interfaces {
|
|
addrs, err := iface.Addrs()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, addr := range addrs {
|
|
ip, _, _ := net.ParseCIDR(addr.String())
|
|
if ip != nil {
|
|
ipStr := ip.String()
|
|
if strings.HasPrefix(ipStr, "192") {
|
|
return ipStr
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|