1.安装golang环境, 以1.16.3为例
# 前期准备
sudo apt-get install flex bison -y
# Download latest stable release, compile and install it
wget http://www.tcpdump.org/release/libpcap-1.7.4.tar.gz && tar xzf libpcap-1.7.4.tar.gz
cd libpcap-1.7.4
./configure && make install
# 下载golang安装包
wget https://golang.org/dl/go1.16.3.linux-amd64.tar.gz
# 解压
tar -C /usr/local -zxvf go1.16.3.linux-amd64.tar.gz
# 添加环境变更
vim /etc/profile
# 最后一行添加
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
# 保存退出后source
source /etc/profile
# 输入go version验证是否安装成功
root@duyan:/# go version
go version go1.16.3 linux/amd64
# go env 查看go环境变量, 只关注以下3个
GO111MODULE=""
GOPATH="/root/go"
GOPROXY="https://proxy.golang.org,direct"
# 若 GOPATH 文件夹不存在,新建一个
mkdir /root/go
# 关闭 go mod
go env -w GO111MODULE=off
# 设置国内代理
go env -w GOPROXY="https://goproxy.io,direct"
# go env 查看结果如下,只关注以下3个
GO111MODULE="off"
GOPATH="/root/go"
GOPROXY="https://goproxy.io,direct"
2.手动下载gorepaly 0.16.1 源码# cd到GOPATH目录
cd /root/go/
# 手动创建gorepaly包目录, 并cd到buger目录
mkdir src && cd src && mkdir github.com && cd github.com && mkdir buger && cd buger
# git 手动下载 指定版本: v0.16.1
git clone https://github.com/buger/goreplay.git -b v0.16.1
3.下载goreplay 依赖# 添加 -v 可以打印详细信息, 中途下载失败可以重复执行这个命令多试几次
go get -v github.com/buger/goreplay
4.编译
cd goreplay
go build -ldflags '-extldflags "-static" -s -w -X main.VERSION=0.16.1'
# 编译结果如下,有warning,不影响
/tmp/go-link-853597898/000004.o: In function `_cgo_26061493d47f_C2func_getaddrinfo':
/tmp/go-build/cgo-gcc-prolog:58: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
//usr/local/lib/libpcap.a(nametoaddr.o): In function `pcap_nametoaddr':
/libpcap-1.7.4/./nametoaddr.c:102: warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
//usr/local/lib/libpcap.a(nametoaddr.o): In function `pcap_nametonetaddr':
/libpcap-1.7.4/./nametoaddr.c:146: warning: Using 'getnetbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
//usr/local/lib/libpcap.a(nametoaddr.o): In function `pcap_nametoproto':
/libpcap-1.7.4/./nametoaddr.c:263: warning: Using 'getprotobyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
//usr/local/lib/libpcap.a(nametoaddr.o): In function `pcap_nametoport':
/libpcap-1.7.4/./nametoaddr.c:176: warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
# goreplay 为输出的可执行文件
# 指定可执行文件名命令: go build -ldflags '-extldflags "-static" -s -w -X main.VERSION=0.16.1' -o gor
root@duyan:~/go/src/github.com/buger/goreplay# ls
...
goreplay input_dummy.go LICENSE.txt output_http.go raw_socket_listener
参考
公众号: 杜焰
https://github.com/buger/goreplay/wiki/Compilation