Command line parameters for GOREPLAY source code parsing

Environment and Compilation
GOREPLAY is written in GO language. After downloading the source code, install the GO operating environment, and after installing PCAP and other linux applications, you need to download the GO package that GOREPLAY depends on and put it in the specified directory. GO is usually obtained through GIT Dependent packages, but because the machine cannot be connected to the Internet on the intranet, I download the dependencies locally, upload them to the GOPATH of GO, put them in the path, and finally go build can compile a runnable binary executable. executable file.

Command Line Arguments
GOREPLAY supports a wide variety of command line arguments. The specific supported command line parameters can be viewed by goreplay --help. We mainly use GOREPLAY to forward HTTP. The usual command is:
goreplay --input-raw :8080 --output-tcp 10.60.20.8:8080


It means to monitor the port 8080 of the machine and forward the data packets on port 8080 to 10.60.20.8:8080.

How does GOREPLAY handle these command line arguments? First of all, starting from the entry of the program, the default entry of the GO language application is in the main function. i.e. in the gor.go file. But in addition to the main entry function in Package main, go also retains another entry, that is, the init function in any package. Such as the init function in setting.go, this function will be executed even before main when the program starts. The functions of go and the initialization of variables can be read in depth. There are relatively deep ways to expand them (the real reason is that I am not very familiar with them now ^_^).

go has a processing module for command line parameters, called flag, and the process of using it can be roughly understood through a sample program:
package main

import (
    "flag"
    "fmt"
)

func main() {
    var src string
    flag.StringVar(&src, "src", "", "source file")
    var level * int
    level = flag.Int("level", 0, "debug level")
    var memo string
    flag.StringVar(&memo, "memo", "", "the memory")
    flag.Parse()
    flag.Usage()
    fmt.Printf("src=%s, level=%d, memo=%s\n", src, *level, memo)
}


Execute args.exe -src="123" -level=4 -memo="567", the output is as follows:
Usage of args.exe:
  -level int
        debug level
  -memo string
        the memory
  -src string
        source file
src=123, level=4, memo=567

The general process is to bind the key on the command line first, and then take out the real value to the specified variable when parsed.
Continue to add the goreplay source code. After executing the init function in setting.go, the command line key is bound to the change. After flag.parse() in the main function, it is parsed into the setting variable, and then InitPlugins is completed. Initial loading of the parameterized module according to the command line. The current command line is:
goreplay --input-raw :8080 --output-tcp 10.60.20.8:8080

That is, input_raw.go and output_tcp.go will be initialized and loaded, and the initialization process is also quite a lot of go's APIs, some of which I don't know very well, such as some reflections, etc., need to be further in-depth, but the command line parameters of goreplay are very important to the program. The process and meaning of running affect the general are as mentioned above. Later, when we want to transform goreplay, we know how to add command line parameters and add corresponding code modules.

The next article will really go into how to parse the TCP and HTTP protocols, and complete the identification of a message before forwarding, so stay tuned.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326365359&siteId=291194637