From 1d4c16dd766bdec3dd4a3a8b78a7f8a3f51c1433 Mon Sep 17 00:00:00 2001 From: Russ Magee Date: Sat, 22 Jun 2019 00:44:23 -0700 Subject: [PATCH] Moved server keepalive ageing for tunnels to goroutine -- fix for premature tunnel collapses --- hkexnet/hkexnet.go | 10 +++++++--- hkexnet/hkextun.go | 30 ++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/hkexnet/hkexnet.go b/hkexnet/hkexnet.go index 66c2adb..21da20f 100644 --- a/hkexnet/hkexnet.go +++ b/hkexnet/hkexnet.go @@ -75,8 +75,9 @@ type ( c *net.Conn // which also implements io.Reader, io.Writer, ... immClose bool - logCipherText bool // somewhat expensive, for debugging - logPlainText bool // INSECURE and somewhat expensive, for debugging + logCipherText bool // somewhat expensive, for debugging + logPlainText bool // INSECURE and somewhat expensive, for debugging + logTunActivity bool cipheropts uint32 // post-KEx cipher/hmac options opts uint32 // post-KEx protocol options (caller-defined) @@ -1112,8 +1113,11 @@ func (hc Conn) Read(b []byte) (n int, err error) { rport := binary.BigEndian.Uint16(payloadBytes[2:4]) //fmt.Printf("[Got CSOTunData: [lport %d:rport %d] data:%v\n", lport, rport, payloadBytes[4:]) if _, ok := (*hc.tuns)[rport]; ok { - logger.LogDebug(fmt.Sprintf("[Writing data to rport [%d:%d]", lport, rport)) + if hc.logTunActivity { + logger.LogDebug(fmt.Sprintf("[Writing data to rport [%d:%d]", lport, rport)) + } (*hc.tuns)[rport].Data <- payloadBytes[4:] + (*hc.tuns)[rport].KeepAlive = 0 } else { logger.LogDebug(fmt.Sprintf("[Attempt to write data to closed tun [%d:%d]", lport, rport)) } diff --git a/hkexnet/hkextun.go b/hkexnet/hkextun.go index da22882..6503c3f 100644 --- a/hkexnet/hkextun.go +++ b/hkexnet/hkextun.go @@ -244,6 +244,24 @@ func (hc *Conn) StartServerTunnel(lport, rport uint16) { go func() { var wg sync.WaitGroup + // + // worker to age server tunnel and kill it if keepalives + // stop from client + // + wg.Add(1) + go func() { + defer wg.Done() + for { + time.Sleep(100 * time.Millisecond) + (*hc.tuns)[rport].KeepAlive += 1 + if (*hc.tuns)[rport].KeepAlive > 25 { + (*hc.tuns)[rport].Died = true + logger.LogDebug("[ServerTun] worker A: Client died, hanging up.") + break + } + } + }() + for cmd := range (*hc.tuns)[rport].Ctl { var c net.Conn logger.LogDebug(fmt.Sprintf("[ServerTun] got Ctl '%c'.", cmd)) @@ -330,12 +348,12 @@ func (hc *Conn) StartServerTunnel(lport, rport uint16) { hc.WritePacket(rBuf[:n+4], CSOTunData) } - if (*hc.tuns)[rport].KeepAlive > 50 { - (*hc.tuns)[rport].Died = true - logger.LogDebug("[ServerTun] worker A: Client died, hanging up.") - } else { - (*hc.tuns)[rport].KeepAlive += 1 - } + //if (*hc.tuns)[rport].KeepAlive > 50000 { + // (*hc.tuns)[rport].Died = true + // logger.LogDebug("[ServerTun] worker A: Client died, hanging up.") + //} else { + // (*hc.tuns)[rport].KeepAlive += 1 + //} } logger.LogDebug("[ServerTun] worker A: exiting")