一個Go語言實現的流量回放工具
前言
哈嘍,大家好,我是asong。
今天給大家推薦一款使用Go語言編寫的流量回放工具 -- goreplay;工作中你一定遇到過需要在服務器上抓包的場景,有了這個工具就可以助你一臂之力,goreplay的功能十分強大,支持流量的放大、縮小,并且集成了ElasticSearch,將流量存入ES進行實時分析;
廢話不多,我們接下來來看一看這個工具;
goreplay介紹與安裝
項目地址:https://github.com/buger/goreplay
goreplay是一個開源網絡監控工具,可以實時記錄TCP/HTTP流量,支持把流量記錄到文件或者elasticSearch實時分析,也支持流量的放大、縮小,還支持頻率限制;goreplay不是代理,無需任何代碼入侵,只需要在服務相同的機器上運行goreplay守護程序,其會在后臺偵聽網絡接口上的流量,goreplay的設計遵循 Unix 設計哲學:一切都是由管道組成的,各種輸入將數據復用為輸出;可以看一下官網畫的架構圖:
goreplay的安裝也比較簡單,只需要在https://github.com/buger/goreplay/releases 下載對應操作系統的二進制文件即可,我的電腦是mac的:
解壓縮后就是一個二進制文件gor,將其添加到您的環境變量中,方便我們后續的操作;
使用示例
實時流量轉發
首先我們要準備一個Web服務,最簡單的就是用Gin 快速實現一個helloworld,替大家實現好了:https://github.com/asong2020/Golang_Dream/tree/master/code_demo/gin_demo;
import (
"flag"
"github.com/gin-gonic/gin"
)
var Port string
func init() {
flag.StringVar(&Port, "port", "8081", "Input Your Port")
}
func main() {
flag.Parse()
r := gin.Default()
r.Use()
r1 := r.Group("/api")
{
r1.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
}
r.Run("localhost:" + Port)
}
因為資源有限,這里我用一臺電腦起兩個進程來模擬流量轉發,分別啟動兩個web服務分別監控端口號8081、8082:
$ go run . --port="8081"
$ go run . --port="8082"
服務弄好了,現在我們來開啟gor守護進程進行流量監聽與轉發,將8081端口的流量轉發到8082端口上:
$ sudo gor --input-raw :8081 --output-http="http://127.0.0.1:8082"
現在我們請求8081端口:
$ curl --location --request GET 'http://127.0.0.1:8081/api/ping'
可以看到8082端口同樣被請求了:
流量放大、縮小
goreplay支持將捕獲的流量存儲到文件中,實際工作中我們可以使用捕獲的流量做壓力測試,首先我們需要將捕獲的流量保存到本地文件,然后利用該文件進行流量回放;
還是上面的Web程序,我們將端口8081的流量保存到本地文件:
$ sudo gor --input-raw :8081 --output-file ./requests.gor
我們對8081端口執行了5次請求:
然后我們對8082端口進行流量縮小測試,縮小一倍:
gor --input-file "requests_0.gor" --output-http="http://127.0.0.1:8082|50%"
調整百分比就是進行流量放大、縮小,這里我們縮小了一倍,可以看到只有2次請求到了8082端口;我們可以調整流量回放的速度,比如我們調整流量以10倍速度進行重播:
$ gor --input-file "requests_0.gor|1000%" --output-http="http://127.0.0.1:8082|50%" # 1000%就是放大10倍
流量寫入到ElastichSearch
goreplay可以將捕獲的流量導出到Es中,只需要執行如下命令:
$ gor --input-raw :8000 --output-http http://staging.cm --output-http-elasticsearch localhost:9200/gor
我們不需要提前創建索引結構,他將自動創建,具體結構如下:
type ESRequestResponse struct {
ReqURL string `json:"Req_URL"`
ReqMethod string `json:"Req_Method"`
ReqUserAgent string `json:"Req_User-Agent"`
ReqAcceptLanguage string `json:"Req_Accept-Language,omitempty"`
ReqAccept string `json:"Req_Accept,omitempty"`
ReqAcceptEncoding string `json:"Req_Accept-Encoding,omitempty"`
ReqIfModifiedSince string `json:"Req_If-Modified-Since,omitempty"`
ReqConnection string `json:"Req_Connection,omitempty"`
ReqCookies string `json:"Req_Cookies,omitempty"`
RespStatus string `json:"Resp_Status"`
RespStatusCode string `json:"Resp_Status-Code"`
RespProto string `json:"Resp_Proto,omitempty"`
RespContentLength string `json:"Resp_Content-Length,omitempty"`
RespContentType string `json:"Resp_Content-Type,omitempty"`
RespTransferEncoding string `json:"Resp_Transfer-Encoding,omitempty"`
RespContentEncoding string `json:"Resp_Content-Encoding,omitempty"`
RespExpires string `json:"Resp_Expires,omitempty"`
RespCacheControl string `json:"Resp_Cache-Control,omitempty"`
RespVary string `json:"Resp_Vary,omitempty"`
RespSetCookie string `json:"Resp_Set-Cookie,omitempty"`
Rtt int64 `json:"RTT"`
Timestamp time.Time
}
goreplay提供了太多的功能,就不一一介紹了,可以通過執行help命令查看其他高級用法,每個命令都提供了例子,入手很快;
$ gor -h
Gor is a simple http traffic replication tool written in Go. Its main goal is to replay traffic from production servers to staging and dev environments.
Project page: https://github.com/buger/gor
Author: <Leonid Bugaev> leonsbox@gmail.com
Current Version: v1.3.0
-copy-buffer-size value
Set the buffer size for an individual request (default 5MB)
-cpuprofile string
write cpu profile to file
-exit-after duration
exit after specified duration
-http-allow-header value
A regexp to match a specific header against. Requests with non-matching headers will be dropped:
gor --input-raw :8080 --output-http staging.com --http-allow-header api-version:^v1
-http-allow-method value
Whitelist of HTTP methods to replay. Anything else will be dropped:
gor --input-raw :8080 --output-http staging.com --http-allow-method GET --http-allow-method OPTIONS
-http-allow-url value
A regexp to match requests against. Filter get matched against full url with domain. Anything else will be dropped:
gor --input-raw :8080 --output-http staging.com --http-allow-url ^www.
-http-basic-auth-filter value
A regexp to match the decoded basic auth string against. Requests with non-matching headers will be dropped:
gor --input-raw :8080 --output-http staging.com --http-basic-auth-filter "^customer[0-9].*"
-http-disallow-header value
A regexp to match a specific header against. Requests with matching headers will be dropped:
gor --input-raw :8080 --output-http staging.com --http-disallow-header "User-Agent: Replayed by Gor"
..........省略
goreplay基本實現原理
goreplay底層也是調用Libpcap,Libpcap即數據包捕獲函數庫,tcpdump也是基于這個庫實現的,Libpcap是C語言寫的,Go語言不能直接調用C語言,需要使用CGo,所以goreplay可以直接使用谷歌的包github.com/google/gopacket,提供了更方便的操作接口,基于goreplay封裝了input、output,在啟動的時候通過命令行參數解析指定的input、output,input讀取數據寫入到output中,默認是一個input復制多份,寫多個output,多個input之前是并行的,但是單個intput到多個output是串行的,所以input-file會有性能瓶頸,壓測的時候需要開多個進程同時跑來達到壓測需求;
goreplay的源碼有點多,就不在這里分析了,大家感興趣哪一部分可以從gor.go的main函數入手,看自己感興趣的部分就可以了;
總結
goreplay提供的玩法非常豐富,合理的改造可以做成回歸工具幫助我們確保服務的穩定性,別放過這個自我展現的機會~。