From 0e23a6535eccf354411bfc2a2a4de78d0cf323ee Mon Sep 17 00:00:00 2001 From: mmatczuk Date: Fri, 17 Feb 2017 12:11:28 +0100 Subject: [PATCH] logging fixes, removed go kit dependency --- client.go | 4 +--- cmd/cmd/log.go | 10 ++++----- cmd/tunnel/options.go | 18 +---------------- cmd/tunnel/tunnel.go | 4 ++-- cmd/tunneld/options.go | 4 ++-- pool.go | 4 +++- server.go | 46 +++++++++++++++++++++++++++++------------- 7 files changed, 46 insertions(+), 44 deletions(-) diff --git a/client.go b/client.go index 4c8e546..6ff55e5 100644 --- a/client.go +++ b/client.go @@ -165,7 +165,7 @@ func (c *Client) dial() (net.Conn, error) { if err != nil { c.logger.Log( "level", 0, - "action", "dial failed", + "msg", "dial failed", "network", network, "addr", addr, "err", err, @@ -199,8 +199,6 @@ func (c *Client) dial() (net.Conn, error) { c.logger.Log( "level", 1, "action", "backoff", - "network", network, - "addr", addr, "sleep", d, ) time.Sleep(d) diff --git a/cmd/cmd/log.go b/cmd/cmd/log.go index 7ef204d..cf9fc01 100644 --- a/cmd/cmd/log.go +++ b/cmd/cmd/log.go @@ -2,10 +2,9 @@ package cmd import ( "io" + golog "log" "os" - "time" - kitlog "github.com/go-kit/kit/log" "github.com/mmatczuk/go-http-tunnel/log" ) @@ -29,9 +28,10 @@ func NewLogger(to string, level int) (log.Logger, error) { w = f } - var logger kitlog.Logger - logger = kitlog.NewJSONLogger(kitlog.NewSyncWriter(w)) - logger = kitlog.NewContext(logger).WithPrefix("time", kitlog.Timestamp(time.Now)) + golog.SetOutput(w) + + var logger log.Logger + logger = log.NewStdLogger() logger = log.NewFilterLogger(logger, level) return logger, nil } diff --git a/cmd/tunnel/options.go b/cmd/tunnel/options.go index 6d7405f..9e35dd6 100644 --- a/cmd/tunnel/options.go +++ b/cmd/tunnel/options.go @@ -4,8 +4,6 @@ import ( "flag" "fmt" "os" - "os/user" - "path/filepath" ) const usage1 string = `Usage: tunnel [OPTIONS] [command args] [...] @@ -58,7 +56,7 @@ type options struct { func parseArgs() (*options, error) { debug := flag.Bool("debug", false, "Starts gops agent") - config := flag.String("config", filepath.Join(defaultPath(), "config.yaml"), "Path to tunnel configuration file") + config := flag.String("config", "tunnel.yaml", "Path to tunnel configuration file") logTo := flag.String("log", "stdout", "Write log messages to this file, file name or 'stdout', 'stderr', 'none'") logLevel := flag.Int("log-level", 1, "Level of messages to log, 0-3") version := flag.Bool("version", false, "Prints tunnel version") @@ -99,17 +97,3 @@ func parseArgs() (*options, error) { return opts, nil } - -func defaultPath() string { - // user.Current() does not work on linux when cross compiling because - // it requires CGO; use os.Getenv("HOME") hack until we compile natively - var dir string - - if user, err := user.Current(); err == nil { - dir = user.HomeDir - } else { - dir = os.Getenv("HOME") - } - - return filepath.Join(dir, ".tunnel") -} diff --git a/cmd/tunnel/tunnel.go b/cmd/tunnel/tunnel.go index e4fc5a7..2cdaca5 100644 --- a/cmd/tunnel/tunnel.go +++ b/cmd/tunnel/tunnel.go @@ -146,8 +146,8 @@ func proxy(m map[string]*TunnelConfig, logger log.Logger) tunnel.ProxyFunc { } return tunnel.Proxy(tunnel.ProxyFuncs{ - HTTP: tunnel.NewMultiHTTPProxy(httpURL, logger).Proxy, - TCP: tunnel.NewMultiTCPProxy(tcpAddr, logger).Proxy, + HTTP: tunnel.NewMultiHTTPProxy(httpURL, log.NewContext(logger).WithPrefix("proxy", "HTTP")).Proxy, + TCP: tunnel.NewMultiTCPProxy(tcpAddr, log.NewContext(logger).WithPrefix("proxy", "TCP")).Proxy, }) } diff --git a/cmd/tunneld/options.go b/cmd/tunneld/options.go index 7fff189..6c3068f 100644 --- a/cmd/tunneld/options.go +++ b/cmd/tunneld/options.go @@ -36,8 +36,8 @@ func parseArgs() *options { httpAddr := flag.String("httpAddr", ":80", "Public address for HTTP connections, empty string to disable") httpsAddr := flag.String("httpsAddr", ":443", "Public address listening for HTTPS connections, emptry string to disable") tunnelAddr := flag.String("tunnelAddr", ":4443", "Public address listening for tunnel client") - tlsCrt := flag.String("tlsCrt", "", "Path to a TLS certificate file") - tlsKey := flag.String("tlsKey", "", "Path to a TLS key file") + tlsCrt := flag.String("tlsCrt", "server.crt", "Path to a TLS certificate file") + tlsKey := flag.String("tlsKey", "server.key", "Path to a TLS key file") clients := flag.String("clients", "", "Comma-separated list of tunnel client ids") logTo := flag.String("log", "stdout", "Write log messages to this file, file name or 'stdout', 'stderr', 'none'") logLevel := flag.Int("log-level", 1, "Level of messages to log, 0-3") diff --git a/pool.go b/pool.go index 586d658..9be6b39 100644 --- a/pool.go +++ b/pool.go @@ -106,5 +106,7 @@ func (p *connPool) addr(identifier id.ID) string { } func (p *connPool) addrToIdentifier(addr string) id.ID { - return id.NewFromString(addr[:len(addr)-4]) + identifier := id.ID{} + identifier.UnmarshalText([]byte(addr[:len(addr)-4])) + return identifier } diff --git a/server.go b/server.go index b7b4f2f..cf76c1c 100644 --- a/server.go +++ b/server.go @@ -106,7 +106,6 @@ func (s *Server) disconnected(identifier id.ID) { "identifier", identifier, "addr", l.Addr(), ) - l.Close() } } @@ -114,23 +113,32 @@ func (s *Server) disconnected(identifier id.ID) { // Start starts accepting connections form clients. For accepting http traffic // from end users server must be run as handler on http server. func (s *Server) Start() { + addr := s.listener.Addr().String() + s.logger.Log( "level", 1, "action", "start", - "addr", s.listener.Addr(), + "addr", addr, ) for { conn, err := s.listener.Accept() if err != nil { - s.logger.Log( - "level", 2, - "msg", "accept control connection failed", - "err", err, - ) if strings.Contains(err.Error(), "use of closed network connection") { + s.logger.Log( + "level", 1, + "action", "control connection listener closed", + "addr", addr, + ) return } + + s.logger.Log( + "level", 0, + "msg", "accept control connection failed", + "addr", addr, + "err", err, + ) continue } @@ -359,7 +367,7 @@ func (s *Server) addTunnels(tunnels map[string]*proto.Tunnel, identifier id.ID) } for _, l := range i.Listeners { - s.listen(l, identifier) + go s.listen(l, identifier) } return nil @@ -380,18 +388,28 @@ func (s *Server) Unsubscribe(identifier id.ID) *RegistryItem { } func (s *Server) listen(l net.Listener, identifier id.ID) { + addr := l.Addr().String() + for { conn, err := l.Accept() if err != nil { - s.logger.Log( - "level", 2, - "msg", "accept connection failed", - "identifier", identifier, - "err", err, - ) if strings.Contains(err.Error(), "use of closed network connection") { + s.logger.Log( + "level", 2, + "action", "listener closed", + "identifier", identifier, + "addr", addr, + ) return } + + s.logger.Log( + "level", 0, + "msg", "accept connection failed", + "identifier", identifier, + "addr", addr, + "err", err, + ) continue }