From 9a0c086741aecc9124e3aec5273a1260154a438e Mon Sep 17 00:00:00 2001 From: Pablu23 Date: Sun, 9 Jun 2024 17:27:28 +0200 Subject: [PATCH] Initial commit working so far, without cookie support --- go.mod | 3 ++ main.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9e7abfe --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/pablu23/domain-router + +go 1.22.3 diff --git a/main.go b/main.go new file mode 100644 index 0000000..3a074d1 --- /dev/null +++ b/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "errors" + "fmt" + "io" + "net/http" +) + +func main() { + domains := make(map[string]int) + domains["test.localhost"] = 8181 + domains["test2.localhost"] = 8282 + + client := &http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + port := domains[r.Host] + fmt.Printf("Received request on Port: %d\n", port) + // logRequest(*r) + + subUrlPath := r.URL.RequestURI() + req, err := http.NewRequest(r.Method, fmt.Sprintf("http://localhost:%d%s", port, subUrlPath), r.Body) + if err != nil { + panic(err) + } + // logRequest(*req) + for _, cookie := range r.Cookies() { + fmt.Printf("Setting cookie, Name: %s, Value: %s\n", cookie.Name, cookie.Value) + req.AddCookie(cookie) + } + + // fmt.Println("Sending request to nested server") + res, err := client.Do(req) + if err != nil { + panic(err) + } + if loc, err := res.Location(); !errors.Is(err, http.ErrNoLocation) { + http.Redirect(w, r, loc.RequestURI(), http.StatusFound) + return + } + + for _, cookie := range res.Cookies() { + fmt.Printf("Setting cookie, Name: %s, Value: %s\n", cookie.Name, cookie.Value) + http.SetCookie(w, cookie) + } + + for name, values := range res.Header { + for _, value := range values { + // fmt.Printf("Adding Header vor name: %s, value: %s\n", name, value) + w.Header().Set(name, value) + } + } + w.WriteHeader(res.StatusCode) + + // fmt.Println("Reading Body") + body, err := io.ReadAll(res.Body) + if err != nil { + panic(err) + } + defer res.Body.Close() + // fmt.Printf("Body: %s\n", hex.EncodeToString(body)) + + _, err = w.Write(body) + if err != nil { + panic(err) + } + fmt.Println("----------------------------------------") + }) + + fmt.Println("Starting server on :80") + http.ListenAndServe(":80", nil) +} + +func logRequest(r http.Request) { + fmt.Printf("URL: %s %s\n", r.Method, r.URL) + fmt.Println("HEADER: ") + for name, values := range r.Header { + for _, value := range values { + fmt.Printf("%s: %s\n", name, value) + // w.Header().Set(name, value) + } + } + rbody, err := io.ReadAll(r.Body) + if err != nil { + panic(err) + } + fmt.Printf("BODY: %s\n", rbody) +}