流量拷貝工具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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章