commit 1fe4f3b573215140feb073b06b90bd8956cc07a8 Author: dustoair <107600816+dustoair@users.noreply.github.com> Date: Sun Aug 21 12:12:01 2022 +0800 privoxy diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..3aeef70 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,51 @@ +kind: pipeline +type: ssh # 使用SSH +name: go-privoxy + +server: + host: + from_secret: ssh_host + user: + from_secret: ssh_user + password: + from_secret: ssh_password + +clone: + disable: true + +steps: + - name: build + commands: + - echo build start + - date + - export PATH=$PATH:/usr/local/go/bin + - cd /root + - rm -rf goPrivoxy + - git clone https://git.sre.ink/go/goPrivoxy.git + - cd goPrivoxy + - make build-linux-arm64 + - date + - echo build end + - name: package + commands: + - echo package start + - date + - cd /root/goPrivoxy + - docker build -t sre/goprivoxy:arm64 . + - date + - echo package end + - name: deploy + commands: + - echo deploy start + - date + - export KUBECONFIG=/etc/kubernetes/admin.conf + - kubectl -n sre rollout restart deployment go-privoxy + - date + - echo deploy end + - name: alert + commands: + - echo alert start + - date + - echo deploy ok + - date + - echo alert end \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a8438f --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +.idea +go.sum +main.exe +eth.exe +monitor.exe +log.txt +logs +*.log +uploads +gin.log diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b63c862 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +all: build-linux-arm64 build-linux-amd64 + +build-linux-arm64: + CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go mod tidy + CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go build -v -a -o privoxy.bin main.go + +build-linux-amd64: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go mod tidy + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -v -a -o privoxy.bin main.go + docker build -t sre/goprivoxy:arm64 . + docker rm -f goprivoxy + docker run --name goprivoxy --restart always --net=host -d sre/goprivoxy:arm64 + docker logs -f goprivoxy \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cfdad9d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module goPrivoxy + +go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 0000000..d211e1e --- /dev/null +++ b/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "io" + "log" + "net" + "net/http" + "strings" +) + +/* +* +@author: sre +@date: 2022/8/21 0021 +@desc: todo +* +*/ + +type goPrivoxy struct { +} + +func (p *goPrivoxy) ServeHTTP(w http.ResponseWriter, req *http.Request) { + log.Printf("接受请求 %s %s %s\n", req.Method, req.Host, req.RemoteAddr) + + transport := http.DefaultTransport + + // 第一步: 代理接受到客户端的请求,复制原来的请求对象,并根据数据配置新请求的各种参数(添加上X-Forward-For头部等) + outReq := new(http.Request) + *outReq = *req // 这只是一个浅层拷贝 + + clientIP, _, err := net.SplitHostPort(req.RemoteAddr) + if err == nil { + prior, ok := outReq.Header["X-Forwarded-For"] + if ok { + clientIP = strings.Join(prior, ", ") + ", " + clientIP + } + outReq.Header.Set("X-Forwarded-For", clientIP) + } + + // 第二步: 把新请求复制到服务器端,并接收到服务器端返回的响应 + res, err := transport.RoundTrip(outReq) + if err != nil { + w.WriteHeader(http.StatusBadGateway) // 502 + return + } + + // 第三步:代理服务器对响应做一些处理,然后返回给客户端 + for key, value := range res.Header { + for _, v := range value { + w.Header().Add(key, v) + } + } + + w.WriteHeader(res.StatusCode) + io.Copy(w, res.Body) + res.Body.Close() +} + +func main() { + log.Println("goPrivoxy start at 0.0.0.0:8118") + http.Handle("/", &goPrivoxy{}) + http.ListenAndServe("0.0.0.0:8118", nil) +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..864a2ea --- /dev/null +++ b/readme.md @@ -0,0 +1,21 @@ +https://github.com/fenggolang/go-proxy + +## build arm64 on oracle k8s +```bash +cd /root +rm -rf goPrivoxy +git clone https://git.sre.ink/go/goPrivoxy.git +cd goPrivoxy +make build-linux-arm64 +docker build -t sre/goprivoxy:arm64 . +kubectl -n sre rollout restart deployment go-privoxy +``` + +## build amd64 on vm +```bash +cd /root +rm -rf goPrivoxy +git clone https://git.sre.ink/go/goPrivoxy.git +cd goPrivoxy +make build-linux-amd64 +```