From b5f3f660f411a20fc0bdd99bf6a99e15546f287e Mon Sep 17 00:00:00 2001 From: PhatPhuckDave <> Date: Sun, 21 Jul 2024 19:01:07 +0200 Subject: [PATCH] Implement IP echo --- deploy.sh | 1 + dockerfile | 32 ++++++++++++++++++++++++++++++++ main.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 deploy.sh create mode 100644 dockerfile diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..b324477 --- /dev/null +++ b/deploy.sh @@ -0,0 +1 @@ +go build main && docker build -t echo-ip . \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..0f1e916 --- /dev/null +++ b/dockerfile @@ -0,0 +1,32 @@ +FROM golang:1.22.4-bullseye as base + +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid 65532 \ + small-user + +WORKDIR $GOPATH/src/app/ + +COPY . . + +RUN go mod download +RUN go mod verify + +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /main . + +FROM scratch + +COPY --from=base /usr/share/zoneinfo /usr/share/zoneinfo +COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=base /etc/passwd /etc/passwd +COPY --from=base /etc/group /etc/group + +COPY --from=base /main . + +USER small-user:small-user + +CMD ["/main"] \ No newline at end of file diff --git a/main.go b/main.go index e69de29..73da53e 100644 --- a/main.go +++ b/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "io" + "log" + "os" + "net/http" +) + +var Error *log.Logger +func init() { + log.SetFlags(log.Lmicroseconds | log.Lshortfile) + // logFile, err := os.Create("main.log") + // if err != nil { + // log.Printf("Error creating log file: %v", err) + // os.Exit(1) + // } + logger := io.MultiWriter(os.Stdout) + log.SetOutput(logger) + + Error = log.New(io.MultiWriter(os.Stderr, os.Stdout), + fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"), + log.Lmicroseconds|log.Lshortfile) +} + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, r.RemoteAddr) + }) + + http.ListenAndServe(":8080", nil) +} \ No newline at end of file