From 51a40c981ab3a4902fb7dd661b2e4eddb1994c12 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 23 Apr 2023 23:04:37 -0500 Subject: begin adding conf tool --- .gitignore | 3 +- conf/go.mod | 3 ++ conf/main.go | 9 +++++ go.mod | 7 ---- go.sum | 15 -------- main.go | 112 ---------------------------------------------------------- phlox/go.mod | 7 ++++ phlox/go.sum | 15 ++++++++ phlox/main.go | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 148 insertions(+), 135 deletions(-) create mode 100644 conf/go.mod create mode 100644 conf/main.go delete mode 100644 go.mod delete mode 100644 go.sum delete mode 100644 main.go create mode 100644 phlox/go.mod create mode 100644 phlox/go.sum create mode 100644 phlox/main.go diff --git a/.gitignore b/.gitignore index a80fc7e..b5dc760 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -phlox +phlox/phlox +conf/phloxconf diff --git a/conf/go.mod b/conf/go.mod new file mode 100644 index 0000000..81b2db5 --- /dev/null +++ b/conf/go.mod @@ -0,0 +1,3 @@ +module sanine.net/phlox/phloxconf + +go 1.20 diff --git a/conf/main.go b/conf/main.go new file mode 100644 index 0000000..13bcf35 --- /dev/null +++ b/conf/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Printf("hello world") +} diff --git a/go.mod b/go.mod deleted file mode 100644 index a2f6f57..0000000 --- a/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index ed65537..0000000 --- a/go.sum +++ /dev/null @@ -1,15 +0,0 @@ -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 deleted file mode 100644 index 3516374..0000000 --- a/main.go +++ /dev/null @@ -1,112 +0,0 @@ -package main - -import ( - "fmt" - "io" - "strings" - "net" - "net/http" - "net/url" - log "github.com/sirupsen/logrus" -) - - -func main() { - const addr = "localhost:3333" - - log.Info("configuring reverse proxy...") - configureEndpoint("/proxy1", "http://localhost:8000") - - log.Infof("serving on %v", addr) - log.Fatal(http.ListenAndServe(addr, nil)) -} - - -type Endpoint struct { - Path string - Origin *url.URL -} - - -func configureEndpoint(path, address string) { - log.Infof("proxying endpoint %v to %v", path, address) - origin, err := url.Parse(address) - if err != nil { - log.Fatal(err) - } - - end := Endpoint{ - Path: path, - Origin: origin, - } - - http.HandleFunc(path + "/", func(w http.ResponseWriter, req *http.Request) { - log.Infof("REQ: %v", req.URL.Path) - proxy(w, req, end) - }) -} - - -func proxy(w http.ResponseWriter, req *http.Request, end Endpoint) { - response := proxyRequest(w, req, end) - proxyResponse(w, response) -} - - -func proxyRequest(w http.ResponseWriter, req *http.Request, end Endpoint) *http.Response { - // configure host address - req.Host = end.Origin.Host - req.URL.Host = end.Origin.Host - - // strip proxy endpoint path from request path - req.URL.Path = strings.TrimPrefix(req.URL.Path, end.Path) - - // set X-Forwarded-For - forwardedFor, _, _ := net.SplitHostPort(req.RemoteAddr) - req.Header.Set("X-Forwarded-For", forwardedFor) - - // misc request cleanups - req.URL.Scheme = end.Origin.Scheme - req.RequestURI = "" - - // make request - response, err := http.DefaultClient.Do(req) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprintf(w, "%v", err) - log.Fatal(err) - } - - return response -} - - -func proxyResponse(w http.ResponseWriter, response *http.Response) { - // 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 to client - io.Copy(w, response.Body) - - // write trailers - for key, values := range response.Trailer { - for _, value := range values { - w.Header().Set(key, value) - } - } -} diff --git a/phlox/go.mod b/phlox/go.mod new file mode 100644 index 0000000..cd9243f --- /dev/null +++ b/phlox/go.mod @@ -0,0 +1,7 @@ +module sanine.net/git/phlox/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/phlox/go.sum b/phlox/go.sum new file mode 100644 index 0000000..ed65537 --- /dev/null +++ b/phlox/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/phlox/main.go b/phlox/main.go new file mode 100644 index 0000000..3516374 --- /dev/null +++ b/phlox/main.go @@ -0,0 +1,112 @@ +package main + +import ( + "fmt" + "io" + "strings" + "net" + "net/http" + "net/url" + log "github.com/sirupsen/logrus" +) + + +func main() { + const addr = "localhost:3333" + + log.Info("configuring reverse proxy...") + configureEndpoint("/proxy1", "http://localhost:8000") + + log.Infof("serving on %v", addr) + log.Fatal(http.ListenAndServe(addr, nil)) +} + + +type Endpoint struct { + Path string + Origin *url.URL +} + + +func configureEndpoint(path, address string) { + log.Infof("proxying endpoint %v to %v", path, address) + origin, err := url.Parse(address) + if err != nil { + log.Fatal(err) + } + + end := Endpoint{ + Path: path, + Origin: origin, + } + + http.HandleFunc(path + "/", func(w http.ResponseWriter, req *http.Request) { + log.Infof("REQ: %v", req.URL.Path) + proxy(w, req, end) + }) +} + + +func proxy(w http.ResponseWriter, req *http.Request, end Endpoint) { + response := proxyRequest(w, req, end) + proxyResponse(w, response) +} + + +func proxyRequest(w http.ResponseWriter, req *http.Request, end Endpoint) *http.Response { + // configure host address + req.Host = end.Origin.Host + req.URL.Host = end.Origin.Host + + // strip proxy endpoint path from request path + req.URL.Path = strings.TrimPrefix(req.URL.Path, end.Path) + + // set X-Forwarded-For + forwardedFor, _, _ := net.SplitHostPort(req.RemoteAddr) + req.Header.Set("X-Forwarded-For", forwardedFor) + + // misc request cleanups + req.URL.Scheme = end.Origin.Scheme + req.RequestURI = "" + + // make request + response, err := http.DefaultClient.Do(req) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "%v", err) + log.Fatal(err) + } + + return response +} + + +func proxyResponse(w http.ResponseWriter, response *http.Response) { + // 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 to client + io.Copy(w, response.Body) + + // write trailers + for key, values := range response.Trailer { + for _, value := range values { + w.Header().Set(key, value) + } + } +} -- cgit v1.2.1