diff --git a/frontend/templateHelper.go b/frontend/templateHelper.go index 7854ff9..b832d70 100644 --- a/frontend/templateHelper.go +++ b/frontend/templateHelper.go @@ -1,24 +1,12 @@ package main import ( - "net" "net/http" + "regexp" "sort" - "strconv" "strings" ) -// Helper to check if the IP is valid -func isIP(s string) bool { - return nil != net.ParseIP(s) -} - -// Helper to check if the number is valid -func isNumber(s string) bool { - _, err := strconv.Atoi(s) - return nil == err -} - func renderTemplate(w http.ResponseWriter, r *http.Request, title string, content string) { path := r.URL.Path[1:] split := strings.SplitN(path, "/", 4) @@ -75,51 +63,16 @@ func smartFormatter(s string) string { var result string result += "
"
for _, line := range strings.Split(s, "\n") {
- var isASes bool = false
-
var lineFormatted string
- words := strings.Split(line, " ")
-
- for wordID, word := range words {
- if len(word) == 0 {
- continue
- }
- if wordID > 0 && (len(words[wordID-1]) == 0 || words[wordID-1][len(words[wordID-1])-1] == ':') {
- // Insert TAB if there are multiple spaces before this word
- lineFormatted += "\t"
- } else {
- lineFormatted += " "
- }
-
- if isIP(word) {
- // Add whois link to the IP, handles IPv4 and IPv6
- lineFormatted += "" + word + ""
- } else if len(strings.Split(word, "%")) == 2 && isIP(strings.Split(word, "%")[0]) {
- // IPv6 link-local with interface name, like fd00::1%eth0
- // Add whois link to address part
- lineFormatted += "" + strings.Split(word, "%")[0] + ""
- lineFormatted += "%" + strings.Split(word, "%")[1]
- } else if len(strings.Split(word, "/")) == 2 && isIP(strings.Split(word, "/")[0]) {
- // IP with a CIDR range, like 192.168.0.1/24
- // Add whois link to first part
- lineFormatted += "" + strings.Split(word, "/")[0] + ""
- lineFormatted += "/" + strings.Split(word, "/")[1]
- } else if word == "AS:" || word == "\tBGP.as_path:" {
- // Bird will output ASNs later
- isASes = true
- lineFormatted += word
- } else if isASes && isNumber(strings.Trim(word, "()")) {
- // Remove brackets in path caused by confederation
- wordNum := strings.Trim(word, "()")
- // Bird is outputing ASNs, add whois for them
- lineFormatted += "" + word + ""
- } else {
- // Just an ordinary word, print it and done
- lineFormatted += word
- }
+ if strings.HasPrefix(strings.TrimSpace(line), "BGP.as_path:") || strings.HasPrefix(strings.TrimSpace(line), "Neighbor AS:") || strings.HasPrefix(strings.TrimSpace(line), "Local AS:") {
+ lineFormatted = regexp.MustCompile(`(\d+)`).ReplaceAllString(line, `${1}`)
+ } else {
+ lineFormatted = regexp.MustCompile(`([a-zA-Z0-9\-]*\.([a-zA-Z]{2,3}){1,2})(\s|$)`).ReplaceAllString(line, `${1}${3}`)
+ lineFormatted = regexp.MustCompile(`\[AS(\d+)`).ReplaceAllString(lineFormatted, `[AS${1}`)
+ lineFormatted = regexp.MustCompile(`(\d+\.\d+\.\d+\.\d+)`).ReplaceAllString(lineFormatted, `${1}`)
+ lineFormatted = regexp.MustCompile(`(?i)(([a-f\d]{0,4}:){3,10}[a-f\d]{0,4})`).ReplaceAllString(lineFormatted, `${1}`)
}
- lineFormatted += "\n"
- result += lineFormatted
+ result += lineFormatted + "\n"
}
result += ""
return result