diff --git a/demo/server/server.go b/demo/server/server.go index 1aca303..bbdd6c6 100644 --- a/demo/server/server.go +++ b/demo/server/server.go @@ -9,7 +9,6 @@ import ( "os" "os/exec" "os/user" - "strings" "syscall" hkex "blitter.com/hkexsh" @@ -27,7 +26,8 @@ type cmdSpec struct { /* -------------------------------------------------------------- */ -// Run a command (via os.exec) as a specific user +/* + // Run a command (via os.exec) as a specific user // // Uses ptys to support commands which expect a terminal. func runCmdAs(who string, cmd string, conn hkex.Conn) (err error) { @@ -66,6 +66,7 @@ func runCmdAs(who string, cmd string, conn hkex.Conn) (err error) { } return } +*/ // Run a command (via default shell) as a specific user // @@ -111,8 +112,11 @@ func runShellAs(who string, cmd string, interactive bool, conn hkex.Conn) (err e } // Make sure to close the pty at the end. defer func() { _ = ptmx.Close() }() // Best effort. - // Copy stdin to the pty and the pty to stdout. - go func() { _, _ = io.Copy(ptmx, conn) }() + // Copy stdin to the pty.. (bgnd goroutine) + go func() { + _, _ = io.Copy(ptmx, conn) + }() + // ..and the pty to stdout. _, _ = io.Copy(conn, ptmx) //err = c.Run() // returns when c finishes. @@ -176,10 +180,10 @@ func main() { //passed down to the command handlers. var rec cmdSpec var len1, len2, len3, len4 uint32 - + n, err := fmt.Fscanf(c, "%d %d %d %d\n", &len1, &len2, &len3, &len4) log.Printf("cmdSpec read:%d %d %d %d\n", len1, len2, len3, len4) - + if err != nil || n < 4 { log.Println("[Bad cmdSpec fmt]") return err diff --git a/hkexnet.go b/hkexnet.go index 4994940..61943db 100644 --- a/hkexnet.go +++ b/hkexnet.go @@ -299,10 +299,10 @@ func (hl HKExListener) Accept() (hc Conn, err error) { // See go doc io.Reader func (c Conn) Read(b []byte) (n int, err error) { //log.Printf("[Decrypting...]\r\n") - //log.Printf("Read() requests %d bytes\n", len(b)) + log.Printf("Read() requests %d bytes\n", len(b)) for { //log.Printf("c.dBuf.Len(): %d\n", c.dBuf.Len()) - if c.dBuf.Len() >= 1 /* len(b) */ { + if c.dBuf.Len() > 0 /* len(b) */ { break } @@ -311,13 +311,21 @@ 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 && err.Error() != "EOF" { - panic(err) + 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 payloadLen > 16384 { panic("Insane payloadLen") @@ -359,10 +367,15 @@ func (c Conn) Read(b []byte) (n int, err error) { hTmp := c.rm.Sum(nil)[0] log.Printf("<%04x) HMAC:(i)%02x (c)%02x\r\n", decryptN, hmacIn, hTmp) } - //log.Printf("Read() got %d bytes\n", c.dBuf.Len()) - copy(b, c.dBuf.Next(len(b))) + retN := c.dBuf.Len() + if retN > len(b) { + retN = len(b) + } + + log.Printf("Read() got %d bytes\n", retN) + copy(b, c.dBuf.Next(retN)) //log.Printf("As Read() returns, c.dBuf is %d long: %s\n", c.dBuf.Len(), hex.Dump(c.dBuf.Bytes())) - return len(b), nil + return retN, nil } // Write a byte slice