mirror of https://github.com/bjdgyc/anylink.git
修复ip分配的bug
This commit is contained in:
parent
273552ddfe
commit
29953911da
|
@ -49,7 +49,7 @@ func initIpPool() {
|
|||
IpPool.IpLongMax = utils.Ip2long(net.ParseIP(base.Cfg.Ipv4End))
|
||||
|
||||
// 获取IpLease数据
|
||||
go cronIpLease()
|
||||
//go cronIpLease()
|
||||
}
|
||||
|
||||
func cronIpLease() {
|
||||
|
@ -86,15 +86,22 @@ func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
|
|||
var (
|
||||
err error
|
||||
tNow = time.Now()
|
||||
sNow = time.Now().Add(-1 * time.Duration(base.Cfg.IpLease) * time.Second)
|
||||
leaseTime = time.Now().Add(-1 * time.Duration(base.Cfg.IpLease) * time.Second)
|
||||
)
|
||||
|
||||
if uniqueMac {
|
||||
// 判断是否已经分配过
|
||||
mi := &dbdata.IpMap{}
|
||||
err = dbdata.One("mac_addr", macAddr, mi)
|
||||
// 查询报错
|
||||
if err != nil {
|
||||
if !dbdata.CheckErrNotFound(err) {
|
||||
base.Error(err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// 存在ip记录
|
||||
if err == nil {
|
||||
ipStr := mi.IpAddr
|
||||
ip := net.ParseIP(ipStr)
|
||||
// 跳过活跃连接
|
||||
|
@ -112,11 +119,18 @@ func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
|
|||
return ip
|
||||
}
|
||||
_ = dbdata.Del(mi)
|
||||
}
|
||||
|
||||
} else {
|
||||
ipMaps := []dbdata.IpMap{}
|
||||
err = dbdata.FindWhere(&ipMaps, 50, 1, "username=? and unique_mac=?", username, false)
|
||||
if err == nil {
|
||||
// 查询报错
|
||||
if err != nil {
|
||||
if !dbdata.CheckErrNotFound(err) {
|
||||
base.Error(err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
//遍历mac记录
|
||||
for _, mi := range ipMaps {
|
||||
ipStr := mi.IpAddr
|
||||
|
@ -126,12 +140,13 @@ func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
|
|||
if _, ok := ipActive[ipStr]; ok {
|
||||
continue
|
||||
}
|
||||
// 跳过ip租期内数据
|
||||
if _, ok := ipLease[ipStr]; ok {
|
||||
//跳过保留ip
|
||||
if mi.Keep {
|
||||
continue
|
||||
}
|
||||
|
||||
if IpPool.Ipv4IPNet.Contains(ip) &&
|
||||
mi.LastLogin.Before(leaseTime) && // 说明已经超过租期,可以直接使用
|
||||
utils.Ip2long(ip) >= IpPool.IpLongMin &&
|
||||
utils.Ip2long(ip) <= IpPool.IpLongMax {
|
||||
mi.LastLogin = tNow
|
||||
|
@ -144,7 +159,6 @@ func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 全局遍历超过租期和未保留的ip
|
||||
for i := IpPool.IpLongMin; i <= IpPool.IpLongMax; i++ {
|
||||
|
@ -155,14 +169,15 @@ func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
|
|||
if _, ok := ipActive[ipStr]; ok {
|
||||
continue
|
||||
}
|
||||
// 跳过ip租期内数据
|
||||
if _, ok := ipLease[ipStr]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
mi := &dbdata.IpMap{}
|
||||
err = dbdata.One("ip_addr", ipStr, mi)
|
||||
if err == nil && mi.LastLogin.Before(sNow) {
|
||||
if err == nil {
|
||||
//跳过保留ip
|
||||
if mi.Keep {
|
||||
continue
|
||||
}
|
||||
if mi.LastLogin.Before(leaseTime) {
|
||||
// 存在记录,说明已经超过租期,可以直接使用
|
||||
mi.LastLogin = tNow
|
||||
mi.MacAddr = macAddr
|
||||
|
@ -172,6 +187,7 @@ func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
|
|||
ipActive[ipStr] = true
|
||||
return ip
|
||||
}
|
||||
}
|
||||
|
||||
if dbdata.CheckErrNotFound(err) {
|
||||
// 该ip没有被使用
|
||||
|
@ -180,10 +196,13 @@ func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
|
|||
ipActive[ipStr] = true
|
||||
return ip
|
||||
}
|
||||
|
||||
// 查询报错
|
||||
if err != nil {
|
||||
base.Error(err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
base.Warn("no ip available, please see ip_map table row")
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue