Snippets
Created by
xchandan
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | package main
import (
"bufio"
"fmt"
"os"
"regexp"
)
func check_err(e error) {
if e != nil {
panic(e)
}
}
var PARSE_CHAIN_MATCH_ACTION_PATTERN = regexp.MustCompile("^-A (?P<chain>[^ ]*) (?P<match>.*) -j (?P<action>.*)$")
var PARSE_CHAIN_ACTION_PATTERN = regexp.MustCompile("^-A (?P<chain>[^ ]*) -j (?P<action>.*)$")
var PARSE_SKIP_PATTERN = regexp.MustCompile("^[#*]|^COMMIT")
var PARSE_TABLES = regexp.MustCompile("^:(?P<table>[^ ]*)")
var TABLES []string
const HEADER = "\033[95m"
const BLUE = "\033[94m"
const GREEN = "\033[92m"
const WARNING = "\033[93m"
const FAIL = "\033[91m"
const ENDC = "\033[0m"
const BOLD = "\033[1m"
const UNDERLINE = "\033[4m"
func customTable(action string) bool {
for _, name := range TABLES {
if name == action {
return true
}
}
return false
}
func colorAction(action string) string {
if !customTable(action) {
return fmt.Sprintf("%s%s%s", GREEN, action, ENDC)
} else {
return fmt.Sprintf("%s%s%s", BLUE, action, ENDC)
}
}
func matchPrint(input []string) {
for _, line := range input {
if PARSE_SKIP_PATTERN.MatchString(line) {
//fmt.Println("Skip", line)
continue
}
match := PARSE_TABLES.FindStringSubmatch(line)
if match != nil {
//fmt.Println("Table", match[1])
TABLES = append(TABLES, match[1])
continue
}
match = PARSE_CHAIN_MATCH_ACTION_PATTERN.FindStringSubmatch(line)
if match != nil {
//fmt.Println("Chain Match Action", line)
chain := match[1]
rule := match[2]
action := match[3]
action = colorAction(action)
fmt.Printf("%s -> %s \n\tif %s\n", chain, action, rule)
continue
}
match = PARSE_CHAIN_ACTION_PATTERN.FindStringSubmatch(line)
if match != nil {
//fmt.Println("Chain Action", line)
chain := match[1]
action := match[2]
action = colorAction(action)
fmt.Printf("%s -> %s\n", chain, action)
continue
}
fmt.Println("Not Matched", line)
}
}
func main() {
f, e := os.Open("/tmp/dump.txt")
check_err(e)
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
var input []string
for scanner.Scan() {
input = append(input, scanner.Text())
}
matchPrint(input)
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.