Capture Traffic and Replay It With GoReplay (gor)

GoReplay or gor is an open-source tool to monitor incoming traffic to your server. It is written in Go. We can record the traffic to a file, then replay it again to a specified URL. It is a great tool to have.

I usually use gor for debugging. But there are more usages of gor, such as monitoring and analyzing traffic, simulating live traffic in a staging server, load testing, etc. We will cover the basic usage of gor in this article.

How to install gor

You can download compiled binary of gor here, or you can download the source code https://github.com/buger/goreplay and compile it your self.

Capturing traffic with gor

We can use this command to start capturing traffic with gor. Sudo is required to run this command.

sudo ./gor --input-raw :5005 --input-raw-track-response --output-file sampletraffic.gor
--input-raw :5005 tells gor to capture traffic from port 5005.
--input-raw-track-response is to make gor record the response too, because by default, gor doesn’t track the response.
--output-file specified the output file.

Press CTRL + C to finish capturing requests.

Below is the sample recorded request in the output file

1 a61949821661947949487690 1612733390214758300 2029300
GET /get_books?category=comic HTTP/1.1
Host: localhost:5005
User-Agent: curl/7.64.1
Accept: */*


🐵🙈🙉
2 a61949821661947949487690 1612733390214806800 2410000
HTTP/1.1 200 OK
Date: Sun, 07 Feb 2021 21:29:50 GMT
Content-Length: 42
Content-Type: text/plain; charset=utf-8

{"data": ["batman", "naruto", "doraemon"]}
🐵🙈🙉
1 84da72211661948c8678eadc 1612733472845589300 2162600
POST /add_books HTTP/1.1
Host: localhost:5005
User-Agent: curl/7.64.1
Accept: */*
Content-Length: 39
Content-Type: application/x-www-form-urlencoded

{"name":"superman", "category":"comic"}
🐵🙈🙉
2 84da72211661948c8678eadc 1612733472845632700 2560000
HTTP/1.1 200 OK
Date: Sun, 07 Feb 2021 21:31:12 GMT
Content-Length: 21
Content-Type: text/plain; charset=utf-8

{"status": "success"}
🐵🙈🙉
1 7f20725e16619497b6461f40 1612733520889231200 5138400
POST /add_user HTTP/1.1
Host: localhost:5005
User-Agent: curl/7.64.1
Accept: */*
Content-Length: 16
Content-Type: application/x-www-form-urlencoded

{"name":"logan"}
🐵🙈🙉
2 7f20725e16619497b6461f40 1612733520889298700 5932700
HTTP/1.1 400 Bad Request
Date: Sun, 07 Feb 2021 21:32:00 GMT
Content-Length: 24
Content-Type: text/plain; charset=utf-8

{"error": "bad request"}
🐵🙈🙉
You can see the URL, header, and body of the requests and responses. Very useful for debugging. We can not only analyze the recorded request but also replay it to a destination server’s URL.

Replaying gor request

We can use this command to replay the traffic from an output file.

sudo gor --input-file sampletraffic_0.gor --output-http "http://localhost:5005"
--input-file tells gor the source file of the requests.
--output-http specify a target URL for replaying the request.

After all the request from the file is replayed, you will get this message.

2021/02/07 21:49:00 [PPID 10 and PID 36] Version:1.2.0
[DEBUG][elapsed 13.5535466s]: [INPUT-FILE] FileInput: end of file 'sampletraffic_0.gor'

Conclusion

GoReplay or gor is very useful for capturing and replaying requests. It has helped me many times. The use-case of gor covered in this article is just the basic. More things can be done with gor. We will cover it sometime later.


See also