commit ee34224cb72312f022724423814ad3c1d04fc734 Author: heimoshuiyu Date: Tue Sep 3 16:52:58 2024 +0800 init diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..7779fd3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +/.git +/ip \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7779fd3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.git +/ip \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..33e9585 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM docker.io/golang:1.23.0 AS builder + +WORKDIR /app + +COPY . . + +RUN make + +FROM alpine:latest + +WORKDIR /app + +COPY --from=builder /app/ip . + +EXPOSE 8080 + +CMD ["/app/ip"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..79a4c37 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +.PHONY: linux +linux: + CGO_ENABLED=0 go build -v -ldflags '-extldflags=-static' diff --git a/README.md b/README.md new file mode 100644 index 0000000..efafe10 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# ip tool + +the simplest ip tool + +usage: + +```bash +curl http://yourservice:8080/ +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..857e8ac --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module ip + +go 1.23.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..9b36940 --- /dev/null +++ b/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "flag" + "log" + "net/http" + "strconv" +) + +func main() { + port := flag.Int("port", 8080, "port to listen on") + flag.Parse() + + mux := http.NewServeMux() + mux.HandleFunc("/", IPHandler) + + log.Println("Listening on port", *port) + http.ListenAndServe(":"+strconv.Itoa(*port), mux) +} + +func IPHandler(w http.ResponseWriter, r *http.Request) { + addr := r.RemoteAddr + + w.Write([]byte(addr)) + w.Write([]byte("\n")) + + if r.Header.Get("X-Real-IP") != "" { + w.Write([]byte("X-Real-IP: ")) + w.Write([]byte(r.Header.Get("X-Real-IP"))) + w.Write([]byte("\n")) + } + + if r.Header.Get("X-Forwarded-For") != "" { + w.Write([]byte("X-Forwarded-For: ")) + w.Write([]byte(r.Header.Get("X-Forwarded-For"))) + w.Write([]byte("\n")) + } +}