From b29d13503039c4e1eb251ebd0d0f899093836162 Mon Sep 17 00:00:00 2001 From: Simon Rieger Date: Tue, 30 Jan 2024 17:24:02 +0100 Subject: [PATCH] first commit --- README.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 41 ++++++++++++++++++++++++++++++ go/Dockerfile | 30 ++++++++++++++++++++++ go/Dockerfile.old | 22 ++++++++++++++++ go/go.mod | 3 +++ go/main.go | 61 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 220 insertions(+) create mode 100644 README.md create mode 100755 docker-compose.yml create mode 100755 go/Dockerfile create mode 100755 go/Dockerfile.old create mode 100644 go/go.mod create mode 100644 go/main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..d035134 --- /dev/null +++ b/README.md @@ -0,0 +1,63 @@ +```markdown +# IP Resolver + +Das ist eine einfache Go-Anwendung, die die IPv4- und IPv6-Adressen des Clients über eine HTTP-Schnittstelle zurückgibt. + +## Installation und Verwendung mit Docker Compose + +1. Stelle sicher, dass Docker und Docker Compose auf deinem System installiert sind. + +2. Klone dieses Repository: + + ```bash + git clone https://github.com/yourusername/ip-resolver.git + ``` + +3. Navigiere in das Projektverzeichnis: + + ```bash + cd ip-resolver + ``` + +4. Passe bei Bedarf die Nginx-Konfiguration in der `nginx/nginx.conf`-Datei an. + +5. Starte die Anwendung mit Docker Compose: + + ```bash + docker-compose up -d + ``` + +6. Die Anwendung ist jetzt unter [http://localhost:8080/getip](http://localhost:8080/getip) erreichbar. + +7. Um die Anwendung zu stoppen, führe aus: + + ```bash + docker-compose down + ``` + +8. Curl Script zum extrahieren der Werte über die Bash + + ```bash +#!/bin/bash + +# HTTP-Anfrage an die Go-Anwendung senden und Antwort in Variable speichern +response=$(curl -s http://localhost:8080/getip) + +# IPv4-Adresse aus JSON extrahieren +ipv4_address=$(echo "$response" | jq -r .ipv4_address) + +# IPv6-Adresse aus JSON extrahieren +ipv6_address=$(echo "$response" | jq -r .ipv6_address) + +# Ausgabe der Adressen +echo "IPv4-Adresse: $ipv4_address" +echo "IPv6-Adresse: $ipv6_address" + ``` + +## Anpassungen + +- Du kannst die Nginx-Konfiguration in der `nginx/nginx.conf`-Datei anpassen, um den Reverse Proxy an deine Anforderungen anzupassen. + +- Die Go-Anwendung kann bei Bedarf im `main.go`-Code weiter angepasst werden. + +Hinweis: Stelle sicher, dass die Portnummern in der Docker Compose-Konfiguration und in deinen Anfragen übereinstimmen. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 0000000..2b9b62d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,41 @@ +version: "3.9" + +services: + +# Go application service + go-app: + build: + context: go/. + args: + - GO111MODULE=on + #ports: + # - "8080:8080" + environment: + - VIRTUAL_HOST=ip.brothertec.eu + - VIRTUAL_PORT=8080 + - LETSENCRYPT_HOST=ip.brothertec.eu + - LETSENCRYPT_EMAIL=admin@brothertec.eu + + #volumes: + # - ./uploads:/uploads + + restart: always + + labels: + - flame.type=application + - flame.name=Get IP + - flame.url=https://ip.brothertec.eu + - flame.icon=ip + + networks: + default: + proxy: + edge-tier: + +networks: + proxy: + name: nginx-proxy + external: true + edge-tier: + name: edge + external: true diff --git a/go/Dockerfile b/go/Dockerfile new file mode 100755 index 0000000..85a903f --- /dev/null +++ b/go/Dockerfile @@ -0,0 +1,30 @@ +# syntax=docker/dockerfile:1 + +# Build the application from source +FROM golang:1.21.4 AS build-stage + +WORKDIR /app + +COPY *.mod ./ +RUN go mod download + +COPY *.go ./ + +RUN CGO_ENABLED=0 GOOS=linux go build -o /main + +# Run the tests in the container +FROM build-stage AS run-test-stage +RUN go test -v ./... + +# Deploy the application binary into a lean image +FROM gcr.io/distroless/base-debian11 AS build-release-stage + +WORKDIR / + +COPY --from=build-stage /main /main + +EXPOSE 8080 + +USER nonroot:nonroot + +ENTRYPOINT ["/main"] diff --git a/go/Dockerfile.old b/go/Dockerfile.old new file mode 100755 index 0000000..2c572c4 --- /dev/null +++ b/go/Dockerfile.old @@ -0,0 +1,22 @@ +# Use an official Golang runtime as a parent image +FROM golang:1.21.4 + +# Set the working directory in the container +WORKDIR /go/src/app + +# Copy the local package files to the container's workspace +COPY . . + +# Download and install any required third-party dependencies into the container. +#RUN go get -u github.com/gorilla/mux +RUN go get -u github.com/go-sql-driver/mysql +RUN go get -u github.com/sirupsen/logrus + +# Build the Go application +RUN go build -o main . + +# Expose port 8080 to the outside world +EXPOSE 8080 + +# Command to run the application with environment variables +CMD ["./main"] diff --git a/go/go.mod b/go/go.mod new file mode 100644 index 0000000..878f7df --- /dev/null +++ b/go/go.mod @@ -0,0 +1,3 @@ +module ip-resolver + +go 1.20 diff --git a/go/main.go b/go/main.go new file mode 100644 index 0000000..8402271 --- /dev/null +++ b/go/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "encoding/json" + "fmt" + "net" + "net/http" + "strings" +) + +type IPAddressResponse struct { + IPv4Address string `json:"ipv4_address"` + IPv6Address string `json:"ipv6_address"` +} + +func getIPHandler(w http.ResponseWriter, r *http.Request) { + ipHeader := r.Header.Get("X-Real-IP") + if ipHeader == "" { + http.Error(w, "X-Real-IP Header nicht vorhanden", http.StatusBadRequest) + return + } + + ip := strings.TrimSpace(strings.SplitN(ipHeader, ",", 2)[0]) // Um den Fall mit mehreren IP-Adressen zu behandeln + + ipResponse := IPAddressResponse{ + IPv4Address: getIPv4(ip), + IPv6Address: getIPv6(ip), + } + + jsonResponse, err := json.Marshal(ipResponse) + if err != nil { + http.Error(w, fmt.Sprintf("Fehler beim Erstellen der JSON-Antwort: %s", err), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.Write(jsonResponse) +} + +func getIPv4(ip string) string { + ipAddr := net.ParseIP(ip) + ipv4 := ipAddr.To4() + if ipv4 != nil { + return ipv4.String() + } + return "" +} + +func getIPv6(ip string) string { + ipAddr := net.ParseIP(ip) + ipv6 := ipAddr.To16() + if ipv6 != nil && strings.Contains(ip, ":") { + return ipv6.String() + } + return "" +} + +func main() { + http.HandleFunc("/getip", getIPHandler) + http.ListenAndServe(":8080", nil) +}