流量拷贝工具goreplay

相比大家比较熟知的tcpcopy之类的流量拷贝工具,goreplay更加简单易用。最近正好有项目迁移,新环境通过流量拷贝现有HTTP服务流量进行测试分析。类似这种需求,goreplay完全可以满足(目前使用的版本为0.16.1)。以下简单说下几种用到的使用场景。

1 不同的–output-xxxx参数适用不同的场景

1)–output-stdout
抓取监听请求,并输出到终端。此功能类似tcpdump抓包,但是打印到终端的格式更直观易读。

./goreplay --input-raw :80  --output-stdout

2)–output-file
将抓取到的请求保存到指定文件

./goreplay --input-raw :80  --output-file 'request.gor'

适用于非实时拷贝的情况,将流量存在文件里,到需要测试使用的时候,再通过使用–input-file参数将流量从文件放出来:

./goreplay --input-file ./requests.gor --output-http="example.com"

3)–output-http
用于实时流量拷贝,将抓取到的请求直接转发给目标地址

./goreplay --input-raw :80 --output-http="example.com"

目标地址可以指定多个的,如:

./goreplay --input-raw :80 --output-http="192.168.0.1:8080"  --output-http="192.168.0.2:8080"

注意,默认情况下将转发一样的流量到这两个地址;可以通过使用–split-output true选项来实现以轮训的方式转发。

此外,goreplay还支持很多其他类型的output,如:tcp,elasticsearch,kafka等等,详细参数的使用可在help说明中查看。

2 限定速率和加速测试

1)限定速率 Rate limiting
分两种方式:
(a) 按比例限速

./goreplay--input-raw :80  --output-http="example.com|5%"

表示只转发不超过5%的请求。

(b) 指定每秒最多转发请求个数

./goreplay--input-raw :80  --output-http="example.com|5"

表示转发的请求数不超过5个。

2)加速测试 Perfrmance testing
目前只支持–input-file参数,且指定指必须为百分比,如:

./goreplay --input-file "requests.gor|200%" --output-http "example.com"

表示将以原来两倍的速率转发请求到目的地址。可以结合–input-file-loop来循环指定文件的流量拷贝。

3 请求过滤

goreplay支持对url,HTTP header,HTTP method的过滤。
1)通过请求url来过滤
(a) --http-allow-url

./goreplay --input-raw :8080 --output-http staging.com --http-allow-url "/api/(v1|v2)"

(b) --http-disallow-url

./goreplay --input-raw :8080 --output-http staging.com --http-disallow-url "/api"

2)通过HTTP header来过滤
(a) --http-allow-header

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

(b) --http-disallow-header

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

3)通过HTTP method来过滤
(a) --http-allow-method

./goreplay --input-raw :80 --output-http "http://staging.server" \
    --http-allow-method GET \
    --http-allow-method OPTIONS

HTTP method的过滤没有类似前两个的那种disallow的选项。

4 请求重写 Request rewriting

goreplay支持对URL,URL参数,以及头信息的重写。
1)重写URL

# 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

通过–http-rewrite-url选项来指定要重写的部分,即用冒号后的部分替换冒号前的部分。

2)设置URL参数

./goreplay --input-raw :8080 --output-http staging.com --http-set-param api_key=1

通过–http-set-param选项来实现,如果次参数以存在,则将用指定的值进行覆盖。

3)设置header头信息

./goreplay --input-raw :80 --output-http "http://staging.server" \
    --http-set-header "User-Agent: Replayed by Gor" \
    --http-set-header "Enable-Feature-X: true"

通过–http-set-header选项来实现,如果头信息已存在,则覆盖。

4)关于Host头信息
Host header的情况跟前面几个不一样,默认的Host是按–output-http选项中指定的来设定的,即使手动通过–http-set-header "Host: newHost.com"指定后,goreplay也不会覆盖的。如果不想goreplay按照–output-http选项来修改Host header,可以带上-http-original-host选项。

附:
github上项目地址:https://github.com/buger/goreplay

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值