From abd0eda45b1763b6bec23f56a60781a4c1f91c23 Mon Sep 17 00:00:00 2001 From: Lan Tian Date: Wed, 8 Apr 2020 18:22:09 +0800 Subject: [PATCH] Add DN42 specific whois handling --- frontend/dn42.go | 47 ++++++++++++++++++++++++++++++++++++++++ frontend/main.go | 20 ++++++++++++----- frontend/telegram_bot.go | 26 ++++------------------ 3 files changed, 65 insertions(+), 28 deletions(-) create mode 100644 frontend/dn42.go diff --git a/frontend/dn42.go b/frontend/dn42.go new file mode 100644 index 0000000..e9ef6fb --- /dev/null +++ b/frontend/dn42.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "strings" +) + +func dn42WhoisFilter(whois string) string { + commandResult := "" + isNextSection := false + skippedLines := 0 + + // Filter out some long (and useless) keys + filteredPrefix := []string{ + "descr:", "remarks:", "ds-rdata:", "auth:", "country:", + "nserver:", "status:", "pgp-fingerprint:", "mp-import:", "mp-export:", + "members:", "key:", "inetnum:", "inet6num:", "%", + } + for _, s := range strings.Split(whois, "\n") { + if len(s) == 0 { + // This line is a separation between parts of results + // Only keep last part of result + isNextSection = true + continue + } + shouldSkip := false + for _, filtered := range filteredPrefix { + if strings.HasPrefix(s, filtered) { + shouldSkip = true + } + } + if shouldSkip { + skippedLines++ + continue + } + + if isNextSection { + isNextSection = false + skippedLines = 0 + commandResult = "" + } + + commandResult += s + "\n" + } + + return commandResult + fmt.Sprintf("\n%d line(s) skipped.\n", skippedLines) +} diff --git a/frontend/main.go b/frontend/main.go index 415af8c..66f9dc2 100644 --- a/frontend/main.go +++ b/frontend/main.go @@ -8,12 +8,13 @@ import ( ) type settingType struct { - servers []string - domain string - proxyPort int - whoisServer string - listen string - dnsInterface string + servers []string + domain string + proxyPort int + whoisServer string + listen string + dnsInterface string + netSpecificMode string } var setting settingType @@ -26,6 +27,7 @@ func main() { "whois.verisign-grs.com", ":5000", "asn.cymru.com", + "", } if env := os.Getenv("BIRDLG_SERVERS"); env != "" { @@ -50,12 +52,17 @@ func main() { settingDefault.dnsInterface = env } + if env := os.Getenv("BIRDLG_NET_SPECIFIC_MODE"); env != "" { + settingDefault.netSpecificMode = env + } + serversPtr := flag.String("servers", strings.Join(settingDefault.servers, ","), "server name prefixes, separated by comma") domainPtr := flag.String("domain", settingDefault.domain, "server name domain suffixes") proxyPortPtr := flag.Int("proxy-port", settingDefault.proxyPort, "port bird-lgproxy is running on") whoisPtr := flag.String("whois", settingDefault.whoisServer, "whois server for queries") listenPtr := flag.String("listen", settingDefault.listen, "address bird-lg is listening on") dnsInterfacePtr := flag.String("dns-interface", settingDefault.dnsInterface, "dns zone to query ASN information") + netSpecificModePtr := flag.String("net-specific-mode", settingDefault.netSpecificMode, "network specific operation mode, [(none)|dn42]") flag.Parse() if *serversPtr == "" { @@ -71,6 +78,7 @@ func main() { *whoisPtr, *listenPtr, *dnsInterfacePtr, + strings.ToLower(*netSpecificModePtr), } webServerStart() diff --git a/frontend/telegram_bot.go b/frontend/telegram_bot.go index b677c45..e0dbe0c 100644 --- a/frontend/telegram_bot.go +++ b/frontend/telegram_bot.go @@ -94,28 +94,10 @@ func webHandlerTelegramBot(w http.ResponseWriter, r *http.Request) { } else if telegramIsCommand(request.Message.Text, "whois") { tempResult := whois(target) - - // Filter out some long (and useless) keys - filteredPrefix := []string{ - "descr:", "remarks:", "ds-rdata:", "auth:", "country:", - "nserver:", "status:", "pgp-fingerprint:", "mp-import:", "mp-export:", - "members:", "key:", "inetnum:", "inet6num:", "%", - } - for _, s := range strings.Split(tempResult, "\n") { - if len(s) == 0 { - continue - } - shouldSkip := false - for _, filtered := range filteredPrefix { - if strings.HasPrefix(s, filtered) { - shouldSkip = true - } - } - if shouldSkip { - continue - } - - commandResult += s + "\n" + if setting.netSpecificMode == "dn42" { + commandResult = dn42WhoisFilter(tempResult) + } else { + commandResult = tempResult } } else if telegramIsCommand(request.Message.Text, "help") {