author | Alberto Bertogli
<albertito@blitiri.com.ar> 2017-08-21 15:10:28 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2017-08-21 15:10:28 UTC |
parent | ac5fd96d2fc1ce44fc426b4bf3ad69bffbd502d5 |
gofer.go | +9 | -10 |
proxy/http.go | +10 | -11 |
proxy/raw.go | +7 | -6 |
util/util.go | +0 | -46 |
diff --git a/gofer.go b/gofer.go index 3fa8364..dcb473c 100644 --- a/gofer.go +++ b/gofer.go @@ -8,7 +8,7 @@ import ( "blitiri.com.ar/go/gofer/config" "blitiri.com.ar/go/gofer/proxy" - "blitiri.com.ar/go/gofer/util" + "blitiri.com.ar/go/log" ) // Flags. @@ -19,15 +19,15 @@ var ( func main() { flag.Parse() - util.InitLog() + log.Init() conf, err := config.Load(*configfile) if err != nil { - util.Log.Fatalf("error reading config file: %v", err) + log.Fatalf("error reading config file: %v", err) } for _, k := range conf.Undecoded() { - util.Log.Printf("warning: undecoded config key: %q", k) + log.Infof("warning: undecoded config key: %q", k) } for _, https := range conf.HTTPS { @@ -49,15 +49,14 @@ func main() { mux.HandleFunc("/debug/config", dumpConfigFunc(conf)) server := http.Server{ - Addr: conf.ControlAddr, - ErrorLog: util.Log, - Handler: mux, + Addr: conf.ControlAddr, + Handler: mux, } - util.Log.Printf("%s Starting monitoring server ", server.Addr) - util.Log.Fatal(server.ListenAndServe()) + log.Infof("%s Starting monitoring server ", server.Addr) + log.Fatalf("%v", server.ListenAndServe()) } else { - util.Log.Print("No monitoring server, idle loop") + log.Infof("No monitoring server, idle loop") time.Sleep(1 * time.Hour) } } diff --git a/proxy/http.go b/proxy/http.go index fa6ee88..fe7de4c 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -9,12 +9,12 @@ import ( "blitiri.com.ar/go/gofer/config" "blitiri.com.ar/go/gofer/util" + "blitiri.com.ar/go/log" ) func httpServer(conf config.HTTP) *http.Server { srv := &http.Server{ - Addr: conf.Addr, - ErrorLog: util.Log, + Addr: conf.Addr, // TODO: timeouts. } @@ -24,10 +24,10 @@ func httpServer(conf config.HTTP) *http.Server { for from, to := range conf.RouteTable { toURL, err := url.Parse(to) if err != nil { - util.Log.Fatalf("route %q -> %q: destination is not a valid URL: %v", + log.Fatalf("route %q -> %q: destination is not a valid URL: %v", from, to, err) } - util.Log.Printf("%s route %q -> %q", srv.Addr, from, toURL) + log.Infof("%s route %q -> %q", srv.Addr, from, toURL) mux.Handle(from, makeProxy(from, *toURL)) } @@ -36,9 +36,9 @@ func httpServer(conf config.HTTP) *http.Server { func HTTP(conf config.HTTP) { srv := httpServer(conf) - util.Log.Printf("HTTP proxy on %q", conf.Addr) + log.Infof("HTTP proxy on %q", conf.Addr) err := srv.ListenAndServe() - util.Log.Fatalf("HTTP proxy exited: %v", err) + log.Fatalf("HTTP proxy exited: %v", err) } func HTTPS(conf config.HTTPS) { @@ -47,17 +47,16 @@ func HTTPS(conf config.HTTPS) { srv.TLSConfig, err = util.LoadCerts(conf.Certs) if err != nil { - util.Log.Fatalf("error loading certs: %v", err) + log.Fatalf("error loading certs: %v", err) } - util.Log.Printf("HTTPS proxy on %q", srv.Addr) + log.Infof("HTTPS proxy on %q", srv.Addr) err = srv.ListenAndServeTLS("", "") - util.Log.Fatalf("HTTPS proxy exited: %v", err) + log.Fatalf("HTTPS proxy exited: %v", err) } func makeProxy(from string, to url.URL) http.Handler { proxy := &httputil.ReverseProxy{} - proxy.ErrorLog = util.Log proxy.Transport = transport // Director that strips "from" from the request path, so that if we have @@ -124,7 +123,7 @@ func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error) } // 1.2.3.4:34575 HTTP/2.0 domain.com https://backend/path -> 200 - util.Log.Printf("%s %s %s %s -> %s%s", + log.Infof("%s %s %s %s -> %s%s", req.RemoteAddr, req.Proto, req.Host, req.URL, resps, errs) diff --git a/proxy/raw.go b/proxy/raw.go index 32f7f67..88564a0 100644 --- a/proxy/raw.go +++ b/proxy/raw.go @@ -7,6 +7,7 @@ import ( "blitiri.com.ar/go/gofer/config" "blitiri.com.ar/go/gofer/util" + "blitiri.com.ar/go/log" ) func Raw(conf config.Raw) { @@ -16,7 +17,7 @@ func Raw(conf config.Raw) { if conf.Certs != "" { tlsConfig, err = util.LoadCerts(conf.Certs) if err != nil { - util.Log.Fatalf("error loading certs: %v", err) + log.Fatalf("error loading certs: %v", err) } } @@ -27,14 +28,14 @@ func Raw(conf config.Raw) { lis, err = net.Listen("tcp", conf.Addr) } if err != nil { - util.Log.Fatalf("error listening: %v", err) + log.Fatalf("error listening: %v", err) } - util.Log.Printf("Raw proxy on %q", conf.Addr) + log.Infof("Raw proxy on %q", conf.Addr) for { conn, err := lis.Accept() if err != nil { - util.Log.Fatalf("%s error accepting: %v", conf.Addr, err) + log.Fatalf("%s error accepting: %v", conf.Addr, err) } go forward(conn, conf.To, conf.ToTLS) @@ -54,7 +55,7 @@ func forward(src net.Conn, dstAddr string, dstTLS bool) { } if err != nil { - util.Log.Printf("%s error dialing back: %v", src.LocalAddr(), err) + log.Errorf("%s error dialing back: %v", src.LocalAddr(), err) return } defer dst.Close() @@ -63,7 +64,7 @@ func forward(src net.Conn, dstAddr string, dstTLS bool) { util.BidirCopy(src, dst) end := time.Now() - util.Log.Printf("%s raw %s -> %s (%s+%s=%s)", + log.Infof("%s raw %s -> %s (%s+%s=%s)", src.RemoteAddr(), src.LocalAddr(), dst.RemoteAddr(), startCopy.Sub(start), end.Sub(startCopy), end.Sub(start)) } diff --git a/util/util.go b/util/util.go index 55420b8..e2e0ead 100644 --- a/util/util.go +++ b/util/util.go @@ -3,59 +3,13 @@ package util import ( "crypto/tls" - "flag" "fmt" "io" "io/ioutil" - "log" - "log/syslog" "os" "path/filepath" ) -// Log is used to log messages. -var Log *log.Logger - -func init() { - // Always have a log from early initialization; this helps with coding - // errors and can simplify some tests. - Log = log.New(os.Stderr, "<early> ", - log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile) -} - -// Flags. -var ( - logfile = flag.String("logfile", "-", - "File to write logs to, use '-' for stdout") -) - -func InitLog() { - var err error - var logfd io.Writer - var flags int - - if *logfile == "-" { - logfd = os.Stdout - flags |= log.Lshortfile - } else if *logfile != "" { - logfd, err = os.OpenFile(*logfile, - os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0664) - if err != nil { - log.Fatalf("error opening log file %s: %v", *logfile, err) - } - flags |= log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile - } else { - logfd, err = syslog.New( - syslog.LOG_INFO|syslog.LOG_DAEMON, "gofer") - if err != nil { - log.Fatalf("error opening syslog: %v", err) - } - flags |= log.Lshortfile - } - - Log = log.New(logfd, "", flags) -} - // LoadCerts loads certificates from the given directory, and returns a TLS // config including them. func LoadCerts(certDir string) (*tls.Config, error) {