feat: add logging for forwarded TCP connections

part of #7963

log TCP connections as they are forwarded by gVisor
This commit is contained in:
Spike Curtis 2023-10-09 19:41:26 +04:00 committed by GitHub
parent 791144ddfd
commit 236e84c4d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 1 deletions

View File

@ -943,7 +943,8 @@ func (c *Conn) DialContextUDP(ctx context.Context, ipp netip.AddrPort) (*gonet.U
return c.netStack.DialContextUDP(ctx, ipp)
}
func (c *Conn) forwardTCP(_, dst netip.AddrPort) (handler func(net.Conn), opts []tcpip.SettableSocketOption, intercept bool) {
func (c *Conn) forwardTCP(src, dst netip.AddrPort) (handler func(net.Conn), opts []tcpip.SettableSocketOption, intercept bool) {
logger := c.logger.Named("tcp").With(slog.F("src", src.String()), slog.F("dst", dst.String()))
c.mutex.Lock()
ln, ok := c.listeners[listenKey{"tcp", "", fmt.Sprint(dst.Port())}]
c.mutex.Unlock()
@ -961,10 +962,14 @@ func (c *Conn) forwardTCP(_, dst netip.AddrPort) (handler func(net.Conn), opts [
defer t.Stop()
select {
case ln.conn <- conn:
logger.Info(context.Background(), "accepted connection")
return
case <-ln.closed:
logger.Info(context.Background(), "listener closed; closing connection")
case <-c.closed:
logger.Info(context.Background(), "tailnet closed; closing connection")
case <-t.C:
logger.Info(context.Background(), "listener timed out accepting; closing connection")
}
_ = conn.Close()
}, opts, true