优化数据

This commit is contained in:
bjdgyc 2022-09-30 16:03:43 +08:00
parent c2642bc183
commit e03bd60b1d
8 changed files with 27 additions and 15 deletions

View File

@ -1,9 +1,11 @@
package handler package handler
import ( import (
"crypto/md5"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io" "io"
"net"
"net/http" "net/http"
"strings" "strings"
"text/template" "text/template"
@ -87,6 +89,15 @@ func LinkAuth(w http.ResponseWriter, r *http.Request) {
sess.Group = cr.GroupSelect sess.Group = cr.GroupSelect
sess.MacAddr = strings.ToLower(cr.MacAddressList.MacAddress) sess.MacAddr = strings.ToLower(cr.MacAddressList.MacAddress)
sess.UniqueIdGlobal = cr.DeviceId.UniqueIdGlobal sess.UniqueIdGlobal = cr.DeviceId.UniqueIdGlobal
// 获取客户端mac地址
macHw, err := net.ParseMAC(sess.MacAddr)
if err != nil {
sum := md5.Sum([]byte(sess.UniqueIdGlobal))
macHw = sum[0:5] // 5个byte
macHw = append([]byte{0x02}, macHw...)
sess.MacAddr = macHw.String()
}
sess.MacHw = macHw
other := &dbdata.SettingOther{} other := &dbdata.SettingOther{}
_ = dbdata.SettingGet(other) _ = dbdata.SettingGet(other)
rd := RequestData{SessionId: sess.Sid, SessionToken: sess.Sid + "@" + sess.Token, rd := RequestData{SessionId: sess.Sid, SessionToken: sess.Sid + "@" + sess.Token,

View File

@ -12,6 +12,7 @@ import (
) )
func LinkCstp(conn net.Conn, bufRW *bufio.ReadWriter, cSess *sessdata.ConnSession) { func LinkCstp(conn net.Conn, bufRW *bufio.ReadWriter, cSess *sessdata.ConnSession) {
base.Debug("LinkCstp connect ip:", cSess.IpAddr, "user:", cSess.Username, "rip:", conn.RemoteAddr())
defer func() { defer func() {
base.Debug("LinkCstp return", cSess.IpAddr) base.Debug("LinkCstp return", cSess.IpAddr)
_ = conn.Close() _ = conn.Close()

View File

@ -10,7 +10,7 @@ import (
) )
func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) { func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
base.Debug("LinkDtls connect ip:", cSess.IpAddr, "udp-rip:", conn.RemoteAddr()) base.Debug("LinkDtls connect ip:", cSess.IpAddr, "user:", cSess.Username, "udp-rip:", conn.RemoteAddr())
dSess := cSess.NewDtlsConn() dSess := cSess.NewDtlsConn()
if dSess == nil { if dSess == nil {
// 创建失败,直接关闭链接 // 创建失败,直接关闭链接

0
server/handler/pool.go Executable file → Normal file
View File

View File

@ -1,7 +1,6 @@
package sessdata package sessdata
import ( import (
"crypto/md5"
"fmt" "fmt"
"math/rand" "math/rand"
"net" "net"
@ -32,6 +31,7 @@ type ConnSession struct {
IpAddr net.IP // 分配的ip地址 IpAddr net.IP // 分配的ip地址
LocalIp net.IP LocalIp net.IP
MacHw net.HardwareAddr // 客户端mac地址,从Session取出 MacHw net.HardwareAddr // 客户端mac地址,从Session取出
Username string
RemoteAddr string RemoteAddr string
Mtu int Mtu int
IfName string IfName string
@ -70,6 +70,7 @@ type Session struct {
DtlsSid string // dtls协议的 session_id DtlsSid string // dtls协议的 session_id
MacAddr string // 客户端mac地址 MacAddr string // 客户端mac地址
UniqueIdGlobal string // 客户端唯一标示 UniqueIdGlobal string // 客户端唯一标示
MacHw net.HardwareAddr
Username string // 用户名 Username string // 用户名
Group string Group string
AuthStep string AuthStep string
@ -147,6 +148,7 @@ func (s *Session) NewConn() *ConnSession {
s.mux.RLock() s.mux.RLock()
active := s.IsActive active := s.IsActive
macAddr := s.MacAddr macAddr := s.MacAddr
macHw := s.MacHw
username := s.Username username := s.Username
s.mux.RUnlock() s.mux.RUnlock()
if active { if active {
@ -157,14 +159,6 @@ func (s *Session) NewConn() *ConnSession {
if !limit { if !limit {
return nil return nil
} }
// 获取客户端mac地址
macHw, err := net.ParseMAC(macAddr)
if err != nil {
sum := md5.Sum([]byte(s.UniqueIdGlobal))
macHw = sum[0:5] // 5个byte
macHw = append([]byte{0x02}, macHw...)
macAddr = macHw.String()
}
ip := AcquireIp(username, macAddr) ip := AcquireIp(username, macAddr)
if ip == nil { if ip == nil {
LimitClient(username, true) LimitClient(username, true)
@ -173,7 +167,7 @@ func (s *Session) NewConn() *ConnSession {
// 查询group信息 // 查询group信息
group := &dbdata.Group{} group := &dbdata.Group{}
err = dbdata.One("Name", s.Group, group) err := dbdata.One("Name", s.Group, group)
if err != nil { if err != nil {
base.Error(err) base.Error(err)
return nil return nil
@ -182,6 +176,7 @@ func (s *Session) NewConn() *ConnSession {
cSess := &ConnSession{ cSess := &ConnSession{
Sess: s, Sess: s,
MacHw: macHw, MacHw: macHw,
Username: username,
IpAddr: ip, IpAddr: ip,
closeOnce: sync.Once{}, closeOnce: sync.Once{},
CloseChan: make(chan struct{}), CloseChan: make(chan struct{}),
@ -229,6 +224,11 @@ func (cs *ConnSession) Close() {
cs.Sess.LastLogin = time.Now() cs.Sess.LastLogin = time.Now()
cs.Sess.CSess = nil cs.Sess.CSess = nil
dSess := cs.GetDtlsSession()
if dSess != nil {
dSess.Close()
}
ReleaseIp(cs.IpAddr, cs.Sess.MacAddr) ReleaseIp(cs.IpAddr, cs.Sess.MacAddr)
LimitClient(cs.Sess.Username, true) LimitClient(cs.Sess.Username, true)
}) })
@ -272,7 +272,7 @@ func (cs *ConnSession) GetDtlsSession() *DtlsSession {
return nil return nil
} }
const BandwidthPeriodSec = 2 // 流量速率统计周期(秒) const BandwidthPeriodSec = 10 // 流量速率统计周期(秒)
func (cs *ConnSession) ratePeriod() { func (cs *ConnSession) ratePeriod() {
tick := time.NewTicker(time.Second * BandwidthPeriodSec) tick := time.NewTicker(time.Second * BandwidthPeriodSec)

View File

@ -11,7 +11,7 @@ import (
) )
const ( const (
StatsCycleSec = 5 // 统计周期(秒) StatsCycleSec = 10 // 统计周期(秒)
AddCycleSec = 60 // 记录到数据表周期(秒) AddCycleSec = 60 // 记录到数据表周期(秒)
) )

View File

@ -190,7 +190,7 @@ export default {
this.getAllStats() this.getAllStats()
const chartsTimer = setInterval(() => { const chartsTimer = setInterval(() => {
this.getAllStats() this.getAllStats()
}, 5000); }, 10000);
this.$once('hook:beforeDestroy', () => { this.$once('hook:beforeDestroy', () => {
clearInterval(chartsTimer); clearInterval(chartsTimer);
}) })

View File

@ -128,7 +128,7 @@ export default {
const chatTimer = setInterval(() => { const chatTimer = setInterval(() => {
this.getData(); this.getData();
}, 2000); }, 10000);
this.$once('hook:beforeDestroy', () => { this.$once('hook:beforeDestroy', () => {
clearInterval(chatTimer); clearInterval(chatTimer);