Skip to content

Commit

Permalink
logging fixes, removed go kit dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatczuk committed Feb 17, 2017
1 parent 40d575f commit 0e23a65
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 41 deletions.
4 changes: 1 addition & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions cmd/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
18 changes: 1 addition & 17 deletions cmd/tunnel/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
)

const usage1 string = `Usage: tunnel [OPTIONS] <command> [command args] [...]
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
}
4 changes: 2 additions & 2 deletions cmd/tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/tunneld/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
40 changes: 29 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,39 @@ func (s *Server) disconnected(identifier id.ID) {
"identifier", identifier,
"addr", l.Addr(),
)

l.Close()
}
}

// 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 {
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", 2,
"level", 0,
"msg", "accept control connection failed",
"addr", addr,
"err", err,
)
if strings.Contains(err.Error(), "use of closed network connection") {
return
}
continue
}

Expand Down Expand Up @@ -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
Expand All @@ -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,
"action", "listener closed",
"identifier", identifier,
"addr", addr,
)
return
}

s.logger.Log(
"level", 2,
"level", 0,
"msg", "accept connection failed",
"identifier", identifier,
"addr", addr,
"err", err,
)
if strings.Contains(err.Error(), "use of closed network connection") {
return
}
continue
}

Expand Down

0 comments on commit 0e23a65

Please sign in to comment.