package pkg import ( "errors" "strconv" "strings" ) type SIPProfile struct { ProfileCommon ALGEnable string `json:"alg_enable"` // disabled Community string `json:"community"` // none DialogAware string `json:"dialog_aware"` // disabled DialogEstablishmentTimeout int `json:"dialog_establishment_timeout"` // 10 EnableSIPFirewall string `json:"enable_sip_firewall"` // no InsertRecordRouteHeader string `json:"insert_record_route_header"` // disabled InsertViaHeader string `json:"insert_via_header"` // disabled LogProfile string `json:"log_profile"` // none LogPublisher string `json:"log_publisher"` // none MaxMediaSessions int `json:"max_media_sessions"` // 6 MaxRegistrations int `json:"max_registrations"` // 100 MaxSessionsPerRegistration int `json:"max_sessions_per_registration"` // 50 MaxSize int `json:"max_size"` // 65535 RegistrationTimeout int `json:"registration_timeout"` // 3600 RTPProxyStyle string `json:"rtp_proxy_style"` // symmetric SecureViaHeader string `json:"secure_via_header"` // disabled Security string `json:"security"` // disabled SIPSessionTimeout int `json:"sip_session_timeout"` // 300 TerminateOnBye string `json:"terminate_on_bye"` // enabled UserViaHeader string `json:"user_via_header"` // none } func ParseSIPProfile(raw string) (*SIPProfile, error) { p := new(SIPProfile) split := strings.Split(raw, "\n") if !strings.Contains(split[0], "ltm profile sip") { return nil, errors.New("not a SIP profile") } p.ProfileCommon = parseCommon(raw) for _, line := range split { f := strings.Fields(line) switch strings.ToLower(f[0]) { case "alg-enable": p.ALGEnable = strings.Join(f[1:], " ") case "community": p.Community = strings.Join(f[1:], " ") case "dialog-aware": p.DialogAware = strings.Join(f[1:], " ") case "dialog-establishment-timeout": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.DialogEstablishmentTimeout = i case "enable-sip-firewall": p.EnableSIPFirewall = strings.Join(f[1:], " ") case "insert-record-route-header": p.InsertRecordRouteHeader = strings.Join(f[1:], " ") case "insert-via-header": p.InsertViaHeader = strings.Join(f[1:], " ") case "log-profile": p.LogProfile = strings.Join(f[1:], " ") case "log-publisher": p.LogPublisher = strings.Join(f[1:], " ") case "max-media-sessions": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.MaxMediaSessions = i case "max-registrations": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.MaxRegistrations = i case "max-sessions-per-registration": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.MaxSessionsPerRegistration = i case "max-size": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.MaxSize = i case "registration-timeout": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.RegistrationTimeout = i case "rtp-proxy-style": p.RTPProxyStyle = strings.Join(f[1:], " ") case "secure-via-header": p.SecureViaHeader = strings.Join(f[1:], " ") case "security": p.Security = strings.Join(f[1:], " ") case "sip-session-timeout": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.SIPSessionTimeout = i case "terminate-on-bye": p.TerminateOnBye = strings.Join(f[1:], " ") case "user-via-header": p.UserViaHeader = strings.Join(f[1:], " ") } } return p, nil }