Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Add comments to the golang code of gor drainage test middleware

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Package main

Import (

"bufio"

"bytes"

"encoding/hex"

"fmt"

"github.com/buger/goreplay/proto"

"os"

)

/ / requestID-> originalToken

Var originalTokens map [string] [] byte

/ / originalToken-> replayedToken

Var tokenAliases map [string] [] byte

Func main () {

OriginalTokens = make (map [string] [] byte)

TokenAliases = make (map [string] [] byte)

Scanner: = bufio.NewScanner (os.Stdin) for scanner.Scan () {encoded: = scanner.Bytes () buf: = make ([] byte, len (encoded) / 2) hex.Decode (buf, encoded) process (buf)}

}

Func process (buf [] byte) {

/ / First byte indicate payload type, possible values:

/ / 1-Request

/ / 2-Response

/ / 3-ReplayedResponse

PayloadType: = buf [0]

HeaderSize: = bytes.IndexByte (buf,'\ n') + 1

Header: = buf [: headerSize-1]

/ / Header contains space separated values of: request type, request id, and request start time (or round-trip time for responses) meta: = bytes.Split (header, [] byte ("")) / / For each request you should receive 3 payloads (request, response, replayed response) with same request idreqID: = string (meta [1]) payload: = buf [headerSize:] Debug ("Received payload:", string (buf)) switch payloadType {case '1customers: / / Request if bytes.Equal (payload) [] byte ("/ token") {originalTokens [reqID] = [] byte {} Debug ("Found token request:", reqID)} else {token, vs, _: = proto.PathParam (payload, [] byte ("token")) / / get the token value of the production server if vs! =-1 {/ / If there is GET token param if alias, ok: = tokenAlias [string (token)] Ok {/ / check whether the token value to be replaced exists / / Rewrite original token to alias payload = proto.SetPathParam (payload, [] byte ("token"), alias) / / replace the produced token with the token / / Copy modified payload to our buffer buf = append (buf [: headerSize] of the test suit) Payload...)}} / / Emitting data back os.Stdout.Write (encode (buf)) / / the rewrite request is ready to be sent to the test server case '2requests: / / Original response if _, ok: = originalTokens [reqID] Ok {/ / Token is inside response body secureToken: = proto.Body (payload) / / fetch the token value returned from the production server originalTokens [reqID] = secureToken Debug ("Remember origial token:", string (secureToken))} case '3values: / / Replayed response if originalToken, ok: = originalTokens [reqID] Ok {delete (originalTokens, reqID) secureToken: = proto.Body (payload) tokenAliases [string (originalToken)] = secureToken// gets the tokenvalue of the test server to replace the tokenvalue of the formal server Debug ("Create alias for new token token, was:", string (originalToken), "now:", string (secureToken))}}

}

Func encode (buf [] byte) [] byte {

Dst: = make ([] byte, len (buf) * 2x1)

Hex.Encode (dst, buf)

Dst [len (dst)-1] ='\ n'

Return dst

}

Func Debug (args... interface {}) {

Fmt.Fprint (os.Stderr, "[DEBUG] [TOKEN-MOD]")

Fmt.Fprintln (os.Stderr, args...)

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 224

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report