logging fixes, removed go kit dependency

This commit is contained in:
mmatczuk 2017-02-17 12:11:28 +01:00
parent 40d575f1e4
commit 0e23a6535e
7 changed files with 46 additions and 44 deletions

View file

@ -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)

View file

@ -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
}

View file

@ -4,8 +4,6 @@ import (
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
)
const usage1 string = `Usage: tunnel [OPTIONS] <command> [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")
}

View file

@ -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,
})
}

View file

@ -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")

View file

@ -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
}

View file

@ -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 {
if strings.Contains(err.Error(), "use of closed network connection") {
s.logger.Log(
"level", 2,
"msg", "accept connection failed",
"action", "listener closed",
"identifier", identifier,
"err", err,
"addr", addr,
)
if strings.Contains(err.Error(), "use of closed network connection") {
return
}
s.logger.Log(
"level", 0,
"msg", "accept connection failed",
"identifier", identifier,
"addr", addr,
"err", err,
)
continue
}