dockerfiles/anylink/server/sessdata/ip_pool.go

274 lines
6.0 KiB
Go
Raw Normal View History

2021-06-08 20:45:26 +08:00
package sessdata
import (
"net"
"sync"
"time"
"github.com/bjdgyc/anylink/base"
"github.com/bjdgyc/anylink/dbdata"
2021-08-26 23:09:52 +08:00
"github.com/bjdgyc/anylink/pkg/utils"
2021-06-08 20:45:26 +08:00
)
var (
IpPool = &ipPoolConfig{}
ipActive = map[string]bool{}
2022-11-10 15:53:48 +08:00
// ipKeep and ipLease ipAddr => type
2023-04-26 22:17:10 +08:00
// ipLease = map[string]bool{}
2022-11-10 15:53:48 +08:00
ipPoolMux sync.Mutex
2023-04-26 22:17:10 +08:00
// 记录循环点
loopCurIp uint32
2021-06-08 20:45:26 +08:00
)
type ipPoolConfig struct {
// 计算动态ip
Ipv4Gateway net.IP
Ipv4Mask net.IP
Ipv4IPNet *net.IPNet
IpLongMin uint32
IpLongMax uint32
}
func initIpPool() {
// 地址处理
_, ipNet, err := net.ParseCIDR(base.Cfg.Ipv4CIDR)
if err != nil {
panic(err)
}
IpPool.Ipv4IPNet = ipNet
IpPool.Ipv4Mask = net.IP(ipNet.Mask)
2023-04-26 22:17:10 +08:00
ipv4Gateway := net.ParseIP(base.Cfg.Ipv4Gateway)
ipStart := net.ParseIP(base.Cfg.Ipv4Start)
ipEnd := net.ParseIP(base.Cfg.Ipv4End)
if !ipNet.Contains(ipv4Gateway) || !ipNet.Contains(ipStart) || !ipNet.Contains(ipEnd) {
panic("ip段 设置错误")
}
// ip地址池
IpPool.Ipv4Gateway = ipv4Gateway
IpPool.IpLongMin = utils.Ip2long(ipStart)
IpPool.IpLongMax = utils.Ip2long(ipEnd)
loopCurIp = IpPool.IpLongMin
2021-06-08 20:45:26 +08:00
// 网络地址零值
// zero := binary.BigEndian.Uint32(ip.Mask(mask))
// 广播地址
// one, _ := ipNet.Mask.Size()
// max := min | uint32(math.Pow(2, float64(32-one))-1)
2022-11-10 15:53:48 +08:00
// 获取IpLease数据
2023-04-26 22:17:10 +08:00
// go cronIpLease()
2022-11-10 15:53:48 +08:00
}
2023-04-26 22:17:10 +08:00
// func cronIpLease() {
// getIpLease()
// tick := time.NewTicker(time.Minute * 30)
// for range tick.C {
// getIpLease()
// }
// }
//
// func getIpLease() {
// xdb := dbdata.GetXdb()
// keepIpMaps := []dbdata.IpMap{}
// sNow := time.Now().Add(-1 * time.Duration(base.Cfg.IpLease) * time.Second)
// err := xdb.Cols("ip_addr").Where("keep=?", true).
// Or("unique_mac=? and last_login>?", true, sNow).Find(&keepIpMaps)
// if err != nil {
// base.Error(err)
// }
// // fmt.Println(keepIpMaps)
// ipPoolMux.Lock()
// ipLease = map[string]bool{}
// for _, v := range keepIpMaps {
// ipLease[v.IpAddr] = true
// }
// ipPoolMux.Unlock()
// }
2021-06-08 20:45:26 +08:00
2021-07-05 18:23:27 +08:00
// AcquireIp 获取动态ip
2023-04-26 22:17:10 +08:00
func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
base.Trace("AcquireIp:", username, macAddr, uniqueMac)
2022-11-10 15:53:48 +08:00
ipPoolMux.Lock()
defer ipPoolMux.Unlock()
2021-06-08 20:45:26 +08:00
2023-04-26 22:17:10 +08:00
var (
err error
tNow = time.Now()
)
2021-06-08 20:45:26 +08:00
2023-04-26 22:17:10 +08:00
if uniqueMac {
// 判断是否已经分配过
mi := &dbdata.IpMap{}
err = dbdata.One("mac_addr", macAddr, mi)
if err != nil {
// 没有查询到数据
if dbdata.CheckErrNotFound(err) {
return loopIp(username, macAddr, uniqueMac)
}
// 查询报错
base.Error(err)
return nil
}
// 存在ip记录
base.Trace("uniqueMac:", username, mi)
2021-08-02 20:41:35 +08:00
ipStr := mi.IpAddr
ip := net.ParseIP(ipStr)
2021-07-05 18:23:27 +08:00
// 跳过活跃连接
_, ok := ipActive[ipStr]
2021-06-08 20:45:26 +08:00
// 检测原有ip是否在新的ip池内
2023-04-26 22:17:10 +08:00
// IpPool.Ipv4IPNet.Contains(ip) &&
if !ok &&
2022-11-10 15:53:48 +08:00
utils.Ip2long(ip) >= IpPool.IpLongMin &&
utils.Ip2long(ip) <= IpPool.IpLongMax {
2021-06-08 20:45:26 +08:00
mi.Username = username
mi.LastLogin = tNow
2023-04-26 22:17:10 +08:00
mi.UniqueMac = uniqueMac
2021-06-08 20:45:26 +08:00
// 回写db数据
2022-11-10 15:53:48 +08:00
_ = dbdata.Set(mi)
2021-06-08 20:45:26 +08:00
ipActive[ipStr] = true
return ip
}
2023-04-26 22:17:10 +08:00
// 删除当前macAddr
mi = &dbdata.IpMap{MacAddr: macAddr}
2021-07-05 18:23:27 +08:00
_ = dbdata.Del(mi)
2023-04-26 22:17:10 +08:00
} else {
// 没有获取到mac的情况
ipMaps := []dbdata.IpMap{}
err = dbdata.FindWhere(&ipMaps, 50, 1, "username=? and unique_mac=?", username, false)
if err != nil {
// 没有查询到数据
if dbdata.CheckErrNotFound(err) {
return loopIp(username, macAddr, uniqueMac)
}
// 查询报错
base.Error(err)
return nil
}
// 遍历mac记录
for _, mi := range ipMaps {
ipStr := mi.IpAddr
ip := net.ParseIP(ipStr)
// 跳过活跃连接
if _, ok := ipActive[ipStr]; ok {
continue
}
// 跳过保留ip
if mi.Keep {
continue
}
// 没有mac的 不需要验证租期
// mi.LastLogin.Before(leaseTime) &&
if utils.Ip2long(ip) >= IpPool.IpLongMin &&
utils.Ip2long(ip) <= IpPool.IpLongMax {
mi.LastLogin = tNow
mi.MacAddr = macAddr
mi.UniqueMac = uniqueMac
// 回写db数据
_ = dbdata.Set(mi)
ipActive[ipStr] = true
return ip
}
}
2021-06-08 20:45:26 +08:00
}
2023-04-26 22:17:10 +08:00
return loopIp(username, macAddr, uniqueMac)
}
func loopIp(username, macAddr string, uniqueMac bool) net.IP {
var (
i uint32
ip net.IP
)
i, ip = loopLong(loopCurIp, IpPool.IpLongMax, username, macAddr, uniqueMac)
if ip != nil {
loopCurIp = i
return ip
}
i, ip = loopLong(IpPool.IpLongMin, loopCurIp, username, macAddr, uniqueMac)
if ip != nil {
loopCurIp = i
return ip
}
base.Warn("no ip available, please see ip_map table row", username, macAddr)
return nil
}
func loopLong(start, end uint32, username, macAddr string, uniqueMac bool) (uint32, net.IP) {
var (
err error
tNow = time.Now()
leaseTime = time.Now().Add(-1 * time.Duration(base.Cfg.IpLease) * time.Second)
)
2022-11-10 15:53:48 +08:00
// 全局遍历超过租期和未保留的ip
2023-04-26 22:17:10 +08:00
for i := start; i <= end; i++ {
2021-08-26 23:09:52 +08:00
ip := utils.Long2ip(i)
2021-06-08 20:45:26 +08:00
ipStr := ip.String()
// 跳过活跃连接
if _, ok := ipActive[ipStr]; ok {
continue
}
2023-04-26 22:17:10 +08:00
mi := &dbdata.IpMap{}
err = dbdata.One("ip_addr", ipStr, mi)
if err != nil {
// 没有查询到数据
if dbdata.CheckErrNotFound(err) {
// 该ip没有被使用
mi = &dbdata.IpMap{IpAddr: ipStr, MacAddr: macAddr, UniqueMac: uniqueMac, Username: username, LastLogin: tNow}
_ = dbdata.Add(mi)
ipActive[ipStr] = true
return i, ip
}
// 查询报错
base.Error(err)
return 0, nil
2022-11-10 15:53:48 +08:00
}
2021-06-08 20:45:26 +08:00
2023-04-26 22:17:10 +08:00
// 查询到已经使用的ip
// 跳过保留ip
if mi.Keep {
2021-06-08 20:45:26 +08:00
continue
}
2023-04-26 22:17:10 +08:00
// 判断租期
if mi.LastLogin.Before(leaseTime) {
// 存在记录,说明已经超过租期,可以直接使用
mi.LastLogin = tNow
mi.MacAddr = macAddr
mi.UniqueMac = uniqueMac
// 回写db数据
_ = dbdata.Set(mi)
2021-06-08 20:45:26 +08:00
ipActive[ipStr] = true
2023-04-26 22:17:10 +08:00
return i, ip
2021-06-08 20:45:26 +08:00
}
}
2023-04-26 22:17:10 +08:00
return 0, nil
2021-06-08 20:45:26 +08:00
}
// 回收ip
func ReleaseIp(ip net.IP, macAddr string) {
2022-11-10 15:53:48 +08:00
ipPoolMux.Lock()
defer ipPoolMux.Unlock()
2021-06-08 20:45:26 +08:00
delete(ipActive, ip.String())
2022-11-10 15:53:48 +08:00
2021-06-08 20:45:26 +08:00
mi := &dbdata.IpMap{}
2021-08-02 20:41:35 +08:00
err := dbdata.One("ip_addr", ip.String(), mi)
2021-06-08 20:45:26 +08:00
if err == nil {
mi.LastLogin = time.Now()
2022-11-10 15:53:48 +08:00
_ = dbdata.Set(mi)
2021-06-08 20:45:26 +08:00
}
}