From aa94fa12ec8b371636745a60719fc9aa59c8102a Mon Sep 17 00:00:00 2001 From: maride Date: Fri, 29 Nov 2019 14:32:07 +0100 Subject: [PATCH] Move common code into new package, 'common' --- {ethernet/arp => common}/common.go | 6 +++--- ethernet/arp/arp.go | 9 +++++---- ethernet/dhcpv4/common.go | 30 ------------------------------ ethernet/dhcpv4/hostnames.go | 3 ++- ethernet/dhcpv4/request.go | 5 +++-- ethernet/dhcpv4/response.go | 3 ++- ethernet/dns/answer.go | 13 +++++++------ ethernet/dns/common.go | 27 --------------------------- ethernet/dns/question.go | 11 ++++++----- 9 files changed, 28 insertions(+), 79 deletions(-) rename {ethernet/arp => common}/common.go (83%) delete mode 100644 ethernet/dhcpv4/common.go diff --git a/ethernet/arp/common.go b/common/common.go similarity index 83% rename from ethernet/arp/common.go rename to common/common.go index 12962d6..1ec31d9 100644 --- a/ethernet/arp/common.go +++ b/common/common.go @@ -1,9 +1,9 @@ -package arp +package common import "fmt" // Appends the appendee to the array if it does not contain appendee yet -func appendIfUnique(appendee string, array []string) []string { +func AppendIfUnique(appendee string, array []string) []string { // Iterate over all elements and check values for _, elem := range array { if elem == appendee { @@ -17,7 +17,7 @@ func appendIfUnique(appendee string, array []string) []string { } // Prints each element, along with a small ASCII tree -func printTree(strarr []string) { +func PrintTree(strarr []string) { // iterate over each element for iter, elem := range strarr { // check if we got the last element diff --git a/ethernet/arp/arp.go b/ethernet/arp/arp.go index c37e91e..63d0c3b 100644 --- a/ethernet/arp/arp.go +++ b/ethernet/arp/arp.go @@ -2,6 +2,7 @@ package arp import ( "fmt" + "git.darknebu.la/maride/pancap/common" "github.com/fatih/color" "github.com/google/gopacket" "github.com/google/gopacket/layers" @@ -33,14 +34,14 @@ func ProcessARPPacket(packet gopacket.Packet) error { if arppacket.Operation == layers.ARPRequest { // Request packet participant.asked++ - participant.askedList = appendIfUnique(net.IP(arppacket.DstProtAddress).String(), participant.askedList) + participant.askedList = common.AppendIfUnique(net.IP(arppacket.DstProtAddress).String(), participant.askedList) // Add device entry addDeviceEntry(sourceAddr, net.IP(arppacket.SourceProtAddress).String()) } else { // Response packet participant.answered++ - participant.answeredList = appendIfUnique(net.IP(arppacket.SourceProtAddress).String(), participant.answeredList) + participant.answeredList = common.AppendIfUnique(net.IP(arppacket.SourceProtAddress).String(), participant.answeredList) // Add device entry addDeviceEntry(sourceAddr, net.IP(arppacket.SourceProtAddress).String()) @@ -68,7 +69,7 @@ func printTrafficStats() { } // And print it as a tree - printTree(tmparr) + common.PrintTree(tmparr) } // Prints an overview over all connected devices in the LAN @@ -81,7 +82,7 @@ func printLANOverview() { } // And print it as a tree - printTree(tmparr) + common.PrintTree(tmparr) } // Returns the arpStats object for the given MAC address, or creates a new one diff --git a/ethernet/dhcpv4/common.go b/ethernet/dhcpv4/common.go deleted file mode 100644 index f50319d..0000000 --- a/ethernet/dhcpv4/common.go +++ /dev/null @@ -1,30 +0,0 @@ -package dhcpv4 - -import "fmt" - -// Appends the appendee to the array if it does not contain appendee yet -func appendIfUnique(appendee string, array []string) []string { - // Iterate over all elements and check values - for _, elem := range array { - if elem == appendee { - // ... found. Stop here - return array - } - } - - // None found, append - return append(array, appendee) -} - -// Prints each element, along with a small ASCII tree -func printTree(strarr []string) { - // iterate over each element - for iter, elem := range strarr { - // check if we got the last element - if iter < len(strarr) - 1 { - fmt.Printf("|- %s\n", elem) - } else { - fmt.Printf("'- %s\n\n", elem) - } - } -} diff --git a/ethernet/dhcpv4/hostnames.go b/ethernet/dhcpv4/hostnames.go index ddac92f..89b9d98 100644 --- a/ethernet/dhcpv4/hostnames.go +++ b/ethernet/dhcpv4/hostnames.go @@ -2,6 +2,7 @@ package dhcpv4 import ( "fmt" + "git.darknebu.la/maride/pancap/common" "github.com/google/gopacket/layers" "log" ) @@ -71,7 +72,7 @@ func printHostnames() { } // and print it as a tree. - printTree(tmparr) + common.PrintTree(tmparr) } // Adds the given hostname to the hostname array, or patches an existing entry if found diff --git a/ethernet/dhcpv4/request.go b/ethernet/dhcpv4/request.go index 459d767..ba6a787 100644 --- a/ethernet/dhcpv4/request.go +++ b/ethernet/dhcpv4/request.go @@ -2,6 +2,7 @@ package dhcpv4 import ( "fmt" + "git.darknebu.la/maride/pancap/common" "github.com/google/gopacket/layers" ) @@ -11,11 +12,11 @@ var ( // Processes the DHCP request packet handed over func processRequestPacket(dhcppacket layers.DHCPv4) { - requestMAC = appendIfUnique(dhcppacket.ClientHWAddr.String(), requestMAC) + requestMAC = common.AppendIfUnique(dhcppacket.ClientHWAddr.String(), requestMAC) } // Prints the summary of all DHCP request packets func printRequestSummary() { fmt.Printf("%d unique DHCP requests\n", len(requestMAC)) - printTree(requestMAC) + common.PrintTree(requestMAC) } diff --git a/ethernet/dhcpv4/response.go b/ethernet/dhcpv4/response.go index d01726f..74e65bb 100644 --- a/ethernet/dhcpv4/response.go +++ b/ethernet/dhcpv4/response.go @@ -2,6 +2,7 @@ package dhcpv4 import ( "fmt" + "git.darknebu.la/maride/pancap/common" "github.com/google/gopacket/layers" "log" ) @@ -30,7 +31,7 @@ func printResponseSummary() { } // Draw as tree - printTree(tmpaddr) + common.PrintTree(tmpaddr) } // Adds a new response entry. If an IP address was already issued or a MAC asks multiple times for DNS, the case is examined further diff --git a/ethernet/dns/answer.go b/ethernet/dns/answer.go index f2e4d5e..55e3902 100644 --- a/ethernet/dns/answer.go +++ b/ethernet/dns/answer.go @@ -2,6 +2,7 @@ package dns import ( "fmt" + "git.darknebu.la/maride/pancap/common" "github.com/google/gopacket/layers" "golang.org/x/net/publicsuffix" "log" @@ -37,16 +38,16 @@ func processDNSAnswer(answers []layers.DNSResourceRecord) { processType(answerType, answer.Type) // Append full domain and base domain - answerDomains = appendIfUnique(name, answerDomains) + answerDomains = common.AppendIfUnique(name, answerDomains) // Check if we need to add the base name to the private list _, icannManaged := publicsuffix.PublicSuffix(name) if icannManaged { // TLD is managed by ICANN, add to the base list - answerBaseDomains = appendIfUnique(basename, answerBaseDomains) + answerBaseDomains = common.AppendIfUnique(basename, answerBaseDomains) } else { // it's not managed by ICANN, so it's private - add it to the private list - answerPrivateDomains = appendIfUnique(name, answerPrivateDomains) + answerPrivateDomains = common.AppendIfUnique(name, answerPrivateDomains) } // Check if we got an A record answer @@ -71,19 +72,19 @@ func printDNSAnswerSummary() { // Output base domains answered with if len(answerBaseDomains) > 0 { fmt.Println("Answered with these base domains:") - printTree(answerBaseDomains) + common.PrintTree(answerBaseDomains) } // Output private domains if len(answerPrivateDomains) > 0 { fmt.Println("Answered with these private (non-ICANN managed) domains:") - printTree(answerPrivateDomains) + common.PrintTree(answerPrivateDomains) } // Check for public and private IPs fmt.Printf("Answered with %d public IP addresses and %d private IP addresses\n", len(answerPublicIPv4), len(answerPrivateIPv4)) if len(answerPrivateIPv4) > 0 { fmt.Println("Private IP addresses in answer:") - printTree(answerPrivateIPv4) + common.PrintTree(answerPrivateIPv4) } } diff --git a/ethernet/dns/common.go b/ethernet/dns/common.go index 3e0b0e8..707409c 100644 --- a/ethernet/dns/common.go +++ b/ethernet/dns/common.go @@ -21,20 +21,6 @@ func processType(typearr map[layers.DNSType]int, dnstype layers.DNSType) { typearr[dnstype]++ } -// Appends the appendee to the array if it does not contain appendee yet -func appendIfUnique(appendee string, array []string) []string { - // Iterate over all elements and check values - for _, elem := range array { - if elem == appendee { - // ... found. Stop here - return array - } - } - - // None found, append - return append(array, appendee) -} - // Checks if the given IP is in a private range or not func ipIsPrivate(ip net.IP) bool { // check every private IP block for our IP @@ -49,19 +35,6 @@ func ipIsPrivate(ip net.IP) bool { return false } -// Prints each element, along with a small ASCII tree -func printTree(strarr []string) { - // iterate over each element - for iter, elem := range strarr { - // check if we got the last element - if iter < len(strarr) - 1 { - fmt.Printf("|- %s\n", elem) - } else { - fmt.Printf("'- %s\n\n", elem) - } - } -} - // Generates a summary string for DNS types in the given array func generateDNSTypeSummary(typearr map[layers.DNSType]int) string { var answerarr []string diff --git a/ethernet/dns/question.go b/ethernet/dns/question.go index 998d3ad..c34ca59 100644 --- a/ethernet/dns/question.go +++ b/ethernet/dns/question.go @@ -2,6 +2,7 @@ package dns import ( "fmt" + "git.darknebu.la/maride/pancap/common" "github.com/google/gopacket/layers" "golang.org/x/net/publicsuffix" "log" @@ -36,16 +37,16 @@ func processDNSQuestion(questions []layers.DNSQuestion) { processType(questionType, question.Type) // Append full domain and base domain - questionDomains = appendIfUnique(name, questionDomains) + questionDomains = common.AppendIfUnique(name, questionDomains) // Check if we need to add the base name to the private list _, icannManaged := publicsuffix.PublicSuffix(name) if icannManaged { // TLD is managed by ICANN, add to the base list - questionBaseDomains = appendIfUnique(basename, questionBaseDomains) + questionBaseDomains = common.AppendIfUnique(basename, questionBaseDomains) } else { // it's not managed by ICANN, so it's private - add it to the private list - questionPrivateDomains = appendIfUnique(name, questionPrivateDomains) + questionPrivateDomains = common.AppendIfUnique(name, questionPrivateDomains) } } } @@ -60,12 +61,12 @@ func printDNSQuestionSummary() { // Output base domains asked for if len(questionBaseDomains) > 0 { fmt.Println("Asked for these base domains:") - printTree(questionBaseDomains) + common.PrintTree(questionBaseDomains) } // Output private domains if len(questionPrivateDomains) > 0 { fmt.Println("Asked for these private (non-ICANN managed) domains:") - printTree(questionPrivateDomains) + common.PrintTree(questionPrivateDomains) } } \ No newline at end of file