From 9a2187ff9641cfaf79aa62e8c2ac3a8851b3b422 Mon Sep 17 00:00:00 2001 From: dimagolomozy Date: Wed, 9 Jun 2021 11:46:56 +0300 Subject: [PATCH 1/9] more exact bpf filter for only incoming traffic --- capture/capture.go | 60 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/capture/capture.go b/capture/capture.go index 16a81010..914a830f 100644 --- a/capture/capture.go +++ b/capture/capture.go @@ -9,6 +9,7 @@ import ( "net" "os" "runtime" + "strings" "sync" "time" @@ -170,21 +171,31 @@ func (l *Listener) ListenBackground(ctx context.Context, handler PacketHandler) func (l *Listener) Filter(ifi net.Interface) (filter string) { // https://www.tcpdump.org/manpages/pcap-filter.7.html - port := fmt.Sprintf("portrange 0-%d", 1<<16-1) - if l.port != 0 { - port = fmt.Sprintf("port %d", l.port) + hosts := []string{l.host} + if listenAll(l.host) || isDevice(l.host, ifi) { + hosts = interfaceAddresses(ifi) } - filter = fmt.Sprintf("%s dst %s", l.Transport, port) - if l.trackResponse { - filter = fmt.Sprintf("%s %s", l.Transport, port) + + filter = portsFilter(l.Transport, "dst", l.port) + + if len(hosts) != 0 { + filter = fmt.Sprintf("((%s) and (%s))", filter, hostsFilter("dst", hosts)) + } else { + filter = fmt.Sprintf("(%s)", filter) } - if listenAll(l.host) || isDevice(l.host, ifi) { - return "(" + filter + ")" + if l.trackResponse { + responseFilter := portsFilter(l.Transport, "src", l.port) + + if len(hosts) != 0 { + responseFilter = fmt.Sprintf("((%s) and (%s))", responseFilter, hostsFilter("src", hosts)) + } else { + responseFilter = fmt.Sprintf("(%s)", responseFilter) + } + + filter = fmt.Sprintf("%s or %s", filter, responseFilter) } - filter = fmt.Sprintf("(host %s and (%s))", l.host, filter) - log.Println("BPF filter: " + filter) return } @@ -481,6 +492,18 @@ func isDevice(addr string, ifi net.Interface) bool { return addr == ifi.Name || addr == fmt.Sprintf("%d", ifi.Index) || (addr != "" && addr == ifi.HardwareAddr.String()) } +func interfaceAddresses(ifi net.Interface) []string { + var hosts []string + if addrs, err := ifi.Addrs(); err == nil { + for _, addr := range addrs { + if ip := addr.(*net.IPNet).IP.To16(); ip != nil { + hosts = append(hosts, ip.String()) + } + } + } + return hosts +} + func listenAll(addr string) bool { switch addr { case "", "0.0.0.0", "[::]", "::": @@ -489,6 +512,23 @@ func listenAll(addr string) bool { return false } +func portsFilter(transport string, direction string, port uint16) string { + if port == 0 { + return fmt.Sprintf("%s %s portrange 0-%d", transport, direction, 1<<16-1) + } + + return fmt.Sprintf("%s %s port %d", transport, direction, port) +} + +func hostsFilter(direction string, hosts []string) string { + var hostsFilters []string + for _, host := range hosts { + hostsFilters = append(hostsFilters, fmt.Sprintf("%s host %s", direction, host)) + } + + return strings.Join(hostsFilters, " or ") +} + func pcapLinkTypeLength(lType int) (int, bool) { switch layers.LinkType(lType) { case layers.LinkTypeEthernet: From 061a02e370cd16d9d3f84fdc27bdc0de75fdb2d5 Mon Sep 17 00:00:00 2001 From: dimagolomozy Date: Wed, 9 Jun 2021 11:47:12 +0300 Subject: [PATCH 2/9] evaluate filters for each nic --- capture/capture.go | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/capture/capture.go b/capture/capture.go index 914a830f..439987e7 100644 --- a/capture/capture.go +++ b/capture/capture.go @@ -270,13 +270,7 @@ func (l *Listener) PcapHandle(ifi net.Interface) (handle *pcap.Handle, err error if err != nil { return nil, fmt.Errorf("PCAP Activate device error: %q, interface: %q", err, ifi.Name) } - if l.BPFFilter != "" { - if l.BPFFilter[0] != '(' || l.BPFFilter[len(l.BPFFilter)-1] != ')' { - l.BPFFilter = "(" + l.BPFFilter + ")" - } - } else { - l.BPFFilter = l.Filter(ifi) - } + l.BPFFilter = l.Filter(ifi) fmt.Println("Interface:", ifi.Name, ". BPF Filter:", l.BPFFilter) err = handle.SetBPFFilter(l.BPFFilter) if err != nil { @@ -295,13 +289,7 @@ func (l *Listener) SocketHandle(ifi net.Interface) (handle Socket, err error) { if err = handle.SetPromiscuous(l.Promiscuous || l.Monitor); err != nil { return nil, fmt.Errorf("promiscuous mode error: %q, interface: %q", err, ifi.Name) } - if l.BPFFilter != "" { - if l.BPFFilter[0] != '(' || l.BPFFilter[len(l.BPFFilter)-1] != ')' { - l.BPFFilter = "(" + l.BPFFilter + ")" - } - } else { - l.BPFFilter = l.Filter(ifi) - } + l.BPFFilter = l.Filter(ifi) fmt.Println("BPF Filter: ", l.BPFFilter) if err = handle.SetBPFFilter(l.BPFFilter); err != nil { handle.Close() @@ -427,16 +415,12 @@ func (l *Listener) activatePcapFile() (err error) { if handle, e = pcap.OpenOffline(l.host); e != nil { return fmt.Errorf("open pcap file error: %q", e) } - if l.BPFFilter != "" { - if l.BPFFilter[0] != '(' || l.BPFFilter[len(l.BPFFilter)-1] != ')' { - l.BPFFilter = "(" + l.BPFFilter + ")" - } - } else { - addr := l.host - l.host = "" - l.BPFFilter = l.Filter(net.Interface{}) - l.host = addr - } + + tmp := l.host + l.host = "" + l.BPFFilter = l.Filter(net.Interface{}) + l.host = tmp + if e = handle.SetBPFFilter(l.BPFFilter); e != nil { handle.Close() return fmt.Errorf("BPF filter error: %q, filter: %s", e, l.BPFFilter) From d3152905644a2e464509981ad075097209ed4969 Mon Sep 17 00:00:00 2001 From: Erik Schweller Date: Tue, 8 Jun 2021 14:05:16 -0400 Subject: [PATCH 3/9] Address issues flagged by (#938) --- elasticsearch.go | 2 +- input_kafka.go | 2 +- output_s3.go | 2 +- output_tcp.go | 2 +- plugins.go | 2 +- proto/proto.go | 2 +- s3_reader.go | 2 +- settings.go | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/elasticsearch.go b/elasticsearch.go index af57db14..6914cc5f 100644 --- a/elasticsearch.go +++ b/elasticsearch.go @@ -57,7 +57,7 @@ type ESRequestResponse struct { // // Proper format is: scheme://[userinfo@]host/index_name // userinfo is: user[:password] -// net/url.Parse() does not fail if scheme is not provided but actualy does not +// net/url.Parse() does not fail if scheme is not provided but actually does not // handle URI properly. // So we must 'validate' URI format to match requirements to use net/url.Parse() func parseURI(URI string) (err error, index string) { diff --git a/input_kafka.go b/input_kafka.go index 9f660bd7..dc33e213 100644 --- a/input_kafka.go +++ b/input_kafka.go @@ -9,7 +9,7 @@ import ( "github.com/Shopify/sarama/mocks" ) -// KafkaInput is used for recieving Kafka messages and +// KafkaInput is used for receiving Kafka messages and // transforming them into HTTP payloads. type KafkaInput struct { config *InputKafkaConfig diff --git a/output_s3.go b/output_s3.go index 5779a3fc..cdebfbf4 100644 --- a/output_s3.go +++ b/output_s3.go @@ -63,7 +63,7 @@ func NewS3Output(pathTemplate string, config *FileOutputConfig) *S3Output { func (o *S3Output) connect() { if o.session == nil { o.session = session.Must(session.NewSession(awsConfig())) - log.Println("[S3 Output] S3 connection succesfully initialized") + log.Println("[S3 Output] S3 connection successfully initialized") } } diff --git a/output_tcp.go b/output_tcp.go index 79f7f3b1..f03e8ac4 100644 --- a/output_tcp.go +++ b/output_tcp.go @@ -11,7 +11,7 @@ import ( // TCPOutput used for sending raw tcp payloads // Currently used for internal communication between listener and replay server -// Can be used for transfering binary payloads like protocol buffers +// Can be used for transferring binary payloads like protocol buffers type TCPOutput struct { address string limit int diff --git a/plugins.go b/plugins.go index 7ea6ab32..b1cef482 100644 --- a/plugins.go +++ b/plugins.go @@ -5,7 +5,7 @@ import ( "strings" ) -// Message represents data accross plugins +// Message represents data across plugins type Message struct { Meta []byte // metadata Data []byte // actual data diff --git a/proto/proto.go b/proto/proto.go index 3de61da0..bb5a7761 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -198,7 +198,7 @@ func Body(payload []byte) []byte { return payload[pos:] } -// Path takes payload and retuns request path: Split(firstLine, ' ')[1] +// Path takes payload and returns request path: Split(firstLine, ' ')[1] func Path(payload []byte) []byte { if !HasRequestTitle(payload) { return nil diff --git a/s3_reader.go b/s3_reader.go index 46198dde..5a26b839 100644 --- a/s3_reader.go +++ b/s3_reader.go @@ -60,7 +60,7 @@ func NewS3ReadCloser(path string) *S3ReadCloser { bucket, key := parseS3Url(path) sess := session.Must(session.NewSession(awsConfig())) - log.Println("[S3 Input] S3 connection succesfully initialized", path) + log.Println("[S3 Input] S3 connection successfully initialized", path) return &S3ReadCloser{ bucket: bucket, diff --git a/settings.go b/settings.go index 928de3b8..9a9cffac 100644 --- a/settings.go +++ b/settings.go @@ -123,7 +123,7 @@ func init() { flag.StringVar(&Settings.OutputFileConfig.BufferPath, "output-file-buffer", "/tmp", "The path for temporary storing current buffer: \n\tgor --input-raw :80 --output-file s3://mybucket/logs/%Y-%m-%d.gz --output-file-buffer /mnt/logs") - flag.BoolVar(&Settings.PrettifyHTTP, "prettify-http", false, "If enabled, will automatically decode requests and responses with: Content-Encoding: gzip and Transfer-Encoding: chunked. Useful for debugging, in conjuction with --output-stdout") + flag.BoolVar(&Settings.PrettifyHTTP, "prettify-http", false, "If enabled, will automatically decode requests and responses with: Content-Encoding: gzip and Transfer-Encoding: chunked. Useful for debugging, in conjunction with --output-stdout") // input raw flags flag.Var(&Settings.InputRAW, "input-raw", "Capture traffic from given port (use RAW sockets and require *sudo* access):\n\t# Capture traffic from 8080 port\n\tgor --input-raw :8080 --output-http staging.com") From 76fe71c0fd3d2bb62c19cc7c94be13f3d40bc630 Mon Sep 17 00:00:00 2001 From: Erik Schweller Date: Tue, 8 Jun 2021 15:43:19 -0400 Subject: [PATCH 4/9] Go report corrections (#939) * A few misspell corrections * apply 'gofmt -s' --- capture/doc.go | 2 +- capture/dump.go | 2 +- capture/sock_linux.go | 2 +- input_file.go | 8 ++++---- input_file_test.go | 18 +++++++++--------- kafka.go | 3 +-- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/capture/doc.go b/capture/doc.go index a22519cc..65c05e4a 100644 --- a/capture/doc.go +++ b/capture/doc.go @@ -22,7 +22,7 @@ if err := listener.Listen(context.Background(), handler); err != nil { // handle error } // or -errCh := listener.ListenBackground(context.Background(), handler) // runs in the backgorund +errCh := listener.ListenBackground(context.Background(), handler) // runs in the background select { case err := <- errCh: // handle error diff --git a/capture/dump.go b/capture/dump.go index 640123dc..a4974f2f 100644 --- a/capture/dump.go +++ b/capture/dump.go @@ -54,7 +54,7 @@ func NewWriterNanos(w io.Writer) *Writer { // NewWriter returns a new writer object, for writing packet data out // to the given writer. If this is a new empty writer (as opposed to // an append), you must call WriteFileHeader before WritePacket. -// Packet timestamps are written witn microsecond precision. +// Packet timestamps are written with microsecond precision. // // // Write a new file: // f, _ := os.Create("/tmp/file.pcap") diff --git a/capture/sock_linux.go b/capture/sock_linux.go index 55dcccd5..00c591eb 100644 --- a/capture/sock_linux.go +++ b/capture/sock_linux.go @@ -217,7 +217,7 @@ func (sock *SockRaw) SetBPFFilter(expr string) error { return unix.SetsockoptSockFprog(sock.fd, unix.SOL_SOCKET, unix.SO_ATTACH_FILTER, fprog) } -// SetPromiscuous sets promiscous mode to the required value. for better result capture on all interfaces instead. +// SetPromiscuous sets promiscuous mode to the required value. for better result capture on all interfaces instead. // If it is enabled, traffic not destined for the interface will also be captured. func (sock *SockRaw) SetPromiscuous(b bool) error { sock.mu.Lock() diff --git a/input_file.go b/input_file.go index 681680ad..a039d9cb 100644 --- a/input_file.go +++ b/input_file.go @@ -162,10 +162,10 @@ func (i *FileInput) init() (err error) { for _, c := range resp.Contents { matches = append(matches, "s3://"+bucket+"/"+(*c.Key)) } - } else if matches, err = filepath.Glob(i.path); err != nil{ - Debug(0, "[INPUT-FILE] Wrong file pattern", i.path, err) - return - + } else if matches, err = filepath.Glob(i.path); err != nil { + Debug(0, "[INPUT-FILE] Wrong file pattern", i.path, err) + return + } if len(matches) == 0 { diff --git a/input_file_test.go b/input_file_test.go index fac5e01b..91b9bcc6 100644 --- a/input_file_test.go +++ b/input_file_test.go @@ -29,9 +29,9 @@ func TestInputFileWithGET(t *testing.T) { // The read request should match the original request if err != nil { t.Error(err) - } else if !expectedCaptureFile.PayloadsEqual(readPayloads){ - t.Error("Request read back from file should match") - + } else if !expectedCaptureFile.PayloadsEqual(readPayloads) { + t.Error("Request read back from file should match") + } } @@ -52,9 +52,9 @@ func TestInputFileWithPayloadLargerThan64Kb(t *testing.T) { // The read request should match the original request if err != nil { t.Error(err) - } else if !expectedCaptureFile.PayloadsEqual(readPayloads){ - t.Error("Request read back from file should match") - + } else if !expectedCaptureFile.PayloadsEqual(readPayloads) { + t.Error("Request read back from file should match") + } } @@ -80,9 +80,9 @@ func TestInputFileWithGETAndPOST(t *testing.T) { // The read requests should match the original request if err != nil { t.Error(err) - } else if !expectedCaptureFile.PayloadsEqual(readPayloads){ - t.Error("Request read back from file should match") - + } else if !expectedCaptureFile.PayloadsEqual(readPayloads) { + t.Error("Request read back from file should match") + } } diff --git a/kafka.go b/kafka.go index 67723458..46bc6c63 100644 --- a/kafka.go +++ b/kafka.go @@ -51,7 +51,6 @@ type KafkaMessage struct { ReqHeaders map[string]string `json:"Req_Headers,omitempty"` } - // NewTLSConfig loads TLS certificates func NewTLSConfig(clientCertFile, clientKeyFile, caCertFile string) (*tls.Config, error) { tlsConfig := tls.Config{} @@ -63,7 +62,7 @@ func NewTLSConfig(clientCertFile, clientKeyFile, caCertFile string) (*tls.Config return &tlsConfig, errors.New("missing TLS client certificate in kafka") } // Load client cert - if (clientCertFile != "") && (clientKeyFile != "") { + if (clientCertFile != "") && (clientKeyFile != "") { cert, err := tls.LoadX509KeyPair(clientCertFile, clientKeyFile) if err != nil { return &tlsConfig, err From f2857424b0f1c6c5589996cc5f4e4264a559120c Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Wed, 19 May 2021 20:19:23 +0300 Subject: [PATCH 5/9] Update to use Go 1.15 and latest libpcap --- Dockerfile.dev | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile.dev b/Dockerfile.dev index f76930ac..ea58441a 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -1,9 +1,9 @@ -FROM golang:1.14 +FROM golang:1.15 RUN apt-get update && apt-get install ruby vim-common -y RUN apt-get install flex bison -y -RUN wget http://www.tcpdump.org/release/libpcap-1.9.1.tar.gz && tar xzf libpcap-1.9.1.tar.gz && cd libpcap-1.9.1 && ./configure && make install +RUN wget http://www.tcpdump.org/release/libpcap-1.10.0.tar.gz && tar xzf libpcap-1.10.0.tar.gz && cd libpcap-1.10.0 && ./configure && make install RUN go get github.com/google/gopacket RUN go get -u golang.org/x/lint/golint From ebe9e222f363b91c2146fc5fa21fbac42595f1a8 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Wed, 9 Jun 2021 07:04:27 +0300 Subject: [PATCH 6/9] Bump version --- version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.go b/version.go index 349ef179..5cecaedb 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,4 @@ package main // VERSION the current version of goreplay -const VERSION = "1.2.0" +const VERSION = "1.3.0" From f00817c13eac7ce0d3307d2866aa3ae86ddd0e71 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Wed, 9 Jun 2021 09:46:41 +0300 Subject: [PATCH 7/9] Add windows support (#940) For a long time there were no official binaries for the windows platform. One of the reasons is the complexities of the build toolchain. Not only CGO is required, but also installing the needed libraries and header files, not talking about mingw and etc. 2 weeks ago [golang-crossbuild](https://github.com/elastic/golang-crossbuild) project added native support for Libpcap based applications. Windows support is based on the WinPcap which is a bit (a lot) outdated, BUT, since we depend only on its interface, it is still possible to use projects like npcap https://nmap.org/npcap/#download. Npcap needs be installed with WinPcap compatibe mode (checkbox during installation) It is also possibe install it in silent mode like this: `npcap-0.86.exe /S /winpcap_mode=yes` After PR merge, will be updated related documentation. Additionally fix dependency on "unix" package (apparently it can be totally replaced using universal syscall package) --- .gitignore | 1 + Makefile | 18 ++++++++++++++++-- capture/capture.go | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 04a6cc6a..fda9013f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ *.rpm *.dep *.pkg +*.exe *.out diff --git a/Makefile b/Makefile index ad8e8709..a3132314 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ FPMCOMMON= \ -s dir \ -C /tmp/gor-build \ -release: release-x64 release-mac +release: release-x64 release-mac release-windows release-bin: docker run -v `pwd`:$(SOURCE_PATH) -t --env GOOS=linux --env GOARCH=amd64 -i $(CONTAINER) go build -o $(BIN_NAME) -tags netgo $(LDFLAGS) @@ -58,11 +58,25 @@ release-mac: fpm $(FPMCOMMON) -a amd64 -t osxpkg ./=/usr/local/bin rm -rf /tmp/gor-build +release-windows: + echo $(pwd) + docker run -it --rm \ + -v `pwd`:/go/src/github.com/buger/goreplay \ + -w /go/src/github.com/buger/goreplay \ + -e CGO_ENABLED=1 \ + docker.elastic.co/beats-dev/golang-crossbuild:1.16.4-main \ + --build-cmd "VERSION=make build" \ + -p "windows/amd64" + + mv ./gor ./gor-$(VERSION)$(PREFIX).exe + +build: + go build -o $(BIN_NAME) $(LDFLAGS) install: go install $(MAC_LDFLAGS) -build: +build-env: docker build -t $(CONTAINER) -f Dockerfile.dev . profile: diff --git a/capture/capture.go b/capture/capture.go index 439987e7..dcb539b3 100644 --- a/capture/capture.go +++ b/capture/capture.go @@ -11,6 +11,7 @@ import ( "runtime" "strings" "sync" + "syscall" "time" "github.com/buger/goreplay/size" @@ -19,7 +20,6 @@ import ( "github.com/google/gopacket" "github.com/google/gopacket/layers" "github.com/google/gopacket/pcap" - "golang.org/x/sys/unix" ) // PacketHandler is a function that is used to handle packets @@ -334,7 +334,7 @@ func (l *Listener) read(handler PacketHandler) { if enext, ok := err.(pcap.NextError); ok && enext == pcap.NextErrorTimeoutExpired { continue } - if eno, ok := err.(unix.Errno); ok && eno.Temporary() { + if eno, ok := err.(syscall.Errno); ok && eno.Temporary() { continue } if enet, ok := err.(*net.OpError); ok && (enet.Temporary() || enet.Timeout()) { From 361ea8967ebfe17cc0d7b95680dad8f5503b6ff0 Mon Sep 17 00:00:00 2001 From: Dima Golomozy Date: Wed, 9 Jun 2021 17:43:06 +0300 Subject: [PATCH 8/9] support only 1.15 and above (#942) --- .github/workflows/ci-test.yaml | 2 +- go.mod | 3 +-- go.sum | 2 -- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci-test.yaml b/.github/workflows/ci-test.yaml index a099b081..0ce0fa06 100644 --- a/.github/workflows/ci-test.yaml +++ b/.github/workflows/ci-test.yaml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - go-version: [1.14.x, 1.15.x] # two latest minor versions + go-version: [1.15.x, 1.16.x] # two latest minor versions runs-on: ubuntu-latest steps: - name: update package index diff --git a/go.mod b/go.mod index c2595460..9336261e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/buger/goreplay -go 1.14 +go 1.15 require ( github.com/Shopify/sarama v1.26.4 @@ -11,7 +11,6 @@ require ( github.com/google/gopacket v1.1.18 github.com/klauspost/compress v1.10.10 // indirect github.com/mattbaird/elastigo v0.0.0-20170123220020-2fe47fd29e4b - github.com/pbnjay/memory v0.0.0-20201129165224-b12e5d931931 github.com/pierrec/lz4 v2.5.2+incompatible // indirect github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect diff --git a/go.sum b/go.sum index 0f7b1fd5..3cb25cfc 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattbaird/elastigo v0.0.0-20170123220020-2fe47fd29e4b h1:v29yPGHhOqw7VHEnTeQFAth3SsBrmwc8JfuhNY0G34k= github.com/mattbaird/elastigo v0.0.0-20170123220020-2fe47fd29e4b/go.mod h1:5MWrJXKRQyhQdUCF+vu6U5c4nQpg70vW3eHaU0/AYbU= -github.com/pbnjay/memory v0.0.0-20201129165224-b12e5d931931 h1:EeWknjeRU+R3O4ghG7XZCpgSfJNStZyEP8aWyQwJM8s= -github.com/pbnjay/memory v0.0.0-20201129165224-b12e5d931931/go.mod h1:RMU2gJXhratVxBDTFeOdNhd540tG57lt9FIUV0YLvIQ= github.com/pierrec/lz4 v2.4.1+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pierrec/lz4 v2.5.2+incompatible h1:WCjObylUIOlKy/+7Abdn34TLIkXiA4UWUMhxq9m9ZXI= github.com/pierrec/lz4 v2.5.2+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= From b1972fb0a842661d07bdff774c7cedb44a7d1525 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Wed, 9 Jun 2021 18:25:31 +0300 Subject: [PATCH 9/9] Add way to test looping from webserver to itsef --- gor.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gor.go b/gor.go index 06051f70..abad0e01 100644 --- a/gor.go +++ b/gor.go @@ -22,8 +22,13 @@ var ( memprofile = flag.String("memprofile", "", "write memory profile to this file") ) -func loggingMiddleware(next http.Handler) http.Handler { +func loggingMiddleware(addr string, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/loop" { + _, err := http.Get("http://" + addr) + log.Println(err) + } + rb, _ := httputil.DumpRequest(r, false) log.Println(string(rb)) next.ServeHTTP(w, r) @@ -45,7 +50,7 @@ func main() { Debug(0, "Started example file server for current directory on address ", args[1]) - log.Fatal(http.ListenAndServe(args[1], loggingMiddleware(http.FileServer(http.Dir(dir))))) + log.Fatal(http.ListenAndServe(args[1], loggingMiddleware(args[1], http.FileServer(http.Dir(dir))))) } else { flag.Parse() checkSettings()