Better logging
This commit is contained in:
8
go.mod
8
go.mod
@@ -1,3 +1,11 @@
|
|||||||
module github.com/pablu23/domain-router
|
module github.com/pablu23/domain-router
|
||||||
|
|
||||||
go 1.22.3
|
go 1.22.3
|
||||||
|
|
||||||
|
require github.com/rs/zerolog v1.33.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||||
|
golang.org/x/sys v0.12.0 // indirect
|
||||||
|
)
|
||||||
|
|||||||
15
go.sum
Normal file
15
go.sum
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||||
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||||
|
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||||
|
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
|
||||||
|
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
|
||||||
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
101
main.go
101
main.go
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -11,10 +12,15 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
configFileFlag = flag.String("config", "domains.conf", "Path to Domain config file")
|
configFileFlag = flag.String("config", "domains.conf", "Path to Domain config file")
|
||||||
|
certFlag = flag.String("cert", "", "Path to cert file")
|
||||||
|
keyFlag = flag.String("key", "", "Path to key file")
|
||||||
|
portFlag = flag.Int("port", 80, "Port")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -37,15 +43,16 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rDump, err := httputil.DumpRequest(r, true)
|
if !dumpRequest(w, r) {
|
||||||
if err != nil {
|
return
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
subUrlPath := r.URL.RequestURI()
|
subUrlPath := r.URL.RequestURI()
|
||||||
req, err := http.NewRequest(r.Method, fmt.Sprintf("http://localhost:%d%s", port, subUrlPath), r.Body)
|
req, err := http.NewRequest(r.Method, fmt.Sprintf("http://localhost:%d%s", port, subUrlPath), r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Error().Err(err).Str("path", subUrlPath).Int("port", port).Msg("Could not create request")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, values := range r.Header {
|
for name, values := range r.Header {
|
||||||
@@ -55,18 +62,18 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, cookie := range r.Cookies() {
|
for _, cookie := range r.Cookies() {
|
||||||
// fmt.Printf("Setting cookie, Name: %s, Value: %s\n", cookie.Name, cookie.Value)
|
|
||||||
req.AddCookie(cookie)
|
req.AddCookie(cookie)
|
||||||
}
|
}
|
||||||
|
|
||||||
reqDump, err := httputil.DumpRequestOut(req, true)
|
if !dumpRequest(w, req) {
|
||||||
if err != nil {
|
return
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Error().Err(err).Str("path", subUrlPath).Int("port", port).Msg("Could not complete request")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cookies := res.Cookies()
|
cookies := res.Cookies()
|
||||||
@@ -75,9 +82,8 @@ func main() {
|
|||||||
http.SetCookie(w, cookie)
|
http.SetCookie(w, cookie)
|
||||||
}
|
}
|
||||||
|
|
||||||
resDump, err := httputil.DumpResponse(res, true)
|
if !dumpResponse(w, res) {
|
||||||
if err != nil {
|
return
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if loc, err := res.Location(); !errors.Is(err, http.ErrNoLocation) {
|
if loc, err := res.Location(); !errors.Is(err, http.ErrNoLocation) {
|
||||||
@@ -91,29 +97,70 @@ func main() {
|
|||||||
w.WriteHeader(res.StatusCode)
|
w.WriteHeader(res.StatusCode)
|
||||||
|
|
||||||
body, err := io.ReadAll(res.Body)
|
body, err := io.ReadAll(res.Body)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Could not read body")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
_, err = w.Write(body)
|
_, err = w.Write(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Error().Err(err).Msg("Could not write body")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Method == "POST" {
|
|
||||||
fmt.Print(string(rDump) + "\n\n")
|
|
||||||
fmt.Print(string(reqDump) + "\n\n")
|
|
||||||
fmt.Print(string(resDump) + "\n")
|
|
||||||
fmt.Println("----------------------------------------")
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println("Starting server on :443")
|
server := http.Server{
|
||||||
http.ListenAndServeTLS(":443", "server.crt", "server.key", nil)
|
Addr: fmt.Sprintf(":%d", *portFlag),
|
||||||
|
}
|
||||||
|
|
||||||
|
if *certFlag != "" && *keyFlag != "" {
|
||||||
|
server.TLSConfig = &tls.Config{
|
||||||
|
GetCertificate: func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
||||||
|
cert, err := tls.LoadX509KeyPair(*certFlag, *keyFlag)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &cert, err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
log.Info().Int("port", *portFlag).Str("cert", *certFlag).Str("key", *keyFlag).Msg("Starting server")
|
||||||
|
err := server.ListenAndServeTLS("", "")
|
||||||
|
log.Fatal().Err(err).Str("cert", *certFlag).Str("key", *keyFlag).Int("port", *portFlag).Msg("Could not start server")
|
||||||
|
} else {
|
||||||
|
log.Info().Int("port", *portFlag).Msg("Starting server")
|
||||||
|
err := server.ListenAndServe()
|
||||||
|
log.Fatal().Err(err).Int("port", *portFlag).Msg("Could not start server")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dumpRequest(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
if e := log.Debug(); e.Enabled() {
|
||||||
|
rDump, err := httputil.DumpRequest(r, true)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Could not dump request")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
log.Debug().Str("dump", string(rDump)).Send()
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func dumpResponse(w http.ResponseWriter, r *http.Response) bool {
|
||||||
|
if e := log.Debug(); e.Enabled() {
|
||||||
|
dump, err := httputil.DumpResponse(r, true)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Could not dump response")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
log.Debug().Str("dump", string(dump)).Send()
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig(path string) (map[string]int, error) {
|
func loadConfig(path string) (map[string]int, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user