本文简要介绍Go网络抓包、引流工具GoReplay。

前言

在后端的实际开发中,会遇到以下一些场景:

  • 用户通过作弊手段绕过前端,利用抓包,进行破解,模拟,魔改交互数据进行伪装。后端程序需要对其操作记录进行重现和跟踪。
  • 某个bug在测试环境无法复现。
  • 服务压测数据和线上数据有偏差,压测数据希望能和线上接近 。

GoReplay是Go语言编写的流量回放工具,侦听器服务器捕获http流量并将其发送到重放服务器或保存到文件。重播服务器将流量转发给给定的地址。

安装

测试环境: Windows。 版本: Version:1.3.0 安装分2步骤:

  1. 前置条件: 安装npcap https://npcap.com

GoReplay可以在Windows机器上工作,但由于Windows堆栈的不同网络层的性质,它有一些细节。 默认情况下,Windows不像Unix系统那样有支持包捕获的网络驱动程序,如果您想捕获通信量,则必须单独安装它。其中一个选项是安装https://nmap.org/npcap/。

  1. 安装:这里测试使用的是 gor-1.3.3_windows.zip https://github.com/buger/goreplay/releases 下载解压即可。

捕获流量

启动两个http服务:

  1. httpServerA: http://localhost:8000 监听端口8000
  2. httpServerB: http://localhost:8001 监听端口8001

路由为 “get: “/helloworld/{name}”

监听端口8000,并输出到stdout

 ./gor --input-raw :8000 --output-stdout

打开浏览器请求接口 http://localhost:8000/helloworld/lilei

在gor的命令行窗口:

1 db0a1f4000000001c344a1d3 1668261985986369000 0
GET /helloworld/lilei HTTP/1.1
Host: localhost:8000
Connection: keep-alive
sec-ch-ua: " Not A;Brand";v=" 此处省略一些系数
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
此处省略一些参数

可见,gor已经把整个http请求的完整信息记录下来。 同时gor还支持输出到文件和ElasticSearch,进行回放和分析。记录到文件request.gor

 ./gor --input-raw :8000 --output-file request.gor --input-raw-track-response --input-raw-override-snaplen

打开浏览器请求接口 http://localhost:8000/helloworld/hanmeimei

写入到文件 request_0.gor

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ---- 
-a----        2022/11/12     21:36           1876 request_0.gor

request_0 内容如下:

1 d4571f4000000001b7adc1a6 1668260139796335000 0
GET /helloworld/lilei HTTP/1.1
Host: localhost:8000
Connection: keep-alive
sec-ch-ua: " Not A;Brand";v="99", 此处省略一些参数
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64), 此处省略一些参数
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
此处省略一些参数


🐵🙈🙉
2 d4571f4000000001b7adc1a6 1668260139796652000 0
HTTP/1.1 200 OK
Content-Type: application/json
Date: Sat, 12 Nov 2022 13:35:39 GMT
Content-Length: 25

{"message":"Hello lilei"}
🐵🙈🙉
1 d4571f4000000001b7adc22b 1668260148757949000 0
GET /helloworld/hanmeimei HTTP/1.1
Host: localhost:8000
Connection: keep-alive
sec-ch-ua: " Not A;Brand";v="99", 此处省略一些参数
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) , 此处省略一些参数
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
此处省略一些参数


🐵🙈🙉
2 d4571f4000000001b7adc22b 1668260148758268000 0
此处省略一些参数

{"message":"Hello hanmeimei"}
🐵🙈🙉

流量回放

接着将该文件request_0.gor 进行流量回放,并转发到另外一台服务器(HttpServerB)上:

./gor --input-file request_0.gor --output-http http://localhost:8001

观察HttpServerB服务器控制台输出:

INFO ts=2022-11-12T21:37:26+08:00 caller=greeter.go:44 service.id=LAPTOP-H3HBMV8A service.name= service.version= trace.id= span.id= msg=CreateGreeter: lilei
INFO ts=2022-11-12T21:37:34+08:00 caller=greeter.go:44 service.id=LAPTOP-H3HBMV8A service.name= service.version= trace.id= span.id= msg=CreateGreeter: hanmeimei

从日志中可以看到两个请求(http://localhost:8000/helloworld/ilei、http://localhost:8000/helloworld/hanmeimei)分别重放请求到了HttpServerB。

gor支持进行流量缩小、放大、倍速重放,实现真实流量的压测效果。 更多命令 ./gor -help 查看

参考