Skip to content

Commit

Permalink
improve logs
Browse files Browse the repository at this point in the history
  • Loading branch information
hexian000 committed Nov 13, 2023
1 parent 0c979f3 commit 2578395
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 50 deletions.
7 changes: 4 additions & 3 deletions v2/forwarder/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"syscall"

"github.com/hashicorp/yamux"
"github.com/hexian000/gosnippets/formats"
"github.com/hexian000/gosnippets/routines"
"github.com/hexian000/gosnippets/slog"
)
Expand Down Expand Up @@ -46,10 +47,10 @@ func (f *forwarder) addConn(accepted net.Conn, dialed net.Conn) {

func (f *forwarder) delConn(accepted net.Conn, dialed net.Conn) {
if err := accepted.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
slog.Warningf("close: %s", formats.Error(err))
}
if err := dialed.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
slog.Warningf("close: %s", formats.Error(err))
}
f.mu.Lock()
defer f.mu.Unlock()
Expand Down Expand Up @@ -116,7 +117,7 @@ func (f *forwarder) Close() {
defer f.mu.Unlock()
for conn := range f.conn {
if err := conn.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
slog.Warningf("close: %s", formats.Error(err))
}
}
}
57 changes: 21 additions & 36 deletions v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (h *TLSHandler) Serve(ctx context.Context, conn net.Conn) {
start := time.Now()
if deadline, ok := ctx.Deadline(); ok {
if err := conn.SetDeadline(deadline); err != nil {
slog.Errorf("tunnel %q: accept %v, (%T) %v", h.t.name, conn.RemoteAddr(), err, err)
slog.Errorf("%q <= %v: %s", h.t.name, conn.RemoteAddr(), formats.Error(err))
return
}
}
Expand All @@ -51,19 +51,19 @@ func (h *TLSHandler) Serve(ctx context.Context, conn net.Conn) {
if tlscfg := h.s.getTLSConfig(); tlscfg != nil {
conn = tls.Server(conn, tlscfg)
} else {
slog.Warningf("tunnel %q: connection is not encrypted", h.t.name)
slog.Warningf("%q <= %v: connection is not encrypted", h.t.name, conn.RemoteAddr())
}
handshake := &proto.Handshake{
Identity: c.Identity,
}
if err := proto.RunHandshake(conn, handshake); err != nil {
slog.Errorf("tunnel %q: accept %v, (%T) %v", h.t.name, conn.RemoteAddr(), err, err)
slog.Errorf("%q <= %v: %s", h.t.name, conn.RemoteAddr(), formats.Error(err))
return
}
_ = conn.SetDeadline(time.Time{})
mux, err := yamux.Server(conn, h.s.getMuxConfig(true))
if err != nil {
slog.Errorf("tunnel %q: accept %v, (%T) %v", h.t.name, conn.RemoteAddr(), err, err)
slog.Errorf("%q <= %v: %s", h.t.name, conn.RemoteAddr(), formats.Error(err))
return
}
h.s.stats.authorized.Add(1)
Expand All @@ -72,45 +72,38 @@ func (h *TLSHandler) Serve(ctx context.Context, conn net.Conn) {
if t := h.s.findTunnel(handshake.Identity); t != nil {
tun = t
} else {
slog.Warningf("unknown remote identity %q", handshake.Identity)
slog.Warningf("%q <= %v: unknown identity %q", tun.name, conn.RemoteAddr(), handshake.Identity)
}
}
if err := h.s.g.Go(func() {
tun.Serve(mux)
}); err != nil {
slog.Errorf("tunnel %q: accept %v, (%T) %v", tun.name, conn.RemoteAddr(), err, err)
if err := mux.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
}
slog.Errorf("%q <= %v: %s", tun.name, conn.RemoteAddr(), formats.Error(err))
ioClose(mux)
return
}
slog.Infof("tunnel %q: accept %v, setup %v", tun.name, conn.RemoteAddr(), formats.Duration(time.Since(start)))
slog.Infof("%q <= %v: setup %v", tun.name, conn.RemoteAddr(), formats.Duration(time.Since(start)))
}

// ForwardHandler forwards connections to another plain address
type ForwardHandler struct {
s *Server
name string
dial string
}

