Pārlūkot izejas kodu

added packet trace for AJP13 live capture

Tobias Begalke 7 gadi atpakaļ
vecāks
revīzija
11b387f5d5
1 mainītis faili ar 23 papildinājumiem un 0 dzēšanām
  1. 23 0
      main.go

+ 23 - 0
main.go

@@ -17,6 +17,7 @@ import (
 	"github.com/google/gopacket"
 	"github.com/google/gopacket/layers"
 	"github.com/google/gopacket/pcap"
+	"github.com/kr/pretty"
 	"github.com/nats-io/nats"
 	"github.com/nats-io/nats/encoders/protobuf"
 
@@ -37,6 +38,7 @@ var (
 	requestsFile          = flag.String("requests", "", "CSV file containing requests (IP and URL)")
 	protocol              = flag.String("protocol", "http", "which protocol to parse: http or ajp13")
 	useXForwardedAsSource = flag.Bool("use-x-forwarded", false, "Use the IP address in X-Forwarded-For as source")
+	trace                 = flag.Bool("trace", false, "Trace the packet capturing")
 	configFile            = flag.String("config", "", "The location of the TOML config file")
 
 	beQuiet   = flag.Bool("quiet", true, "Be quiet")
@@ -69,6 +71,7 @@ type Config struct {
 	UseXForwardedAsSource bool
 	Quiet                 bool
 	Protocol              string
+	Trace                 bool
 }
 
 type duration struct {
@@ -94,6 +97,7 @@ func (c Config) print() {
 	fmt.Printf("UseXForwardedAsSource: %t\n", c.UseXForwardedAsSource)
 	fmt.Printf("Protocol:              %s\n", c.Protocol)
 	fmt.Printf("Quiet:                 %t\n", c.Quiet)
+	fmt.Printf("Trace:                 %t\n", c.Trace)
 }
 
 func init() {
@@ -250,15 +254,29 @@ func processPacket(packet gopacket.Packet) {
 		request.Source = request.XRealIP
 	}
 
+	if *trace {
+		fmt.Println("Request for NATS")
+		pretty.Println(request)
+	}
+
 	natsEC.Publish(config.NatsQueue, &request)
 }
 
 func processAJP13(request *data.Request, appData []byte) error {
+	if *trace {
+		fmt.Printf("packet: %v\n", appData)
+	}
+
 	a, err := ajp13.Parse(appData)
 	if err != nil {
 		return fmt.Errorf("Failed to parse AJP13 request: %s", err)
 	}
 
+	if *trace {
+		fmt.Println("AJP13")
+		pretty.Println(a)
+	}
+
 	request.Url = a.URI
 	request.Method = a.Method()
 	request.Host = a.Server
@@ -311,6 +329,11 @@ func processAJP13(request *data.Request, appData []byte) error {
 		}
 	}
 
+	if *trace {
+		fmt.Println("Request")
+		pretty.Println(request)
+	}
+
 	return nil
 }