Moved server keepalive ageing for tunnels to goroutine -- fix for premature tunnel collapses

This commit is contained in:
Russ Magee 2019-06-22 00:44:23 -07:00
parent 54396a4e4b
commit 1d4c16dd76
2 changed files with 31 additions and 9 deletions

View File

@ -77,6 +77,7 @@ type (
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 {
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))
}

View File

@ -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")