您现在的位置是:物联网 >>正文
一个Go语言实现的流量回放工具
物联网8人已围观
简介前言哈喽,大家好,我是asong。今天给大家推荐一款使用Go语言编写的流量回放工具 --goreplay;工作中你一定遇到过需要在服务器上抓包的场景,有了这个工具就可以助你一臂之力,goreplay的 ...
哈喽,个G工具大家好,语言实我是流量asong。
今天给大家推荐一款使用Go语言编写的回放流量回放工具 -- goreplay;工作中你一定遇到过需要在服务器上抓包的场景 ,有了这个工具就可以助你一臂之力,个G工具goreplay的语言实功能十分强大 ,支持流量的流量放大 、缩小,回放并且集成了ElasticSearch ,个G工具将流量存入ES进行实时分析;
废话不多,语言实我们接下来来看一看这个工具;
goreplay介绍与安装项目地址 :https://github.com/buger/goreplay
goreplay是流量一个开源网络监控工具 ,云计算可以实时记录TCP/HTTP流量 ,回放支持把流量记录到文件或者elasticSearch实时分析,个G工具也支持流量的语言实放大、缩小,流量还支持频率限制;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)}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.因为资源有限,这里我用一台电脑起两个进程来模拟流量转发 ,分别启动两个web服务分别监控端口号8081 、8082:
复制$ go run . --port="8081"$ go run . --port="8082"1.2.
服务弄好了,现在我们来开启gor守护进程进行流量监听与转发,将8081端口的流量转发到8082端口上 :
复制$ sudo gor --input-raw :8081 --output-http="http://127.0.0.1:8082"1.
现在我们请求8081端口 :
复制$ curl --location --request GET http://127.0.0.1:8081/api/ping1.可以看到8082端口同样被请求了:

goreplay支持将捕获的流量存储到文件中 ,实际工作中我们可以使用捕获的流量做压力测试,首先我们需要将捕获的流量保存到本地文件,然后利用该文件进行流量回放;
还是上面的建站模板Web程序,我们将端口8081的流量保存到本地文件:
复制$ sudo gor --input-raw :8081 --output-file ./requests.gor1.我们对8081端口执行了5次请求:

然后我们对8082端口进行流量缩小测试,缩小一倍:
复制gor --input-file "requests_0.gor" --output-http="http://127.0.0.1:8082|50%"1.调整百分比就是进行流量放大 、缩小,这里我们缩小了一倍,可以看到只有2次请求到了8082端口;我们可以调整流量回放的速度 ,比如我们调整流量以10倍速度进行重播 :
复制$ gor --input-file "requests_0.gor|1000%" --output-http="http://127.0.0.1:8082|50%" # 1000%就是放大10倍1. 流量写入到ElastichSearchgoreplay可以将捕获的流量导出到Es中,只需要执行如下命令:
复制$ gor --input-raw :8000 --output-http http://staging.cm --output-http-elasticsearch localhost:9200/gor1.我们不需要提前创建索引结构 ,他将自动创建,具体结构如下:
复制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}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.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 anddev environments.
Project page: https://github.com/buger/gor
Author: <Leonid Bugaev> leonsbox@gmail.comCurrent 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" ..........省略1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28. 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提供的玩法非常丰富,合理的改造可以做成回归工具帮助我们确保服务的稳定性 ,别放过这个自我展现的机会~ 。
Tags:
转载:欢迎各位朋友分享到网络,但转载请说明文章出处“商站动力”。http://www.noorid.com/news/889b999101.html
相关文章
2024年值得关注的三大安全趋势
物联网到2024年,随着越来越多的组织实施云优先方法,全球云支出预计将增长20%以上。随着资产越来越多地转移到混合和多云环境,安全策略将成为首要考虑因素,而威胁参与者则寻找机会利用互连云部署中的漏洞。为了加 ...
【物联网】
阅读更多22.2Tbps DDoS 攻击再次刷新世界纪录:史上最强网络攻击
物联网Cloudflare近日宣布成功自动拦截了有史以来最大规模的分布式拒绝服务DDoS)攻击。这次超大规模攻击峰值达到创纪录的22.2Tbps带宽和每秒106亿个数据包,为网络威胁规模树立了新的危险标杆, ...
【物联网】
阅读更多戴尔PowerStore 性能提升 降低了IT预算
物联网Womens Day3月8日一个伟大的日子无论你从事什么行业无论你身处何方身为女性的你都应该感到骄傲和自豪这一天属于你们女神节快乐!生命与母爱,是人生中最宝贵的财富。从生理上来说,不管是肌肉量、身高还 ...
【物联网】
阅读更多