From ce2ab3e5e42f3bfe5cbc3f49e0c7905a0e097099 Mon Sep 17 00:00:00 2001 From: Jett Wang Date: Mon, 30 Dec 2024 21:10:30 +0800 Subject: [PATCH] 2024-12-30 21:10:20 : add ipv6 attrs --- assets/buildinfo.txt | 10 +-- assets/templates/radius_accounting.html | 2 + assets/templates/radius_session.html | 2 + models/radius.go | 96 +++++++++++---------- toughradius/auth_passwd_check.go | 8 +- toughradius/radius.go | 108 ++++++++++++++---------- 6 files changed, 127 insertions(+), 99 deletions(-) diff --git a/assets/buildinfo.txt b/assets/buildinfo.txt index 8207ae62..069ca3a0 100644 --- a/assets/buildinfo.txt +++ b/assets/buildinfo.txt @@ -1,8 +1,8 @@ -BuildVersion=latest v8.0.7 2024-10-09 14:12:13 +BuildVersion=latest v8.0.7 2024-12-30 21:10:20 ReleaseVersion=v8.0.7 -BuildTime=2024-10-09 14:12:13 +BuildTime=2024-12-30 21:10:20 BuildName=toughradius -CommitID=f33f34d9a034dd98d3c86db4da716be2b3db96e0 -CommitDate=Wed, 9 Oct 2024 14:11:52 +0800 +CommitID=339021aeea126bd15b5c492341d30c3b48393c80 +CommitDate=Wed, 9 Oct 2024 14:12:21 +0800 CommitUser=jamiesun.net@gmail.com -CommitSubject=Refactor panic messages in GetCurrUser and GetCurrUserlevel functions +CommitSubject=2024-10-09 14:12:13 : diff --git a/assets/templates/radius_accounting.html b/assets/templates/radius_accounting.html index f5b2aa2b..da6b06d4 100644 --- a/assets/templates/radius_accounting.html +++ b/assets/templates/radius_accounting.html @@ -79,6 +79,8 @@ {id: "nas_id", header: [tr("radius","NasId")], adjust: true, sort: "string"}, {id: "nas_addr", header: [tr("radius","NasIp")], adjust: true, sort: "string"}, {id: "framed_ipaddr", header: [tr("radius","UserIP")], adjust: true, sort: "string"}, + {id: "framed_ipv6_address", header: [tr("radius","IPv6")], adjust: true, sort: "string"}, + {id: "framed_ipv6_prefix", header: [tr("radius","IPv6Prefix")], adjust: true, sort: "string"}, {id: "mac_addr", header: [tr("radius","Mac")], adjust: true, sort: "string"}, {id: "nas_port_id", header: [tr("radius","NasPortId")], hidden: true, sort: "string"}, {id: "acct_session_time", header: [tr("radius","SessionTime")], adjust: true, sort: "int"}, diff --git a/assets/templates/radius_session.html b/assets/templates/radius_session.html index 9eb3f501..61dae00c 100644 --- a/assets/templates/radius_session.html +++ b/assets/templates/radius_session.html @@ -83,6 +83,8 @@ {id: "nas_id", header: [tr("radius","NasId")], adjust: true, sort: "string"}, {id: "nas_addr", header: [tr("radius","NasIp")], adjust: true, sort: "string"}, {id: "framed_ipaddr", header: [tr("radius","UserIP")], adjust: true, sort: "string"}, + {id: "framed_ipv6_address", header: [tr("radius","IPv6")], adjust: true, sort: "string"}, + {id: "framed_ipv6_prefix", header: [tr("radius","IPv6Prefix")], adjust: true, sort: "string"}, {id: "mac_addr", header: [tr("radius","Mac")], adjust: true, sort: "string"}, {id: "nas_port_id", header: [tr("radius","NasPortId")], hidden: true, sort: "string"}, {id: "acct_session_time", header: [tr("radius","SessionTime")], adjust: true, sort: "int"}, diff --git a/models/radius.go b/models/radius.go index f096e299..d3f52e83 100644 --- a/models/radius.go +++ b/models/radius.go @@ -52,54 +52,60 @@ type RadiusUser struct { // RadiusOnline // Radius RadiusOnline Recode type RadiusOnline struct { - ID int64 `json:"id,string"` // 主键 ID - Username string `gorm:"index" json:"username"` - NasId string `json:"nas_id"` - NasAddr string `json:"nas_addr"` - NasPaddr string `json:"nas_paddr"` - SessionTimeout int `json:"session_timeout"` - FramedIpaddr string `json:"framed_ipaddr"` - FramedNetmask string `json:"framed_netmask"` - MacAddr string `json:"mac_addr"` - NasPort int64 `json:"nas_port,string"` - NasClass string `json:"nas_class"` - NasPortId string `json:"nas_port_id"` - NasPortType int `json:"nas_port_type"` - ServiceType int `json:"service_type"` - AcctSessionId string `gorm:"index" json:"acct_session_id"` - AcctSessionTime int `json:"acct_session_time"` - AcctInputTotal int64 `json:"acct_input_total,string"` - AcctOutputTotal int64 `json:"acct_output_total,string"` - AcctInputPackets int `json:"acct_input_packets"` - AcctOutputPackets int `json:"acct_output_packets"` - AcctStartTime time.Time `gorm:"index" json:"acct_start_time"` - LastUpdate time.Time `json:"last_update"` + ID int64 `json:"id,string"` // 主键 ID + Username string `gorm:"index" json:"username"` + NasId string `json:"nas_id"` + NasAddr string `json:"nas_addr"` + NasPaddr string `json:"nas_paddr"` + SessionTimeout int `json:"session_timeout"` + FramedIpaddr string `json:"framed_ipaddr"` + FramedNetmask string `json:"framed_netmask"` + FramedIpv6Prefix string `json:"framed_ipv6_prefix"` + FramedIpv6Address string `json:"framed_ipv6_address"` + DelegatedIpv6Prefix string `json:"delegated_ipv6_prefix"` + MacAddr string `json:"mac_addr"` + NasPort int64 `json:"nas_port,string"` + NasClass string `json:"nas_class"` + NasPortId string `json:"nas_port_id"` + NasPortType int `json:"nas_port_type"` + ServiceType int `json:"service_type"` + AcctSessionId string `gorm:"index" json:"acct_session_id"` + AcctSessionTime int `json:"acct_session_time"` + AcctInputTotal int64 `json:"acct_input_total,string"` + AcctOutputTotal int64 `json:"acct_output_total,string"` + AcctInputPackets int `json:"acct_input_packets"` + AcctOutputPackets int `json:"acct_output_packets"` + AcctStartTime time.Time `gorm:"index" json:"acct_start_time"` + LastUpdate time.Time `json:"last_update"` } // RadiusAccounting // Radius Accounting Recode type RadiusAccounting struct { - ID int64 `json:"id,string"` // 主键 ID - Username string `gorm:"index" json:"username"` - AcctSessionId string `gorm:"index" json:"acct_session_id"` - NasId string `json:"nas_id"` - NasAddr string `json:"nas_addr"` - NasPaddr string `json:"nas_paddr"` - SessionTimeout int `json:"session_timeout"` - FramedIpaddr string `json:"framed_ipaddr"` - FramedNetmask string `json:"framed_netmask"` - MacAddr string `json:"mac_addr"` - NasPort int64 `json:"nas_port,string"` - NasClass string `json:"nas_class"` - NasPortId string `json:"nas_port_id"` - NasPortType int `json:"nas_port_type"` - ServiceType int `json:"service_type"` - AcctSessionTime int `json:"acct_session_time"` - AcctInputTotal int64 `json:"acct_input_total,string"` - AcctOutputTotal int64 `json:"acct_output_total,string"` - AcctInputPackets int `json:"acct_input_packets"` - AcctOutputPackets int `json:"acct_output_packets"` - LastUpdate time.Time `json:"last_update"` - AcctStartTime time.Time `gorm:"index" json:"acct_start_time"` - AcctStopTime time.Time `gorm:"index" json:"acct_stop_time"` + ID int64 `json:"id,string"` // 主键 ID + Username string `gorm:"index" json:"username"` + AcctSessionId string `gorm:"index" json:"acct_session_id"` + NasId string `json:"nas_id"` + NasAddr string `json:"nas_addr"` + NasPaddr string `json:"nas_paddr"` + SessionTimeout int `json:"session_timeout"` + FramedIpaddr string `json:"framed_ipaddr"` + FramedNetmask string `json:"framed_netmask"` + FramedIpv6Prefix string `json:"framed_ipv6_prefix"` + FramedIpv6Address string `json:"framed_ipv6_address"` + DelegatedIpv6Prefix string `json:"delegated_ipv6_prefix"` + MacAddr string `json:"mac_addr"` + NasPort int64 `json:"nas_port,string"` + NasClass string `json:"nas_class"` + NasPortId string `json:"nas_port_id"` + NasPortType int `json:"nas_port_type"` + ServiceType int `json:"service_type"` + AcctSessionTime int `json:"acct_session_time"` + AcctInputTotal int64 `json:"acct_input_total,string"` + AcctOutputTotal int64 `json:"acct_output_total,string"` + AcctInputPackets int `json:"acct_input_packets"` + AcctOutputPackets int `json:"acct_output_packets"` + LastUpdate time.Time `json:"last_update"` + AcctStartTime time.Time `gorm:"index" json:"acct_start_time"` + AcctStopTime time.Time `gorm:"index" json:"acct_stop_time"` } diff --git a/toughradius/auth_passwd_check.go b/toughradius/auth_passwd_check.go index 49535dd2..ad7c1e4b 100644 --- a/toughradius/auth_passwd_check.go +++ b/toughradius/auth_passwd_check.go @@ -65,10 +65,10 @@ func (s *AuthService) CheckPassword(r *radius.Request, username, localpassword s return nil } - if password != "" && !ignoreChk && !isMacAuth { - if strings.TrimSpace(password) != localpassword { - return NewAuthError(app.MetricsRadiusRejectPasswdError, "user pap password is not match") - } + if strings.TrimSpace(password) != "" && + !ignoreChk && !isMacAuth && + strings.TrimSpace(password) != localpassword { + return NewAuthError(app.MetricsRadiusRejectPasswdError, "user pap password is not match") } return nil diff --git a/toughradius/radius.go b/toughradius/radius.go index 6e1c5788..238eda8c 100644 --- a/toughradius/radius.go +++ b/toughradius/radius.go @@ -18,12 +18,15 @@ import ( "github.com/talkincode/toughradius/v8/common" "github.com/talkincode/toughradius/v8/common/zaplog/log" "github.com/talkincode/toughradius/v8/models" + "github.com/talkincode/toughradius/v8/toughradius/vendors/huawei" "go.uber.org/zap" "gorm.io/gorm" "layeh.com/radius" "layeh.com/radius/rfc2865" "layeh.com/radius/rfc2866" "layeh.com/radius/rfc2869" + "layeh.com/radius/rfc3162" + "layeh.com/radius/rfc4818" ) const ( @@ -201,6 +204,15 @@ func (s *RadiusService) GetEapMethod() string { return val } +func GetFramedIpv6Address(r *radius.Request, vpe *models.NetVpe) string { + switch vpe.VendorCode { + case VendorHuawei: + return common.IfEmptyStr(huawei.HuaweiFramedIPv6Address_Get(r.Packet).String(), common.NA) + default: + return "" + } +} + func GetNetRadiusOnlineFromRequest(r *radius.Request, vr *VendorRequest, vpe *models.NetVpe, nasrip string) models.RadiusOnline { acctInputOctets := int(rfc2866.AcctInputOctets_Get(r.Packet)) acctInputGigawords := int(rfc2869.AcctInputGigawords_Get(r.Packet)) @@ -212,28 +224,31 @@ func GetNetRadiusOnlineFromRequest(r *radius.Request, vr *VendorRequest, vpe *mo return time.Now().Add(m) } return models.RadiusOnline{ - ID: 0, - Username: rfc2865.UserName_GetString(r.Packet), - NasId: common.IfEmptyStr(rfc2865.NASIdentifier_GetString(r.Packet), common.NA), - NasAddr: vpe.Ipaddr, - NasPaddr: nasrip, - SessionTimeout: int(rfc2865.SessionTimeout_Get(r.Packet)), - FramedIpaddr: common.IfEmptyStr(rfc2865.FramedIPAddress_Get(r.Packet).String(), common.NA), - FramedNetmask: common.IfEmptyStr(rfc2865.FramedIPNetmask_Get(r.Packet).String(), common.NA), - MacAddr: common.IfEmptyStr(vr.MacAddr, common.NA), - NasPort: 0, - NasClass: common.NA, - NasPortId: common.IfEmptyStr(rfc2869.NASPortID_GetString(r.Packet), common.NA), - NasPortType: 0, - ServiceType: 0, - AcctSessionId: rfc2866.AcctSessionID_GetString(r.Packet), - AcctSessionTime: int(rfc2866.AcctSessionTime_Get(r.Packet)), - AcctInputTotal: int64(acctInputOctets) + int64(acctInputGigawords)*4*1024*1024*1024, - AcctOutputTotal: int64(acctOutputOctets) + int64(acctOutputGigawords)*4*1024*1024*1024, - AcctInputPackets: int(rfc2866.AcctInputPackets_Get(r.Packet)), - AcctOutputPackets: int(rfc2866.AcctInputPackets_Get(r.Packet)), - AcctStartTime: getAcctStartTime(int(rfc2866.AcctSessionTime_Get(r.Packet))), - LastUpdate: time.Now(), + ID: 0, + Username: rfc2865.UserName_GetString(r.Packet), + NasId: common.IfEmptyStr(rfc2865.NASIdentifier_GetString(r.Packet), common.NA), + NasAddr: vpe.Ipaddr, + NasPaddr: nasrip, + SessionTimeout: int(rfc2865.SessionTimeout_Get(r.Packet)), + FramedIpaddr: common.IfEmptyStr(rfc2865.FramedIPAddress_Get(r.Packet).String(), common.NA), + FramedNetmask: common.IfEmptyStr(rfc2865.FramedIPNetmask_Get(r.Packet).String(), common.NA), + FramedIpv6Address: GetFramedIpv6Address(r, vpe), + FramedIpv6Prefix: common.IfEmptyStr(rfc3162.FramedIPv6Prefix_Get(r.Packet).String(), common.NA), + DelegatedIpv6Prefix: common.IfEmptyStr(rfc4818.DelegatedIPv6Prefix_Get(r.Packet).String(), common.NA), + MacAddr: common.IfEmptyStr(vr.MacAddr, common.NA), + NasPort: 0, + NasClass: common.NA, + NasPortId: common.IfEmptyStr(rfc2869.NASPortID_GetString(r.Packet), common.NA), + NasPortType: 0, + ServiceType: 0, + AcctSessionId: rfc2866.AcctSessionID_GetString(r.Packet), + AcctSessionTime: int(rfc2866.AcctSessionTime_Get(r.Packet)), + AcctInputTotal: int64(acctInputOctets) + int64(acctInputGigawords)*4*1024*1024*1024, + AcctOutputTotal: int64(acctOutputOctets) + int64(acctOutputGigawords)*4*1024*1024*1024, + AcctInputPackets: int(rfc2866.AcctInputPackets_Get(r.Packet)), + AcctOutputPackets: int(rfc2866.AcctInputPackets_Get(r.Packet)), + AcctStartTime: getAcctStartTime(int(rfc2866.AcctSessionTime_Get(r.Packet))), + LastUpdate: time.Now(), } } @@ -274,29 +289,32 @@ func (s *RadiusService) AddRadiusOnline(ol models.RadiusOnline) error { func (s *RadiusService) AddRadiusAccounting(ol models.RadiusOnline, start bool) error { accounting := models.RadiusAccounting{ - ID: common.UUIDint64(), - Username: ol.Username, - AcctSessionId: ol.AcctSessionId, - NasId: ol.NasId, - NasAddr: ol.NasAddr, - NasPaddr: ol.NasPaddr, - SessionTimeout: ol.SessionTimeout, - FramedIpaddr: ol.FramedIpaddr, - FramedNetmask: ol.FramedNetmask, - MacAddr: ol.MacAddr, - NasPort: ol.NasPort, - NasClass: ol.NasClass, - NasPortId: ol.NasPortId, - NasPortType: ol.NasPortType, - ServiceType: ol.ServiceType, - AcctSessionTime: ol.AcctSessionTime, - AcctInputTotal: ol.AcctInputTotal, - AcctOutputTotal: ol.AcctOutputTotal, - AcctInputPackets: ol.AcctInputPackets, - AcctOutputPackets: ol.AcctOutputPackets, - LastUpdate: time.Now(), - AcctStartTime: ol.AcctStartTime, - AcctStopTime: time.Time{}, + ID: common.UUIDint64(), + Username: ol.Username, + AcctSessionId: ol.AcctSessionId, + NasId: ol.NasId, + NasAddr: ol.NasAddr, + NasPaddr: ol.NasPaddr, + SessionTimeout: ol.SessionTimeout, + FramedIpaddr: ol.FramedIpaddr, + FramedNetmask: ol.FramedNetmask, + FramedIpv6Prefix: ol.FramedIpv6Prefix, + FramedIpv6Address: ol.FramedIpv6Address, + DelegatedIpv6Prefix: ol.DelegatedIpv6Prefix, + MacAddr: ol.MacAddr, + NasPort: ol.NasPort, + NasClass: ol.NasClass, + NasPortId: ol.NasPortId, + NasPortType: ol.NasPortType, + ServiceType: ol.ServiceType, + AcctSessionTime: ol.AcctSessionTime, + AcctInputTotal: ol.AcctInputTotal, + AcctOutputTotal: ol.AcctOutputTotal, + AcctInputPackets: ol.AcctInputPackets, + AcctOutputPackets: ol.AcctOutputPackets, + LastUpdate: time.Now(), + AcctStartTime: ol.AcctStartTime, + AcctStopTime: time.Time{}, } if !start {