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

Signed-off-by: Russ Magee <rmagee@gmail.com>
This commit is contained in:
Russ Magee 2019-06-22 00:44:23 -07:00
parent 54396a4e4b
commit 3ae48addbc
2 changed files with 31 additions and 9 deletions

View File

@ -75,8 +75,9 @@ type (
c *net.Conn // which also implements io.Reader, io.Writer, ... c *net.Conn // which also implements io.Reader, io.Writer, ...
immClose bool immClose bool
logCipherText bool // somewhat expensive, for debugging logCipherText bool // somewhat expensive, for debugging
logPlainText bool // INSECURE and somewhat expensive, for debugging logPlainText bool // INSECURE and somewhat expensive, for debugging
logTunActivity bool
cipheropts uint32 // post-KEx cipher/hmac options cipheropts uint32 // post-KEx cipher/hmac options
opts uint32 // post-KEx protocol options (caller-defined) 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]) rport := binary.BigEndian.Uint16(payloadBytes[2:4])
//fmt.Printf("[Got CSOTunData: [lport %d:rport %d] data:%v\n", lport, rport, payloadBytes[4:]) //fmt.Printf("[Got CSOTunData: [lport %d:rport %d] data:%v\n", lport, rport, payloadBytes[4:])
if _, ok := (*hc.tuns)[rport]; ok { 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].Data <- payloadBytes[4:]
(*hc.tuns)[rport].KeepAlive = 0
} else { } else {
logger.LogDebug(fmt.Sprintf("[Attempt to write data to closed tun [%d:%d]", lport, rport)) 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() { go func() {
var wg sync.WaitGroup 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 { for cmd := range (*hc.tuns)[rport].Ctl {
var c net.Conn var c net.Conn
logger.LogDebug(fmt.Sprintf("[ServerTun] got Ctl '%c'.", cmd)) 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) hc.WritePacket(rBuf[:n+4], CSOTunData)
} }
if (*hc.tuns)[rport].KeepAlive > 50 { //if (*hc.tuns)[rport].KeepAlive > 50000 {
(*hc.tuns)[rport].Died = true // (*hc.tuns)[rport].Died = true
logger.LogDebug("[ServerTun] worker A: Client died, hanging up.") // logger.LogDebug("[ServerTun] worker A: Client died, hanging up.")
} else { //} else {
(*hc.tuns)[rport].KeepAlive += 1 // (*hc.tuns)[rport].KeepAlive += 1
} //}
} }
logger.LogDebug("[ServerTun] worker A: exiting") logger.LogDebug("[ServerTun] worker A: exiting")