mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-07 20:23:03 +08:00
增加 macvtap 模式支持
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
package sessdata
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/bjdgyc/anylink/base"
|
||||
"github.com/bjdgyc/anylink/dbdata"
|
||||
"github.com/bjdgyc/anylink/pkg/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -43,19 +43,8 @@ func initIpPool() {
|
||||
// max := min | uint32(math.Pow(2, float64(32-one))-1)
|
||||
|
||||
// ip地址池
|
||||
IpPool.IpLongMin = ip2long(net.ParseIP(base.Cfg.Ipv4Start))
|
||||
IpPool.IpLongMax = ip2long(net.ParseIP(base.Cfg.Ipv4End))
|
||||
}
|
||||
|
||||
func long2ip(i uint32) net.IP {
|
||||
ip := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(ip, i)
|
||||
return ip
|
||||
}
|
||||
|
||||
func ip2long(ip net.IP) uint32 {
|
||||
ip = ip.To4()
|
||||
return binary.BigEndian.Uint32(ip)
|
||||
IpPool.IpLongMin = utils.Ip2long(net.ParseIP(base.Cfg.Ipv4Start))
|
||||
IpPool.IpLongMax = utils.Ip2long(net.ParseIP(base.Cfg.Ipv4End))
|
||||
}
|
||||
|
||||
// AcquireIp 获取动态ip
|
||||
@@ -90,7 +79,7 @@ func AcquireIp(username, macAddr string) net.IP {
|
||||
farIp := &dbdata.IpMap{LastLogin: tNow}
|
||||
// 全局遍历超过租期ip
|
||||
for i := IpPool.IpLongMin; i <= IpPool.IpLongMax; i++ {
|
||||
ip := long2ip(i)
|
||||
ip := utils.Long2ip(i)
|
||||
ipStr := ip.String()
|
||||
|
||||
// 跳过活跃连接
|
||||
|
@@ -54,13 +54,13 @@ func OnlineSess() []Online {
|
||||
Group: v.Group,
|
||||
MacAddr: v.MacAddr,
|
||||
RemoteAddr: v.CSess.RemoteAddr,
|
||||
TunName: v.CSess.TunName,
|
||||
TunName: v.CSess.IfName,
|
||||
Mtu: v.CSess.Mtu,
|
||||
Client: v.CSess.Client,
|
||||
BandwidthUp: utils.HumanByte(atomic.LoadUint32(&v.CSess.BandwidthUpPeriod)) + "/s",
|
||||
BandwidthDown: utils.HumanByte(atomic.LoadUint32(&v.CSess.BandwidthDownPeriod)) + "/s",
|
||||
BandwidthUpAll: utils.HumanByte(atomic.LoadUint32(&v.CSess.BandwidthUpAll)),
|
||||
BandwidthDownAll: utils.HumanByte(atomic.LoadUint32(&v.CSess.BandwidthDownAll)),
|
||||
BandwidthUpAll: utils.HumanByte(atomic.LoadUint64(&v.CSess.BandwidthUpAll)),
|
||||
BandwidthDownAll: utils.HumanByte(atomic.LoadUint64(&v.CSess.BandwidthDownAll)),
|
||||
LastLogin: v.LastLogin,
|
||||
}
|
||||
datas = append(datas, val)
|
||||
|
@@ -32,7 +32,7 @@ type ConnSession struct {
|
||||
MacHw net.HardwareAddr // 客户端mac地址,从Session取出
|
||||
RemoteAddr string
|
||||
Mtu int
|
||||
TunName string
|
||||
IfName string
|
||||
Client string // 客户端 mobile pc
|
||||
CstpDpd int
|
||||
Group *dbdata.Group
|
||||
@@ -41,8 +41,8 @@ type ConnSession struct {
|
||||
BandwidthDown uint32 // 使用下行带宽 Byte
|
||||
BandwidthUpPeriod uint32 // 前一周期的总量
|
||||
BandwidthDownPeriod uint32
|
||||
BandwidthUpAll uint32 // 使用上行带宽总量
|
||||
BandwidthDownAll uint32 // 使用下行带宽总量
|
||||
BandwidthUpAll uint64 // 使用上行带宽总量
|
||||
BandwidthDownAll uint64 // 使用下行带宽总量
|
||||
closeOnce sync.Once
|
||||
CloseChan chan struct{}
|
||||
PayloadIn chan *Payload
|
||||
@@ -111,9 +111,9 @@ func checkSession() {
|
||||
|
||||
func GenToken() string {
|
||||
// 生成32位的 token
|
||||
btoken := make([]byte, 32)
|
||||
rand.Read(btoken)
|
||||
return fmt.Sprintf("%x", btoken)
|
||||
bToken := make([]byte, 32)
|
||||
rand.Read(bToken)
|
||||
return fmt.Sprintf("%x", bToken)
|
||||
}
|
||||
|
||||
func NewSession(token string) *Session {
|
||||
@@ -285,8 +285,8 @@ func (cs *ConnSession) ratePeriod() {
|
||||
atomic.SwapUint32(&cs.BandwidthUpPeriod, rtUp/BandwidthPeriodSec)
|
||||
atomic.SwapUint32(&cs.BandwidthDownPeriod, rtDown/BandwidthPeriodSec)
|
||||
// 累加所有流量
|
||||
atomic.AddUint32(&cs.BandwidthUpAll, rtUp)
|
||||
atomic.AddUint32(&cs.BandwidthDownAll, rtDown)
|
||||
atomic.AddUint64(&cs.BandwidthUpAll, uint64(rtUp))
|
||||
atomic.AddUint64(&cs.BandwidthDownAll, uint64(rtDown))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,10 +305,10 @@ func (cs *ConnSession) SetMtu(mtu string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (cs *ConnSession) SetTunName(name string) {
|
||||
func (cs *ConnSession) SetIfName(name string) {
|
||||
cs.Sess.mux.Lock()
|
||||
defer cs.Sess.mux.Unlock()
|
||||
cs.TunName = name
|
||||
cs.IfName = name
|
||||
}
|
||||
|
||||
func (cs *ConnSession) RateLimit(byt int, isUp bool) error {
|
||||
|
Reference in New Issue
Block a user