package parsers import ( "regexp" "strconv" "strings" ) var ( virtualServersRe = regexp.MustCompile(`server\s+virtual\s+(\S+)\s+(\S+)\n((.|\n)*?)!`) symPriorityRe = regexp.MustCompile(`sym-priority\s(\d+)`) vsDescriptionRe = regexp.MustCompile(`description\s(.*)`) portsRe = regexp.MustCompile(`port\s(\S+)(.*)`) usePortPolicyRe = regexp.MustCompile(`use-port-policy\s(\S+)`) cswEnabledRe = regexp.MustCompile(`csw$`) cswPolicyRe = regexp.MustCompile(`port\s\S+\scsw-policy\s(\S+)`) clientSubNetStickyRe = regexp.MustCompile(`client-subnet-sticky\s(\S+)\s(\S+)`) ) type ( // VirtualServers type defines a listing of virtual-servers VirtualServers struct { VirtualServers []VirtualServer `json:"virtual_servers"` } // VirtualServer type is the parsed attributes of a single virtual-server VirtualServer struct { Description string `json:"description"` Name string `json:"name"` Address string `json:"address"` Ports []VirtualPort `json:"ports"` SymPriority *int `json:"sym_priority"` } // VirtualPort type is parsed port data within a virtual-server config VirtualPort struct { Port string `json:"port"` Sticky bool `json:"sticky"` CSW bool `json:"csw_enabled"` CSWPolicy string `json:"csw_policy"` KeepAlive bool `json:"keepalive"` LBPriServers bool `json:"lb_pri_servers"` ResetOnPortFail bool `json:"reset_on_port_fail"` TCPOnly bool `json:"tcp_only"` PortPolicy string `json:"port_policy"` SSLTerminate PortSSL `json:"ssl_terminate"` SSLProxy SSLProxy `json:"ssl_proxy"` ClientSubnetSticky ClientSubNetSticky `json:"client_subnet_sticky"` BoundServers []BoundServer `json:"bound_servers"` } // ClientSubNetSticky type parses out the sticky persistence if configured ClientSubNetSticky struct { Type string `json:"type"` SubnetMask string `json:"subnet_mask,omitempty"` PrefixLength string `json:"prefix_length,omitempty"` } // SSLProxy : :ihavenoideawhatimdoing: SSLProxy struct { } // PortSSL parent struct for the Profile type PortSSL struct { Profile string `json:"profile"` } // BoundServer type describes a single server bound to the virtual-server BoundServer struct { Name string `json:"name"` AliasPort *string `json:"alias_port,omitempty"` RealPort string `json:"real_port"` } ) // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (vs *VirtualServers) Parse(s *string) { p := virtualServersRe.FindAllStringSubmatch(*s, -1) for _, i := range p { newVS := VirtualServer{} newVS.parse(&i[1], &i[2], &i[0]) vs.VirtualServers = append(vs.VirtualServers, newVS) } } func (vs *VirtualServer) isVirtualPortPresent(p *string) bool { for _, port := range vs.Ports { if port.Port == *p { return true } } return false } func (vs *VirtualServer) findPort(p *string) VirtualPort { for _, port := range vs.Ports { if port.Port == *p { return port } } return VirtualPort{} } func (vs *VirtualServer) parse(name *string, address *string, raw *string) { vs.Name = *name vs.Address = *address // Sym-priority p := symPriorityRe.FindStringSubmatch(*raw) if p != nil { pri, _ := strconv.Atoi(p[1]) vs.SymPriority = &pri } // Description d := vsDescriptionRe.FindStringSubmatch(*raw) if d != nil { vs.Description = d[1] } // Virtual Ports for _, po := range portsRe.FindAllStringSubmatch(*raw, -1) { if !vs.isVirtualPortPresent(&po[1]) { np := VirtualPort{} np.Port = po[1] if strings.Contains(po[2], "sticky") { np.Sticky = true } vs.Ports = append(vs.Ports, np) continue } // already added the port, parse the other things! // get the address of the port we're currently in cp := vs.findPort(&po[1]) if cp.Port != "" { // SSL-Terminate if strings.Contains(po[2], "ssl-terminate") { t := PortSSL{} t.Profile = strings.Split(strings.TrimSpace(po[2]), " ")[1] cp.SSLTerminate = t } // TCP-Only if strings.Contains(po[2], "tcp-only") { cp.TCPOnly = true } // Port-Policy pp := usePortPolicyRe.FindStringSubmatch(po[2]) if pp != nil { cp.PortPolicy = pp[1] } // CSW Enabled if cswEnabledRe.MatchString(po[2]) { cp.CSW = true } // CSW Policy Name csp := cswPolicyRe.FindStringSubmatch(po[2]) if csp != nil { cp.CSWPolicy = csp[1] } // client subnet sticky css := clientSubNetStickyRe.FindStringSubmatch(po[2]) if css != nil { newCSS := ClientSubNetSticky{} newCSS.Type = css[1] if css[1] == "subnet-mask" { newCSS.SubnetMask = css[2] } else { newCSS.PrefixLength = css[2] } cp.ClientSubnetSticky = newCSS } } } }