gitlab.com/Raven-IO/raven-delve@v1.22.4/_fixtures/issue406.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "log" 6 "regexp" 7 "strings" 8 9 // "golang.org/x/crypto/ssh/terminal" 10 ) 11 12 // Blacklist type is a map of Nodes with string keys 13 type Blacklist map[string]*Node 14 15 // Source type is a map of Srcs with string keys 16 type Source map[string]*Src 17 18 // Node configuration record 19 type Node struct { 20 Disable bool 21 IP string 22 Include, Exclude []string 23 Source 24 } 25 26 // Src record, struct for Source map 27 type Src struct { 28 Disable bool 29 Desc string 30 Prfx string 31 URL string 32 } 33 34 // String returns pretty print for the Blacklist struct 35 func (b Blacklist) String() (result string) { 36 // cols, _, _ := terminal.GetSize(int(os.Stdout.Fd())) 37 cols := 20 38 for pkey := range b { 39 result += fmt.Sprintf("Node: %v\n\tDisabled: %v\n", pkey, b[pkey].Disable) 40 result += fmt.Sprintf("\tRedirect IP: %v\n\tExclude(s):\n", b[pkey].IP) 41 for _, exclude := range b[pkey].Exclude { 42 result += fmt.Sprintf("\t\t%v\n", exclude) 43 } 44 result += fmt.Sprintf("\tInclude(s):\n") 45 for _, include := range b[pkey].Include { 46 result += fmt.Sprintf("\t\t%v\n", include) 47 } 48 for skey, src := range b[pkey].Source { 49 result += fmt.Sprintf("\tSource: %v\n\t\tDisabled: %v\n", skey, src.Disable) 50 result += fmt.Sprintf("\t\tDescription: %v\n", b[pkey].Source[skey].Desc) 51 result += fmt.Sprintf("\t\tPrefix: %v\n\t\tURL: %v\n", b[pkey].Source[skey].Prfx, b[pkey].Source[skey].URL) 52 } 53 result += fmt.Sprintln(strings.Repeat("-", cols/2)) 54 } 55 return result 56 } 57 58 // ToBool converts a string ("true" or "false") to its boolean equivalent 59 func ToBool(s string) (b bool) { 60 if len(s) == 0 { 61 log.Fatal("ERROR: variable empty, cannot convert to boolean") 62 } 63 switch s { 64 case "false": 65 b = false 66 case "true": 67 b = true 68 } 69 return b 70 } 71 72 // Get extracts nodes from a EdgeOS/VyOS configuration structure 73 func Get(cfg string) { 74 type re struct { 75 brkt, cmnt, desc, dsbl, leaf, misc, mlti, mpty, name, node *regexp.Regexp 76 } 77 78 rx := &re{} 79 rx.brkt = regexp.MustCompile(`[}]`) 80 rx.cmnt = regexp.MustCompile(`^([\/*]+).*([*\/]+)$`) 81 rx.desc = regexp.MustCompile(`^(?:description)+\s"?([^"]+)?"?$`) 82 rx.dsbl = regexp.MustCompile(`^(disabled)+\s([\S]+)$`) 83 rx.leaf = regexp.MustCompile(`^(source)+\s([\S]+)\s[{]{1}$`) 84 rx.misc = regexp.MustCompile(`^([\w-]+)$`) 85 rx.mlti = regexp.MustCompile(`^((?:include|exclude)+)\s([\S]+)$`) 86 rx.mpty = regexp.MustCompile(`^$`) 87 rx.name = regexp.MustCompile(`^([\w-]+)\s([\S]+)$`) 88 rx.node = regexp.MustCompile(`^([\w-]+)\s[{]{1}$`) 89 90 cfgtree := make(map[string]*Node) 91 92 var tnode string 93 var leaf string 94 95 for _, line := range strings.Split(cfg, "\n") { 96 line = strings.TrimSpace(line) 97 switch { 98 case rx.mlti.MatchString(line): 99 { 100 IncExc := rx.mlti.FindStringSubmatch(line) 101 switch IncExc[1] { 102 case "exclude": 103 cfgtree[tnode].Exclude = append(cfgtree[tnode].Exclude, IncExc[2]) 104 case "include": 105 cfgtree[tnode].Include = append(cfgtree[tnode].Include, IncExc[2]) 106 } 107 } 108 case rx.node.MatchString(line): 109 { 110 node := rx.node.FindStringSubmatch(line) 111 tnode = node[1] 112 cfgtree[tnode] = &Node{} 113 cfgtree[tnode].Source = make(map[string]*Src) 114 } 115 case rx.leaf.MatchString(line): 116 src := rx.leaf.FindStringSubmatch(line) 117 leaf = src[2] 118 119 if src[1] == "source" { 120 cfgtree[tnode].Source[leaf] = &Src{} 121 } 122 case rx.dsbl.MatchString(line): 123 { 124 disabled := rx.dsbl.FindStringSubmatch(line) 125 cfgtree[tnode].Disable = ToBool(disabled[1]) 126 } 127 case rx.name.MatchString(line): 128 { 129 name := rx.name.FindStringSubmatch(line) 130 switch name[1] { 131 case "prefix": 132 cfgtree[tnode].Source[leaf].Prfx = name[2] 133 case "url": 134 cfgtree[tnode].Source[leaf].URL = name[2] 135 case "description": 136 cfgtree[tnode].Source[leaf].Desc = name[2] 137 case "dns-redirect-ip": 138 cfgtree[tnode].IP = name[2] 139 } 140 } 141 case rx.desc.MatchString(line) || rx.cmnt.MatchString(line) || rx.misc.MatchString(line): 142 break 143 } 144 // fmt.Printf("%s\n", line) 145 } 146 fmt.Println(cfgtree) 147 } 148 149 func main() { 150 cfgtree := make(Blacklist) 151 for _, k := range []string{"root", "hosts", "domains"} { 152 cfgtree[k] = &Node{} 153 cfgtree[k].Source = make(Source) 154 } 155 cfgtree["hosts"].Exclude = append(cfgtree["hosts"].Exclude, "rackcdn.com", "schema.org") 156 cfgtree["hosts"].Include = append(cfgtree["hosts"].Include, "msdn.com", "badgits.org") 157 cfgtree["hosts"].IP = "192.168.168.1" 158 cfgtree["hosts"].Source["hpHosts"] = &Src{URL: "http://www.bonzon.com", Prfx: "127.0.0.0"} 159 fmt.Println(cfgtree) 160 fmt.Println(cfgtree["hosts"]) 161 Get(testdata) 162 } 163 164 var testdata = `blacklist { 165 disabled false 166 dns-redirect-ip 0.0.0.0 167 domains { 168 include adsrvr.org 169 include adtechus.net 170 include advertising.com 171 include centade.com 172 include doubleclick.net 173 include free-counter.co.uk 174 include intellitxt.com 175 include kiosked.com 176 source malc0de { 177 description "List of zones serving malicious executables observed by malc0de.com/database/" 178 prefix "zone " 179 url http://malc0de.com/bl/ZONES 180 } 181 } 182 exclude 122.2o7.net 183 exclude 1e100.net 184 exclude adobedtm.com 185 exclude akamai.net 186 exclude amazon.com 187 exclude amazonaws.com 188 exclude apple.com 189 exclude ask.com 190 exclude avast.com 191 exclude bitdefender.com 192 exclude cdn.visiblemeasures.com 193 exclude cloudfront.net 194 exclude coremetrics.com 195 exclude edgesuite.net 196 exclude freedns.afraid.org 197 exclude github.com 198 exclude githubusercontent.com 199 exclude google.com 200 exclude googleadservices.com 201 exclude googleapis.com 202 exclude googleusercontent.com 203 exclude gstatic.com 204 exclude gvt1.com 205 exclude gvt1.net 206 exclude hb.disney.go.com 207 exclude hp.com 208 exclude hulu.com 209 exclude images-amazon.com 210 exclude msdn.com 211 exclude paypal.com 212 exclude rackcdn.com 213 exclude schema.org 214 exclude skype.com 215 exclude smacargo.com 216 exclude sourceforge.net 217 exclude ssl-on9.com 218 exclude ssl-on9.net 219 exclude static.chartbeat.com 220 exclude storage.googleapis.com 221 exclude windows.net 222 exclude yimg.com 223 exclude ytimg.com 224 hosts { 225 include beap.gemini.yahoo.com 226 source adaway { 227 description "Blocking mobile ad providers and some analytics providers" 228 prefix "127.0.0.1 " 229 url http://adaway.org/hosts.txt 230 } 231 source malwaredomainlist { 232 description "127.0.0.1 based host and domain list" 233 prefix "127.0.0.1 " 234 url http://www.malwaredomainlist.com/hostslist/hosts.txt 235 } 236 source openphish { 237 description "OpenPhish automatic phishing detection" 238 prefix http 239 url https://openphish.com/feed.txt 240 } 241 source someonewhocares { 242 description "Zero based host and domain list" 243 prefix 0.0.0.0 244 url http://someonewhocares.org/hosts/zero/ 245 } 246 source volkerschatz { 247 description "Ad server blacklists" 248 prefix http 249 url http://www.volkerschatz.com/net/adpaths 250 } 251 source winhelp2002 { 252 description "Zero based host and domain list" 253 prefix "0.0.0.0 " 254 url http://winhelp2002.mvps.org/hosts.txt 255 } 256 source yoyo { 257 description "Fully Qualified Domain Names only - no prefix to strip" 258 prefix "" 259 url http://pgl.yoyo.org/as/serverlist.php?hostformat=nohtml&showintro=1&mimetype=plaintext 260 } 261 } 262 }`