GoReplay流量复制

场景

1、正常接收服务,测试服务不存在

没有问题、没有输出日志

2、正常接收服务,测试服务掉线再上线

没有问题、没有日志输出

3、正常接收服务,测试服务上下文改变

无法发送到测试服务

命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#捕获5673端口的请求和响应输出到stdout(用于调试)
#--input-raw-track-response用于捕获response,不加这个参数只捕获请求
#--input-raw-override-snaplen将捕获的snaplen覆盖为64k,避免抓取的流量被截断
./gor --input-raw :5673 --output-stdout --input-raw-track-response --input-raw-override-snaplen
# 捕获5673端口的请求和响应并转发到x.x.x.x:5673服务上,output-http可以跟多个,会将相同的流量转发到多个端点
./gor --input-raw :5673 --output-http http://x.x.x.x:5673 --input-raw-track-response --input-raw-override-snaplen
# 捕获5673端口的请求和响应输出到文件request.gor
./gor --input-raw :5673 --output-file request.gor --input-raw-track-response --input-raw-override-snaplen
# 捕获5673端口的请求和响应输出到文件文件格式以当前日期命名
./gor --input-raw :5673 --output-file requests-%Y-%m-%d.log --input-raw-override-snaplen
#捕获5673端口的请求和响应输出到屏幕,并在请求header中添加‘Real-IP:真实ip’的字段,如果有则覆盖原有值
./gor --input-raw :5673 --output-stdout --input-raw-track-response --input-raw-override-snaplen --input-raw-realip-header "Real-IP"
# --output-file-append gor默认以块的形式写入文件,默认256块(可以理解为256次请求),使用此参数可以将所有的块写入一个文件,文件大小限制默认32mb
./gor --input-raw :5673 --output-file request.gor --input-raw-track-response --input-raw-override-snaplen --output-file-append
#--output-file-size-limit和--output-file-queue-limit选项设置块限制,默认值分别为32mb、256,文件大小可以使用kb、mb、gb限制,只想要大小限制,你可以设置--output-file-queue-limit为0,反之亦然
#设置录制的文件大小为40kb,不限制块大小
./gor --input-raw :5673 --output-file request.gor --input-raw-override-snaplen --output-file-size-limit 40kb --output-file-queue-limit 0
1
2
3
4
5
6
7
8
9
10
11
# 从request_0.gor文件中回放请求到x.x.x.x:5673服务上
./gor --input-file request_0.gor --output-http http://x.x.x.x:5673
# --input-file-loop从request_0.gor文件中回放请求到x.x.x.x:5673服务上,循环回放
./gor --input-file request_0.gor --output-http http://x.x.x.x:5673 --input-file-loop
# 以2倍的速度从requests-2022-01-18_0.log文件中回放请求到x.x.x.x:5673服务上
./gor --input-file 'requests-2022-01-18_0.log|200%' --output-http http://x.x.x.x:5673
#模拟从文件requests-2022-01-18_0.log回放查看需要回放需要执行多长时间
#--input-file-dry-run模拟从数据源读取并不真正的回放,可以获得回放的时间、回放的请求数等等
./gor --input-file ./gortest/requests-2022-01-18_0.log --output-http http://x.x.x.x:5673 --input-file-dry-run
#这里使用了通配符*,gor会自动读取匹配的所有文件并按照时间自动排序
./gor --input-file './gortest/requests-2022-01-18_*' --output-http http://x.x.x.x:5673 --input-file-dry-run
  • 回放模拟--input-file-dry-run
1
2
3
4
5
6
7
8
Records found: 17 # 请求总数据量
Malformed records: <nil> # 不符合标准的请求数量,nil表示没有
Files processed: 1
Bytes processed: 7564 # 大小
Max wait: 6.024768111s # 请求间最大等待时间
Min wait: 4.197µs # 请求间最小等待时间
First wait: 118.78604ms # 第一次请求的间隔
It will take `18.078520319s` to replay at current speed.
  • 回放状态

    1
    --stats --output-http-stats

转发其它地址

--http-rewrite-url expects value in “:” format: “:” is a dilimiter. In `` section you may use captured regexp group values. This works similar to replace method in Javascript or gsub in Ruby.

1
2
# Rewrites all `/v1/user/<user_id>/ping` requests to `/v2/user/<user_id>/ping`
gor --input-raw :8080 --output-http staging.com --http-rewrite-url /v1/user/([^\\/]+)/ping:/v2/user/$1/ping

过滤URL

Allow url regexp

1
2
# only forward requests being sent to the /api endpoint
gor --input-raw :8080 --output-http staging.com --http-allow-url /api

Disallow url regexp

1
2
# only forward requests NOT being sent to the /api... endpoint
gor --input-raw :8080 --output-http staging.com --http-disallow-url /api

Filter based on regexp of header

1
2
3
4
5
# only forward requests with an api version of 1.0x
gor --input-raw :8080 --output-http staging.com --http-allow-header api-version:^1\.0\d

# only forward requests NOT containing User-Agent header value "Replayed by Gor"
gor --input-raw :8080 --output-http staging.com --http-disallow-header "User-Agent: Replayed by Gor"

Filter based on HTTP method

Requests not matching a specified whitelist can be filtered out. For example to strip non-nullipotent requests:

1
2
3
gor --input-raw :80 --output-http "http://staging.server" \
--http-allow-method GET \
--http-allow-method OPTIONS

下载

1
2
3
4
mkdir goreplay && cd goreplay

wget https://github.com/buger/goreplay/releases/download/1.3.3/gor_1.3.3_x64.tar.gz
tar -zxf gor_1.3.3_x64.tar.gz

本文地址: https://github.com/maxzhao-it/blog/post/823fd600/