package parsers import ( "regexp" "strconv" "strings" ) type ( // AccessLists type defines the aggregated structures of access-list // possible on an ADX AccessLists struct { StandardAcls []ACL `json:"standard"` ExtendedAcls []ACL `json:"extended"` } // ACL type is the parsed data within an access-list ACL struct { Name string `json:"name"` Type string `json:"type"` Rules []string `json:"rules"` } ) // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (a *AccessLists) Parse(s *string) { // Named ACLs var numExtendedACLS, nameExtendedACLS, nameStandardACLS, numStandardACLS [][]string namedACLs := regexp.MustCompile(`ip\s+access-list\s+(extended|standard)\s+(\S+)((.|\n)*?)!`).FindAllStringSubmatch(*s, -1) for _, line := range namedACLs { if line[1] == "extended" { nameExtendedACLS = append(nameExtendedACLS, line) } else { nameStandardACLS = append(nameStandardACLS, line) } } a.ExtendedAcls = parseNamedExtendedACL(nameExtendedACLS) a.StandardAcls = parseNamedStandardACL(nameStandardACLS) // Numbered ACLs numberedACLS := regexp.MustCompile(`access-list (\d+) (.*)`).FindAllStringSubmatch(*s, -1) for _, line := range numberedACLS { t, _ := strconv.Atoi(line[1]) if t >= 100 { numExtendedACLS = append(numExtendedACLS, line) } else { numStandardACLS = append(numStandardACLS, line) } } a.parseNumberedStandardACL(numStandardACLS) a.parseNumberedExtendedACL(numExtendedACLS) } // GetStandardACLbyNum returns the location of the matching ACL, or nil if not found func (a *AccessLists) GetStandardACLbyName(n string) ACL { for _, acl := range a.StandardAcls { if acl.Name == n { return acl } } return ACL{} } func (a *AccessLists) parseNumberedStandardACL(acls [][]string) { sAcls := make(map[string]ACL) for _, line := range acls { if _, ok := sAcls[line[1]]; !ok { sAcls[line[1]] = ACL{ Name: line[1], Type: "numbered", Rules: make([]string, 0), } } acl := sAcls[line[1]] acl.Rules = append(acl.Rules, line[2]) sAcls[line[1]] = acl } // Transfer the ACL's from the map to the slice of pointers to StandardACL entries for _, acl := range sAcls { a.StandardAcls = append(a.StandardAcls, acl) } } func (a *AccessLists) parseNumberedExtendedACL(acls [][]string) { eAcls := make(map[string]ACL) for _, line := range acls { if _, ok := eAcls[line[1]]; !ok { eAcls[line[1]] = ACL{ Name: line[1], Type: "numbered", Rules: make([]string, 0), } } acl := eAcls[line[1]] acl.Rules = append(acl.Rules, line[2]) eAcls[line[1]] = acl } for _, acl := range eAcls { a.ExtendedAcls = append(a.ExtendedAcls, acl) } } func parseNamedExtendedACL(acls [][]string) []ACL { var nAcls []ACL for _, acl := range acls { var rules []string for _, r := range strings.Split(acl[3], "\n") { if r != "" { rules = append(rules, strings.TrimSpace(r)) } } nAcls = append(nAcls, ACL{ Name: acl[2], Type: "named", Rules: rules, }) } return nAcls } func parseNamedStandardACL(acls [][]string) []ACL { var nAcls = make([]ACL, 0) for _, acl := range acls { var rules []string for _, r := range strings.Split(acl[3], "\n") { if r != "" { rules = append(rules, strings.TrimSpace(r)) } } nAcls = append(nAcls, ACL{ Name: acl[2], Type: "named", Rules: rules, }) } return nAcls }