Skip to content

Commit

Permalink
fix tunnel log
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Sep 16, 2024
1 parent 9c1f4db commit d841f91
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (h *TLSHandler) Serve(ctx context.Context, conn net.Conn) {
slog.Infof("%q <= %v: unknown identity %q", t.name, conn.RemoteAddr(), handshake.Identity)
}
}
t.addMux(mux)
t.addMux(mux, false)
if err := h.s.g.Go(func() {
defer t.delMux(mux)
t.Serve(mux)
Expand Down
50 changes: 31 additions & 19 deletions v2/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ type Tunnel struct {
c *TunnelConfig
l hlistener.Listener
mu sync.RWMutex
mux map[*yamux.Session]struct{}
muxCloseSig chan *yamux.Session
mux map[*yamux.Session]string // map[mux]tag
redialCount int
dialMu sync.Mutex
lastChanged time.Time
}

func NewTunnel(s *Server, c *TunnelConfig) *Tunnel {
return &Tunnel{
name: c.Identity,
s: s,
c: c,
mux: make(map[*yamux.Session]struct{}),
muxCloseSig: make(chan *yamux.Session, 16),
name: c.Identity,
s: s,
c: c,
mux: make(map[*yamux.Session]string),
}
}

Expand Down Expand Up @@ -124,9 +122,6 @@ func (t *Tunnel) runWithRedial() {
for {
t.redial()
select {
case mux := <-t.muxCloseSig:
slog.Infof("tunnel %q: connection lost %v", t.name, mux.RemoteAddr())
t.s.recentEvents.Add(time.Now(), fmt.Sprintf("%q => %v: connection lost", t.name, mux.RemoteAddr()))
case <-t.scheduleRedial():
case <-t.s.g.CloseC():
// server shutdown
Expand All @@ -150,17 +145,25 @@ func (t *Tunnel) run() {
}
for {
select {
case mux := <-t.muxCloseSig:
slog.Infof("tunnel %q: connection lost %v", t.name, mux.RemoteAddr())
t.s.recentEvents.Add(time.Now(), fmt.Sprintf("%q => %v: connection lost", t.name, mux.RemoteAddr()))
case <-t.s.g.CloseC():
// server shutdown
return
}
}
}

func (t *Tunnel) addMux(mux *yamux.Session) {
func (t *Tunnel) addMux(mux *yamux.Session, isDialed bool) {
now := time.Now()
var tag string
if isDialed {
tag = fmt.Sprintf("%q => %v", t.name, mux.RemoteAddr())
} else {
tag = fmt.Sprintf("%q <= %v", t.name, mux.RemoteAddr())
}
msg := fmt.Sprintf("%s: established", tag)
slog.Info(msg)
t.s.recentEvents.Add(now, msg)

t.mu.Lock()
defer t.mu.Unlock()
num := len(t.mux)
Expand All @@ -169,13 +172,22 @@ func (t *Tunnel) addMux(mux *yamux.Session) {
delete(t.mux, mux)
}
}
t.mux[mux] = struct{}{}
t.mux[mux] = tag
t.s.numSessions.Add(uint32(len(t.mux) - num))
t.lastChanged = time.Now()
t.lastChanged = now
}

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

t.mu.Lock()
defer t.mu.Unlock()
num := len(t.mux)
Expand All @@ -186,7 +198,7 @@ func (t *Tunnel) delMux(mux *yamux.Session) {
}
}
t.s.numSessions.Add(uint32(len(t.mux) - num))
t.lastChanged = time.Now()
t.lastChanged = now
}

func (t *Tunnel) getMux() *yamux.Session {
Expand Down Expand Up @@ -270,7 +282,7 @@ func (t *Tunnel) dial(ctx context.Context) (*yamux.Session, error) {
slog.Infof("%q => %v: unknown identity %q", t.name, conn.RemoteAddr(), handshake.Identity)
}
}
t.addMux(mux)
t.addMux(mux, true)
if err := t.s.g.Go(func() {
defer t.delMux(mux)
tun.Serve(mux)
Expand Down

0 comments on commit d841f91

Please sign in to comment.