From cb0ce956b9c3b6e274aa6154ad55010f2b098b09 Mon Sep 17 00:00:00 2001 From: Russ Magee Date: Sun, 25 Mar 2018 23:00:37 -0700 Subject: [PATCH] Server-side client hangup working; TODO - client-side handling of post-exit EOF (broken pipe) --- hkexnet.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/hkexnet.go b/hkexnet.go index 61943db..b74407e 100644 --- a/hkexnet.go +++ b/hkexnet.go @@ -300,6 +300,7 @@ func (hl HKExListener) Accept() (hc Conn, err error) { func (c Conn) Read(b []byte) (n int, err error) { //log.Printf("[Decrypting...]\r\n") log.Printf("Read() requests %d bytes\n", len(b)) + for { //log.Printf("c.dBuf.Len(): %d\n", c.dBuf.Len()) if c.dBuf.Len() > 0 /* len(b) */ { @@ -311,21 +312,32 @@ func (c Conn) Read(b []byte) (n int, err error) { // Read the hmac LSB and payload len first err = binary.Read(c.c, binary.BigEndian, &hmacIn) - if err != nil { - if err.Error() != "EOF" { - log.Println("Error was:", err.Error()) + // Normal client 'exit' from interactive session will cause + // (on server side) err.Error() == ": use of closed network connection" + if err != nil && err.Error() != "EOF" { + if !strings.HasSuffix(err.Error(), "use of closed network connection") { + log.Println("unexpected Read() err:", err) } else { - return 0, err + log.Println("[Client hung up]") + return 0, io.EOF } } + //if err != nil { + // if err.Error() != "EOF" { + // log.Println("Error was:", err.Error()) + // } else { + // return 0, err + // } + //} + err = binary.Read(c.c, binary.BigEndian, &payloadLen) if err != nil { - // if err.Error() != "EOF" { - panic(err) - // } else { - // return 0, err - // } + if err.Error() != "EOF" { + panic(err) + } // else { + // return 0, err + //} } if payloadLen > 16384 { panic("Insane payloadLen")