diff options
| author | terminaldweller <thabogre@gmail.com> | 2023-01-17 12:05:39 +0000 | 
|---|---|---|
| committer | terminaldweller <thabogre@gmail.com> | 2023-01-17 12:05:39 +0000 | 
| commit | f3c36dad4ec3e17042741d475848bca913e48f3c (patch) | |
| tree | 36fade01013055da16e68af812d1dfcdaec83846 | |
| parent | Initial commit (diff) | |
| download | icanhazallips-f3c36dad4ec3e17042741d475848bca913e48f3c.tar.gz icanhazallips-f3c36dad4ec3e17042741d475848bca913e48f3c.zip | |
first commit
| -rw-r--r-- | Dockerfile | 20 | ||||
| -rw-r--r-- | docker-compose.yaml | 17 | ||||
| -rw-r--r-- | go.mod | 3 | ||||
| -rw-r--r-- | icanhazallips.go | 54 | 
4 files changed, 94 insertions, 0 deletions
| diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fdd725a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM alpine:3.17 as builder +ENV GOPROXY=https://goproxy.io +RUN apk update && apk upgrade +RUN apk add go git +ENV GOPROXY=https://goproxy.io +COPY go.* /icanhazallips/ +RUN cd /icanhazallips && go mod download +COPY *.go /icanhazallips/ +RUN cd /icanhazallips && go build + +FROM alpine:3.17 as certbuilder +RUN apk add openssl +WORKDIR /certs +RUN openssl req -nodes -new -x509 -subj="/C=US/ST=Denial/L=springfield/O=Dis/CN=localhost" -keyout server.key -out server.cert + +FROM gcr.io/distroless/static-debian11 +# FROM alpine:3.17 +COPY --from=certbuilder /certs /certs +COPY --from=builder /icanhazallips/icanhazallips /icanhazallips/icanhazallips +ENTRYPOINT ["/icanhazallips/icanhazallips"] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..769885b --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,17 @@ +version: "3" +services: +  icanhazallips: +    image: icanhazallips +    build: +      context: . +    networks: +      - mainnet +    restart: unless-stopped +    ports: +      - "9380:8080" +    entrypoint: ["/icanhazallips/icanhazallips"] +    cap_drop: +      - ALL +networks: +  mainnet: +    driver: bridge @@ -0,0 +1,3 @@ +module icanhazallips + +go 1.19 diff --git a/icanhazallips.go b/icanhazallips.go new file mode 100644 index 0000000..bcca91e --- /dev/null +++ b/icanhazallips.go @@ -0,0 +1,54 @@ +// https://gist.github.com/miguelmota/7b765edff00dc676215d6174f3f30216 +package main + +import ( +	"errors" +	"log" +	"net" +	"net/http" +	"strings" +) + +func getIP(r *http.Request) (string, error) { +	ips := r.Header.Get("X-Forwarded-For") +	splitIps := strings.Split(ips, ",") + +	if len(splitIps) > 0 { +		netIP := net.ParseIP(splitIps[len(splitIps)-1]) +		if netIP != nil { +			return netIP.String(), nil +		} +	} + +	ip, _, err := net.SplitHostPort(r.RemoteAddr) +	if err != nil { +		return "", err +	} + +	netIP := net.ParseIP(ip) +	if netIP != nil { +		ip := netIP.String() +		if ip == "::1" { +			return "127.0.0.1", nil +		} +		return ip, nil +	} + +	return "", errors.New("IP not found") +} + +func handler(w http.ResponseWriter, r *http.Request) { +	ip, err := getIP(r) +	if err != nil { +		w.WriteHeader(http.StatusInternalServerError) +		return +	} + +	w.WriteHeader(http.StatusOK) +	w.Write([]byte(ip)) +} + +func main() { +	http.HandleFunc("/", handler) +	log.Fatal(http.ListenAndServe(":8080", nil)) +} | 
