This commit is contained in:
2024-09-03 16:52:58 +08:00
commit ee34224cb7
7 changed files with 74 additions and 0 deletions

2
.dockerignore Normal file
View File

@@ -0,0 +1,2 @@
/.git
/ip

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/.git
/ip

17
Dockerfile Normal file
View File

@@ -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"]

3
Makefile Normal file
View File

@@ -0,0 +1,3 @@
.PHONY: linux
linux:
CGO_ENABLED=0 go build -v -ldflags '-extldflags=-static'

9
README.md Normal file
View File

@@ -0,0 +1,9 @@
# ip tool
the simplest ip tool
usage:
```bash
curl http://yourservice:8080/
```

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module ip
go 1.23.0

38
main.go Normal file
View File

@@ -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"))
}
}