利用Go語言實(shí)現(xiàn)流量回放工具的示例代碼_第1頁
利用Go語言實(shí)現(xiàn)流量回放工具的示例代碼_第2頁
利用Go語言實(shí)現(xiàn)流量回放工具的示例代碼_第3頁
利用Go語言實(shí)現(xiàn)流量回放工具的示例代碼_第4頁
利用Go語言實(shí)現(xiàn)流量回放工具的示例代碼_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第利用Go語言實(shí)現(xiàn)流量回放工具的示例代碼目錄前言goreplay介紹與安裝使用示例流量放大、縮小流量寫入到ElastichSearchgoreplay基本實(shí)現(xiàn)原理總結(jié)

前言

哈嘍,大家好,我是asong。

今天給大家推薦一款使用Go語言編寫的流量回放工具--goreplay;工作中你一定遇到過需要在服務(wù)器上抓包的場景,有了這個工具就可以助你一臂之力,goreplay的功能十分強(qiáng)大,支持流量的放大、縮小,并且集成了ElasticSearch,將流量存入ES進(jìn)行實(shí)時分析;

廢話不多,我們接下來來看一看這個工具;

goreplay介紹與安裝

項(xiàng)目地址:/buger/goreplay

goreplay是一個開源網(wǎng)絡(luò)監(jiān)控工具,可以實(shí)時記錄TCP/HTTP流量,支持把流量記錄到文件或者elasticSearch實(shí)時分析,也支持流量的放大、縮小,還支持頻率限制;goreplay不是代理,無需任何代碼入侵,只需要在服務(wù)相同的機(jī)器上運(yùn)行g(shù)oreplay守護(hù)程序,其會在后臺偵聽網(wǎng)絡(luò)接口上的流量,goreplay的設(shè)計(jì)遵循Unix設(shè)計(jì)哲學(xué):一切都是由管道組成的,各種輸入將數(shù)據(jù)復(fù)用為輸出;可以看一下官網(wǎng)畫的架構(gòu)圖:

goreplay的安裝也比較簡單,只需要在/buger/goreplay/releases下載對應(yīng)操作系統(tǒng)的二進(jìn)制文件即可,我的電腦是mac的:

解壓縮后就是一個二進(jìn)制文件gor,將其添加到您的環(huán)境變量中,方便我們后續(xù)的操作;

使用示例

實(shí)時流量轉(zhuǎn)發(fā)

首先我們要準(zhǔn)備一個Web服務(wù),最簡單的就是用Gin快速實(shí)現(xiàn)一個helloworld,替大家實(shí)現(xiàn)好了:/asong2025/Golang_Dream/tree/master/code_demo/gin_demo;

import

(

"flag"

"/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)

因?yàn)橘Y源有限,這里我用一臺電腦起兩個進(jìn)程來模擬流量轉(zhuǎn)發(fā),分別啟動兩個web服務(wù)分別監(jiān)控端口號8081、8082:

$

go

run

.

--port="8081"

$

go

run

.

--port="8082"

服務(wù)弄好了,現(xiàn)在我們來開啟gor守護(hù)進(jìn)程進(jìn)行流量監(jiān)聽與轉(zhuǎn)發(fā),將8081端口的流量轉(zhuǎn)發(fā)到8082端口上:

$

sudo

gor

--input-raw

:8081

--output-http=":8082"

現(xiàn)在我們請求8081端口:

$

curl

--location

--request

GET

':8081/api/ping'

可以看到8082端口同樣被請求了:

流量放大、縮小

goreplay支持將捕獲的流量存儲到文件中,實(shí)際工作中我們可以使用捕獲的流量做壓力測試,首先我們需要將捕獲的流量保存到本地文件,然后利用該文件進(jìn)行流量回放;

還是上面的Web程序,我們將端口8081的流量保存到本地文件:

$

sudo

gor

--input-raw

:8081

--output-file

./requests.gor

我們對8081端口執(zhí)行了5次請求:

然后我們對8082端口進(jìn)行流量縮小測試,縮小一倍:

gor

--input-file

"requests_0.gor"

--output-http=":8082|50%"

調(diào)整百分比就是進(jìn)行流量放大、縮小,這里我們縮小了一倍,可以看到只有2次請求到了8082端口;我們可以調(diào)整流量回放的速度,比如我們調(diào)整流量以10倍速度進(jìn)行重播:

$

gor

--input-file

"requests_0.gor|1000%"

--output-http=":8082|50%"

#

1000%就是放大10倍

流量寫入到ElastichSearch

goreplay可以將捕獲的流量導(dǎo)出到Es中,只需要執(zhí)行如下命令:

$

gor

--input-raw

:8000

--output-http

http://staging.cm

--output-http-elasticsearch

localhost:9200/gor

我們不需要提前創(chuàng)建索引結(jié)構(gòu),他將自動創(chuàng)建,具體結(jié)構(gòu)如下:

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提供了太多的功能,就不一一介紹了,可以通過執(zhí)行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:

/buger/gor

Author:

Leonid

Bugaev

leonsbox@

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

--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

--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

--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

--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

--http-disallow-header

"User-Agent:

Replayed

by

Gor"

省略

goreplay基本實(shí)現(xiàn)原理

goreplay底層也是調(diào)用Libpcap,Libpcap即數(shù)據(jù)包捕獲函數(shù)庫,tcpdump也是基于這個庫實(shí)現(xiàn)的,Libpcap是C語言寫的,Go語言不能直接調(diào)用C語言,需要使用CGo,所以goreplay可以直接使用谷歌的包/google/gopacket,提供了更方便的操作接口,基于goreplay封裝了input、output,在啟動的時候通過命令行參數(shù)解析指定的input、output,input讀取數(shù)據(jù)寫入到output中,默認(rèn)是一個input復(fù)制多份,寫多個output,多個

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論