From ed9caa3e497f739601a36d3e2e7ac259e2f9dac7 Mon Sep 17 00:00:00 2001 From: charlie <3140647@qq.com> Date: Fri, 7 Jun 2024 16:39:01 +0800 Subject: [PATCH] update --- .dockerignore | 2 ++ .gitignore | 2 ++ Dockerfile | 35 +++++++++++++++++++++++++++++++ go.mod | 2 +- main.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..00718e9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +go.sum +echo \ No newline at end of file diff --git a/.gitignore b/.gitignore index 8e5400b..7f33bc2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ echo.exe +nohup.out +echo diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a6bd9eb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +FROM reg.charlienet.top/go/golang:1.22-alpine as builder + +ARG app_name=echo +RUN echo $app_name + +ENV APPNAME=echo +RUN echo $app_name + +WORKDIR /build + +RUN echo $app_name + +COPY . . + +ENV GOPROXY https://goproxy.cn,direct +RUN go mod tidy && go build -ldflags="-s -w" + +FROM alpine:3.19 + +# 使用阿里云镜像 +# RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +# RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata +ENV TZ Asia/Shanghai + +WORKDIR /app +COPY --from=builder /build/echo echo + +# COPY conf conf + +# ENV GIN_MODE=release +# ENV PATH /app:$PATH + +EXPOSE 8080 + +CMD ["./echo"] \ No newline at end of file diff --git a/go.mod b/go.mod index 426830f..e0a81e2 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module echo -go 1.22.4 +go 1.22 diff --git a/main.go b/main.go index 7e130d4..5161587 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,16 @@ package main import ( "fmt" "log" + "net" "net/http" "os" + "strings" ) -var name string +var ( + name string + RemoteIPHeaders = []string{"X-Forwarded-For", "X-Real-IP"} +) func main() { hostname, err := os.Hostname() @@ -28,9 +33,56 @@ func main() { func echoHandler(w http.ResponseWriter, r *http.Request) { clientIp := getRemoteIP(r) - fmt.Fprintf(w, "hello %s Host %s provides services", clientIp, name) + + log.Printf("client %s connected\n\n", clientIp) + fmt.Fprintf(w, "hello %s, Host %s provides services\n", clientIp, name) } func getRemoteIP(r *http.Request) string { - return r.RemoteAddr + for k, v := range r.Header { + fmt.Printf("KEY=%s VALUE=%v\n", k, v) + } + + for _, headerName := range RemoteIPHeaders { + ip, valid := validateHeader(r.Header.Get(headerName)) + if valid { + return ip + } + } + + return RemoteIP(r) +} + +func validateHeader(header string) (clientIP string, valid bool) { + if header == "" { + return "", false + } + items := strings.Split(header, ",") + for i := len(items) - 1; i >= 0; i-- { + ipStr := strings.TrimSpace(items[i]) + ip := net.ParseIP(ipStr) + if ip == nil { + break + } + + if i == 0 { + return ipStr, true + } + + // // X-Forwarded-For is appended by proxy + // // Check IPs in reverse order and stop when find untrusted proxy + // if (i == 0) || (!engine.isTrustedProxy(ip)) { + // return ipStr, true + // } + } + return "", false +} + +func RemoteIP(r *http.Request) string { + ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)) + if err != nil { + return "" + } + + return ip }