func (h *ForwardHandler) Serve(ctx context.Context, accepted net.Conn) {
h.s.stats.request.Add(1)
dialed, err := h.s.dialDirect(ctx, h.dial)
if err != nil {
slog.Errorf("forward [%s]: %v", h.dial, err)
if err := accepted.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
}
slog.Errorf("%q -> %s: %v", h.name, h.dial, err)
ioClose(accepted)
return
}
if err := h.s.f.Forward(accepted, dialed); err != nil {
slog.Errorf("forward [%s]: %v", h.dial, err)
if err := accepted.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
}
if err := dialed.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
}
slog.Errorf("%q -> %s: %v", h.name, h.dial, err)
ioClose(accepted)
ioClose(dialed)
return
}
h.s.stats.success.Add(1)
Expand All @@ -126,23 +119,17 @@ func (h *TunnelHandler) Serve(ctx context.Context, accepted net.Conn) {
dialed, err := h.t.MuxDial(ctx)
if err != nil {
if errors.Is(err, ErrNoSession) {
slog.Debugf("tunnel %q: %v", h.t.name, err)
slog.Debugf("%v -> %q: %s", accepted.RemoteAddr(), h.t.name, formats.Error(err))
} else {
slog.Errorf("tunnel %q: (%T) %v", h.t.name, err, err)
}
if err := accepted.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
slog.Errorf("%v -> %q: %s", accepted.RemoteAddr(), h.t.name, formats.Error(err))
}
ioClose(accepted)
return
}
if err := h.s.f.Forward(accepted, dialed); err != nil {
slog.Errorf("tunnel %q: (%T) %v", h.t.name, err, err)
if err := accepted.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
}
if err := dialed.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
}
slog.Errorf("%v -> %q: %s", accepted.RemoteAddr(), h.t.name, formats.Error(err))
ioClose(accepted)
ioClose(dialed)
return
}
}
Expand All @@ -151,7 +138,5 @@ func (h *TunnelHandler) Serve(ctx context.Context, accepted net.Conn) {
type EmptyHandler struct{}

func (h *EmptyHandler) Serve(_ context.Context, accepted net.Conn) {
if err := accepted.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
}
ioClose(accepted)
}
13 changes: 13 additions & 0 deletions v2/tlswrapper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
package tlswrapper

import (
"io"

"github.com/hexian000/gosnippets/formats"
"github.com/hexian000/gosnippets/slog"
)

var (
Version = "dev"
Homepage = "https://github.com/hexian000/tlswrapper"
)

func ioClose(c io.Closer) {
if err := c.Close(); err != nil {
slog.Warningf("close: %s", formats.Error(err))
}
}
17 changes: 6 additions & 11 deletions v2/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ func (t *Tunnel) run() {
t.mu.Lock()
defer t.mu.Unlock()
for mux := range t.mux {
if err := mux.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
}
ioClose(mux)
delete(t.mux, mux)
}
}()
Expand Down Expand Up @@ -181,8 +179,7 @@ func (t *Tunnel) Serve(mux *yamux.Session) {
var h Handler
if t.c.Dial != "" {
h = &ForwardHandler{
t.s,
t.c.Dial,
t.s, t.name, t.c.Dial,
}
} else {
h = &EmptyHandler{}
Expand Down Expand Up @@ -219,7 +216,7 @@ func (t *Tunnel) dial(ctx context.Context) (*yamux.Session, error) {
if tlscfg := t.s.getTLSConfig(); tlscfg != nil {
conn = tls.Client(conn, tlscfg)
} else {
slog.Warningf("tunnel %q: connection is not encrypted", t.name)
slog.Warningf("%q => %v: connection is not encrypted", t.name, conn.RemoteAddr())
}
handshake := &proto.Handshake{
Identity: c.Identity,
Expand All @@ -237,18 +234,16 @@ func (t *Tunnel) dial(ctx context.Context) (*yamux.Session, error) {
if found := t.s.findTunnel(handshake.Identity); found != nil {
tun = found
} else {
slog.Warningf("unknown remote identity %q", handshake.Identity)
slog.Warningf("%q => %v: unknown identity %q", t.name, conn.RemoteAddr(), handshake.Identity)
}
}
if err := t.s.g.Go(func() {
tun.Serve(mux)
}); err != nil {
if err := mux.Close(); err != nil {
slog.Warningf("close: (%T) %v", err, err)
}
ioClose(mux)
return nil, err
}
slog.Infof("tunnel %q: dial %v, setup: %v", t.name, conn.RemoteAddr(), formats.Duration(time.Since(start)))
slog.Infof("%q => %v: setup %v", t.name, conn.RemoteAddr(), formats.Duration(time.Since(start)))
return mux, nil
}

Expand Down

0 comments on commit 2578395

Please sign in to comment.