init
This commit is contained in:
2
.dockerignore
Normal file
2
.dockerignore
Normal file
@@ -0,0 +1,2 @@
|
||||
/.git
|
||||
/ip
|
||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/.git
|
||||
/ip
|
||||
17
Dockerfile
Normal file
17
Dockerfile
Normal 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
3
Makefile
Normal file
@@ -0,0 +1,3 @@
|
||||
.PHONY: linux
|
||||
linux:
|
||||
CGO_ENABLED=0 go build -v -ldflags '-extldflags=-static'
|
||||
9
README.md
Normal file
9
README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# ip tool
|
||||
|
||||
the simplest ip tool
|
||||
|
||||
usage:
|
||||
|
||||
```bash
|
||||
curl http://yourservice:8080/
|
||||
```
|
||||
38
main.go
Normal file
38
main.go
Normal 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"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user