feat: support radius accounting request (#2362)

* feat: add radius server

* feat: parse org from packet

* feat: add comment

* feat: support radius accounting

* feat: change log

* feat: add copyright
This commit is contained in:
haiwu
2023-09-26 22:48:00 +08:00
committed by GitHub
parent 981908b0b6
commit 6fe5c44c1c
6 changed files with 229 additions and 38 deletions

View File

@ -15,26 +15,53 @@
package radius
import (
"fmt"
"time"
"github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/util"
"layeh.com/radius"
"layeh.com/radius/rfc2865"
"layeh.com/radius/rfc2866"
"layeh.com/radius/rfc2869"
)
const (
OrganizationVendorID = uint32(100)
)
func parseOrganization(p *radius.Packet) string {
for _, avp := range p.Attributes {
if avp.Type == rfc2865.VendorSpecific_Type {
attr := avp.Attribute
vendorId, value, err := radius.VendorSpecific(attr)
if err != nil {
return ""
}
if vendorId == OrganizationVendorID {
return string(value)
}
}
func GetAccountingFromRequest(r *radius.Request) *object.RadiusAccounting {
acctInputOctets := int(rfc2866.AcctInputOctets_Get(r.Packet))
acctInputGigawords := int(rfc2869.AcctInputGigawords_Get(r.Packet))
acctOutputOctets := int(rfc2866.AcctOutputOctets_Get(r.Packet))
acctOutputGigawords := int(rfc2869.AcctOutputGigawords_Get(r.Packet))
organization := rfc2865.Class_GetString(r.Packet)
getAcctStartTime := func(sessionTime int) time.Time {
m, _ := time.ParseDuration(fmt.Sprintf("-%ds", sessionTime))
return time.Now().Add(m)
}
return ""
ra := &object.RadiusAccounting{
Owner: organization,
Name: "ra_" + util.GenerateId()[:6],
CreatedTime: time.Now(),
Username: rfc2865.UserName_GetString(r.Packet),
ServiceType: int64(rfc2865.ServiceType_Get(r.Packet)),
NasId: rfc2865.NASIdentifier_GetString(r.Packet),
NasIpAddr: rfc2865.NASIPAddress_Get(r.Packet).String(),
NasPortId: rfc2869.NASPortID_GetString(r.Packet),
NasPortType: int64(rfc2865.NASPortType_Get(r.Packet)),
NasPort: int64(rfc2865.NASPort_Get(r.Packet)),
FramedIpAddr: rfc2865.FramedIPAddress_Get(r.Packet).String(),
FramedIpNetmask: rfc2865.FramedIPNetmask_Get(r.Packet).String(),
AcctSessionId: rfc2866.AcctSessionID_GetString(r.Packet),
AcctSessionTime: int64(rfc2866.AcctSessionTime_Get(r.Packet)),
AcctInputTotal: int64(acctInputOctets) + int64(acctInputGigawords)*4*1024*1024*1024,
AcctOutputTotal: int64(acctOutputOctets) + int64(acctOutputGigawords)*4*1024*1024*1024,
AcctInputPackets: int64(rfc2866.AcctInputPackets_Get(r.Packet)),
AcctOutputPackets: int64(rfc2866.AcctInputPackets_Get(r.Packet)),
AcctStartTime: getAcctStartTime(int(rfc2866.AcctSessionTime_Get(r.Packet))),
AcctTerminateCause: int64(rfc2866.AcctTerminateCause_Get(r.Packet)),
LastUpdate: time.Now(),
}
return ra
}