first commit
This commit is contained in:
commit
b29d135030
6 changed files with 220 additions and 0 deletions
63
README.md
Normal file
63
README.md
Normal file
|
@ -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.
|
41
docker-compose.yml
Executable file
41
docker-compose.yml
Executable file
|
@ -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
|
30
go/Dockerfile
Executable file
30
go/Dockerfile
Executable file
|
@ -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"]
|
22
go/Dockerfile.old
Executable file
22
go/Dockerfile.old
Executable file
|
@ -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"]
|
3
go/go.mod
Normal file
3
go/go.mod
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
module ip-resolver
|
||||||
|
|
||||||
|
go 1.20
|
61
go/main.go
Normal file
61
go/main.go
Normal file
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in a new issue