Skip to content

Commit

Permalink
logging improvements
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <hexian000@outlook.com>
  • Loading branch information
hexian000 committed Sep 16, 2024
1 parent d1a6dfe commit 0669dc5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (h *ForwardHandler) Serve(ctx context.Context, accepted net.Conn) {
ioClose(dialed)
return
}
slog.Debugf("%q -> %v: forward established", h.name, dialed.RemoteAddr())
h.s.stats.success.Add(1)
}

Expand All @@ -135,6 +136,7 @@ func (h *TunnelHandler) Serve(ctx context.Context, accepted net.Conn) {
ioClose(dialed)
return
}
slog.Debugf("%v -> %q: forward established", h.t.l.Addr(), h.t.name)
}

// EmptyHandler rejects all connections
Expand Down
20 changes: 12 additions & 8 deletions v2/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,20 @@ func (t *Tunnel) addMux(mux *yamux.Session, isDialed bool) {
t.lastChanged = now
}

func (t *Tunnel) getMuxTag(mux *yamux.Session) (string, bool) {
t.mu.RLock()
defer t.mu.RUnlock()
tag, ok := t.mux[mux]
return tag, ok
}

func (t *Tunnel) delMux(mux *yamux.Session) {
now := time.Now()
getTag := func(mux *yamux.Session) string {
t.mu.RLock()
defer t.mu.RUnlock()
return t.mux[mux]
if tag, ok := t.getMuxTag(mux); ok {
msg := fmt.Sprintf("%s: connection lost", tag)
slog.Info(msg)
t.s.recentEvents.Add(now, msg)
}
msg := fmt.Sprintf("%s: connection lost", getTag(mux))
slog.Info(msg)
t.s.recentEvents.Add(now, msg)

t.mu.Lock()
defer t.mu.Unlock()
Expand Down Expand Up @@ -300,7 +304,7 @@ func (t *Tunnel) MuxDial(ctx context.Context) (net.Conn, error) {
if err != nil {
return nil, err
}
slog.Debugf("stream open: %s ID=%v", t.name, stream.StreamID())
slog.Debugf("stream open: %q ID=%v", t.name, stream.StreamID())
return stream, nil
}

Expand Down

0 comments on commit 0669dc5

Please sign in to comment.