No need for custom hkexsh.Copy()

This commit is contained in:
Russ Magee 2018-05-04 23:31:06 -07:00
parent c5498642fc
commit 70448dda08
3 changed files with 3 additions and 43 deletions

View file

@ -530,42 +530,3 @@ func (c *Conn) chaffHelper(szMax int) {
} }
}() }()
} }
// hkexsh.Copy() is a modified version of io.Copy()
func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
// If the reader has a WriteTo method, use it to do the copy.
// Avoids an allocation and a copy.
if wt, ok := src.(io.WriterTo); ok {
return wt.WriteTo(dst)
}
// Similarly, if the writer has a ReadFrom method, use it to do the copy.
if rt, ok := dst.(io.ReaderFrom); ok {
return rt.ReadFrom(src)
}
buf := make([]byte, 32*1024)
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return written, err
}

View file

@ -230,7 +230,7 @@ func main() {
//!_, outerr := io.Copy(conn, os.Stdin) //!_, outerr := io.Copy(conn, os.Stdin)
conn.Chaff(true, 100, 500, 32) // enable client->server chaffing conn.Chaff(true, 100, 500, 32) // enable client->server chaffing
_, outerr := func(conn *hkexsh.Conn, r io.Reader) (w int64, e error) { _, outerr := func(conn *hkexsh.Conn, r io.Reader) (w int64, e error) {
return hkexsh.Copy(conn, r) return io.Copy(conn, r)
}(conn, os.Stdin) }(conn, os.Stdin)
if outerr != nil { if outerr != nil {

View file

@ -131,7 +131,7 @@ func runShellAs(who string, cmd string, interactive bool, conn hkexsh.Conn) (err
// Copy stdin to the pty.. (bgnd goroutine) // Copy stdin to the pty.. (bgnd goroutine)
go func() { go func() {
_, _ = hkexsh.Copy(ptmx, conn) _, _ = io.Copy(ptmx, conn)
}() }()
// ..and the pty to stdout. // ..and the pty to stdout.
@ -140,8 +140,7 @@ func runShellAs(who string, cmd string, interactive bool, conn hkexsh.Conn) (err
// --knowledge of another thread which would do chaffing. // --knowledge of another thread which would do chaffing.
// --Modify pty somehow to slave the command through hkexsh.Copy() ? // --Modify pty somehow to slave the command through hkexsh.Copy() ?
conn.Chaff(true, 100, 500, 32) conn.Chaff(true, 100, 500, 32)
_, _ = hkexsh.Copy(conn, ptmx) _, _ = io.Copy(conn, ptmx)
//_, _ = io.Copy(conn, ptmx)
//err = c.Run() // returns when c finishes. //err = c.Run() // returns when c finishes.