成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一個Go語言實現的流量回放工具

安全 應用安全
goreplay是一個開源網絡監控工具,可以實時記錄TCP/HTTP流量,支持把流量記錄到文件或者elasticSearch?實時分析,也支持流量的放大、縮小,還支持頻率限制;goreplay不是代理,無需任何代碼入侵,只需要在服務相同的機器上運行goreplay?守護程序,其會在后臺偵聽網絡接口上的流量,goreplay的設計遵循 Unix 設計哲學:一切都是由管道組成的,各種輸入將數據復用為輸

前言

哈嘍,大家好,我是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提供的玩法非常豐富,合理的改造可以做成回歸工具幫助我們確保服務的穩定性,別放過這個自我展現的機會~。

責任編輯:武曉燕 來源: Golang夢工廠
相關推薦

2019-05-16 14:15:19

開源技術 工具

2023-06-16 23:57:56

智能運營系統

2022-05-19 14:14:26

go語言限流算法

2023-12-07 12:59:46

C語言循環隊列代碼

2022-11-01 18:29:25

Go語言排序算法

2020-08-12 08:56:30

代碼凱撒密碼函數

2024-08-29 13:23:04

WindowsGo語言

2023-05-08 07:55:05

快速排序Go 語言

2024-04-26 09:38:36

2018-06-22 10:30:56

C語言虛擬機編譯器

2024-06-06 09:47:56

2024-10-15 11:09:43

2021-01-26 10:33:45

前端開發技術

2012-03-13 10:40:58

Google Go

2021-07-26 09:47:38

Go語言C++

2020-09-30 13:29:25

工具代碼開發

2022-10-08 08:15:55

GScriptGo 語言

2022-09-14 08:01:54

語法樹編譯器語法糖

2021-07-12 15:50:55

Go 語言netstat命令

2012-08-06 08:50:05

Go語言
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 伊人网国产 | 国产精品一区二区欧美黑人喷潮水 | 97伦理电影网 | 精品欧美一区二区在线观看视频 | 免费一区二区 | 妞干网福利视频 | 国产一区2区 | 久久久久久久久久久久久久国产 | 久久久噜噜噜久久中文字幕色伊伊 | 国产午夜精品久久 | 亚洲人精品午夜 | 日韩免费视频一区二区 | 亚洲欧美精品在线观看 | 国产成人99| 亚洲免费三区 | 午夜电影在线播放 | 毛片一级电影 | 一本一道久久a久久精品综合蜜臀 | 中文字幕精品一区二区三区精品 | 日本天堂视频在线观看 | 国产剧情一区 | 中文字幕乱码视频32 | 亚洲国产成人精 | 在线伊人网 | 操操日| 亚洲视频观看 | 天堂中文字幕av | 国产精品.xx视频.xxtv | 中文字幕一区二区三区在线视频 | 亚洲精品日韩视频 | 国产日韩欧美 | 日韩中文字幕av | 午夜视频一区二区 | 国产精品99久久久久久久久久久久 | 青青草中文字幕 | 欧美一区二区在线看 | 久久久久成人精品亚洲国产 | 免费在线观看毛片 | 国产精品视频一区二区三区 | 免费精品国产 | 久久69精品久久久久久国产越南 |