From c3a2afa0246ee1cc34409ad83c9333e2b0cb1019 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 23 Apr 2023 20:52:16 -0500 Subject: initial commit --- .gitattributes | 1 + .gitignore | 1 + go.mod | 7 ++++++ go.sum | 15 ++++++++++++ main.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a80fc7e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +phlox diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a2f6f57 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module sanine.net/git/phlox + +go 1.20 + +require github.com/sirupsen/logrus v1.9.0 + +require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ed65537 --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..bd85dcc --- /dev/null +++ b/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + "io" + "strings" + "net" + "net/http" + "net/url" + log "github.com/sirupsen/logrus" +) + + +//type Endpoint struct { +// path string +// address string +//} +// +// +//func proxy(w http.ResponseWriter, req *http.Request, end Endpoint) { +//} +// +//func makeProxiedRequest(w http.ResponseWriter, req *http.Request, end Endpoint) { +// +//} +// + +func main() { + const addr = "localhost:3333" + log.Info("configuring reverse proxy...") + origin, err := url.Parse("http://localhost:8000") + if err != nil { + log.Fatal(err) + } + log.Infof("listening on %v", addr) + http.ListenAndServe(addr, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + req.Host = origin.Host + req.URL.Host = origin.Host + req.URL.Scheme = origin.Scheme + req.RequestURI = "" + // set X-Forwarded-For + forwardedFor, _, _ := net.SplitHostPort(req.RemoteAddr) + req.Header.Set("X-Forwarded-For", forwardedFor) + response, err := http.DefaultClient.Do(req) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "%v", err) + log.Fatal(err) + } + + // copy header + for key, values := range response.Header { + for _, value := range values { + w.Header().Add(key, value) + } + } + + // get trailer keys + trailerKeys := []string{} + for key := range response.Trailer { + trailerKeys = append(trailerKeys, key) + } + if (len(trailerKeys) > 0) { + w.Header().Set("Trailer", strings.Join(trailerKeys, ",")) + } + + w.WriteHeader(response.StatusCode) + + // copy body + io.Copy(w, response.Body) + })) +} -- cgit v1.2.1