コード例 #1
0
ファイル: regx_test.go プロジェクト: britannic/blacklist
func TestGet(t *testing.T) {
	Convey("Testing Get()", t, func() {
		for k := range c {
			act := regx.Get([]byte(k), c[k].input)
			So(len(act), ShouldBeGreaterThan, 0)
			So(act[c[k].index], ShouldResemble, c[k].result)
		}
	})
}
コード例 #2
0
ファイル: config.go プロジェクト: britannic/blacklist
// ReadCfg extracts nodes from a EdgeOS/VyOS configuration structure
func (c *Config) ReadCfg(r ConfLoader) error {
	var (
		tnode string
		b     = bufio.NewScanner(r.read())
		leaf  string
		nodes []string
		rx    = regx.Obj
		o     *object
	)

LINE:
	for b.Scan() {
		line := bytes.TrimSpace(b.Bytes())

		switch {
		case rx.MLTI.Match(line):
			incExc := regx.Get([]byte("mlti"), line)
			switch string(incExc[1]) {
			case "exclude":
				c.tree[tnode].exc = append(c.tree[tnode].exc, string(incExc[2]))

			case "include":
				c.tree[tnode].inc = append(c.tree[tnode].inc, string(incExc[2]))
			}

		case rx.NODE.Match(line):
			node := regx.Get([]byte("node"), line)
			tnode = string(node[1])
			nodes = append(nodes, tnode)
			c.tree[tnode] = newObject()

		case rx.LEAF.Match(line):
			srcName := regx.Get([]byte("leaf"), line)
			leaf = string(srcName[2])
			nodes = append(nodes, string(srcName[1]))

			if bytes.Equal(srcName[1], []byte(src)) {
				o = newObject()
				o.name = leaf
				o.nType = getType(tnode).(ntype)
			}

		case rx.DSBL.Match(line):
			c.tree[tnode].disabled = strToBool(string(regx.Get([]byte("dsbl"), line)[1]))

		case rx.IPBH.Match(line) && nodes[len(nodes)-1] != src:
			c.tree[tnode].ip = string(regx.Get([]byte("ipbh"), line)[1])

		case rx.NAME.Match(line):
			name := regx.Get([]byte("name"), line)
			switch string(name[1]) {
			case "description":
				o.desc = string(name[2])

			case blackhole:
				o.ip = string(name[2])

			case files:
				o.file = string(name[2])
				o.ltype = string(name[1])
				c.tree[tnode].Objects.x = append(c.tree[tnode].Objects.x, o)

			case "prefix":
				o.prefix = string(name[2])

			case urls:
				o.ltype = string(name[1])
				o.url = string(name[2])
				c.tree[tnode].Objects.x = append(c.tree[tnode].Objects.x, o)
			}

		case rx.DESC.Match(line) || rx.CMNT.Match(line) || rx.MISC.Match(line):
			continue LINE

		case rx.RBRC.Match(line):
			if len(nodes) > 1 {
				nodes = nodes[:len(nodes)-1] // pop last node
				tnode = nodes[len(nodes)-1]
			}
		}
	}

	if len(c.tree) < 1 {
		return errors.New("Configuration data is empty, cannot continue")
	}

	return nil
}