Merge pull request #215 from bjdgyc/dev

修复ip分配的bug
This commit is contained in:
bjdgyc 2023-01-13 11:47:52 +08:00 committed by GitHub
commit 8ab46e3279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 58 deletions

View File

@ -3,5 +3,5 @@ package base
const ( const (
APP_NAME = "AnyLink" APP_NAME = "AnyLink"
// app版本号 // app版本号
APP_VER = "0.9.2-beta1" APP_VER = "0.9.2-beta2"
) )

View File

@ -8,6 +8,7 @@ import (
"net" "net"
"reflect" "reflect"
"regexp" "regexp"
"strconv"
"time" "time"
"github.com/go-ldap/ldap" "github.com/go-ldap/ldap"
@ -117,6 +118,10 @@ func (auth AuthLdap) checkUser(name, pwd string, g *Group) error {
} }
return fmt.Errorf("LDAP发现 %s 用户,存在多个账号", name) return fmt.Errorf("LDAP发现 %s 用户,存在多个账号", name)
} }
err = parseEntries(sr)
if err != nil {
return fmt.Errorf("LDAP %s 用户 %s", name, err.Error())
}
userDN := sr.Entries[0].DN userDN := sr.Entries[0].DN
err = l.Bind(userDN, pwd) err = l.Bind(userDN, pwd)
if err != nil { if err != nil {
@ -125,6 +130,32 @@ func (auth AuthLdap) checkUser(name, pwd string, g *Group) error {
return nil return nil
} }
func parseEntries(sr *ldap.SearchResult) error {
for _, attr := range sr.Entries[0].Attributes {
switch attr.Name {
case "shadowExpire":
// -1 启用, 1 停用, >1 从1970-01-01至到期日的天数
val, _ := strconv.ParseInt(attr.Values[0], 10, 64)
if val == -1 {
return nil
}
if val == 1 {
return fmt.Errorf("账号已停用")
}
if val > 1 {
expireTime := time.Unix(val*86400, 0)
t := time.Date(expireTime.Year(), expireTime.Month(), expireTime.Day(), 23, 59, 59, 0, time.Local)
if t.Before(time.Now()) {
return fmt.Errorf("账号已过期(过期日期: %s)", t.Format("2006-01-02"))
}
return nil
}
return fmt.Errorf("账号shadowExpire值异常: %d", val)
}
}
return nil
}
func ValidateDomainPort(addr string) bool { func ValidateDomainPort(addr string) bool {
re := regexp.MustCompile(`^([a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+[A-Za-z]{2,18}\:([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-5]{2}[0-3][0-5])$`) re := regexp.MustCompile(`^([a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+[A-Za-z]{2,18}\:([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-5]{2}[0-3][0-5])$`)
return re.MatchString(addr) return re.MatchString(addr)

View File

@ -56,7 +56,7 @@ func sniNewParser(b []byte) (uint8, string) {
sessionIDLength := int(rest[current]) sessionIDLength := int(rest[current])
current += 1 current += 1
current += sessionIDLength current += sessionIDLength
if current >= restLen { if current+1 >= restLen {
return acc_proto_https, "" return acc_proto_https, ""
} }
cipherSuiteLength := (int(rest[current]) << 8) + int(rest[current+1]) cipherSuiteLength := (int(rest[current]) << 8) + int(rest[current+1])

View File

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