From 090bbe1f4c841a085dff9c6bb99ac9581bc4994d Mon Sep 17 00:00:00 2001 From: q191201771 <191201771@qq.com> Date: Fri, 21 Jun 2024 10:34:31 +0800 Subject: [PATCH] =?UTF-8?q?demo=20dispatch:=20=E5=BD=93=E8=B6=85=E8=BF=87M?= =?UTF-8?q?axSubSessionPerIp=E6=97=B6=EF=BC=8C=E8=B8=A2=E5=87=BA=E8=B6=85?= =?UTF-8?q?=E8=BF=87=E9=99=90=E5=88=B6=E7=9A=84=E3=80=81=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E4=B8=8A=E5=81=8F=E5=90=8E=E7=9A=84=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/demo/dispatch/dispatch__security.go | 31 +++++++++++++++++-------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/app/demo/dispatch/dispatch__security.go b/app/demo/dispatch/dispatch__security.go index 0be33cf0..31761641 100644 --- a/app/demo/dispatch/dispatch__security.go +++ b/app/demo/dispatch/dispatch__security.go @@ -26,7 +26,7 @@ func securityMaxSubSessionPerIp(info base.UpdateInfo) { for _, sub := range g.StatSubs { host, _, err := net.SplitHostPort(sub.RemoteAddr) if err != nil { - nazalog.Warnf("split host port failed. remote addr=%s", sub.RemoteAddr) + nazalog.Warnf("securityMaxSubSessionPerIp. split host port failed. remote addr=%s", sub.RemoteAddr) continue } ip2SubSessions[host] = append(ip2SubSessions[host], sub) @@ -34,20 +34,31 @@ func securityMaxSubSessionPerIp(info base.UpdateInfo) { } } + lastUnix := int64(-1) + lastId := "" for ip, subs := range ip2SubSessions { if len(subs) <= config.MaxSubSessionPerIp { continue } - nazalog.Debugf("close session. ip=%s, session count=%d", ip, len(subs)) + nazalog.Debugf("securityMaxSubSessionPerIp. close session. ip=%s, session count=%d", ip, len(subs)) + for _, sub := range subs { - //if sub.Protocol == base.SessionProtocolHlsStr { - // host, _, err := net.SplitHostPort(sub.RemoteAddr) - // if err != nil { - // nazalog.Warnf("split host port failed. remote addr=%s", sub.RemoteAddr) - // continue - // } - // addIpBlacklist(info.ServerId, host, 60) - //} + st, err := base.ParseReadableTime(sub.StartTime) + if err != nil { + nazalog.Warnf("securityMaxSubSessionPerIp. parse readable time failed. start time=%s, err=%+v", sub.StartTime, err) + continue + } + if st.UnixMilli() > lastUnix { + lastUnix = st.UnixMilli() + lastId = sub.SessionId + } + } + + for _, sub := range subs { + if sub.SessionId == lastId { + nazalog.Debugf("securityMaxSubSessionPerIp. skip kick. last id=%s", lastId) + continue + } kickSession(info.ServerId, sessionId2StreamName[sub.SessionId], sub.SessionId) } }