From 10a3c0a4e9383251e028da39344183aa3e5e7f6f Mon Sep 17 00:00:00 2001 From: Sundog Date: Fri, 16 Apr 2021 13:42:17 -0700 Subject: [PATCH] clean up door i/o a bit --- go.mod | 5 +++-- go.sum | 5 +++++ main.go | 30 +++++++++++++++++++----------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index a130012..34e591c 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module versestudios.com/go-telnet-asyncio-test go 1.16 require ( - github.com/creack/pty v1.1.11 // indirect + github.com/creack/pty v1.1.11 github.com/xtaci/gaio v1.2.9 - golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0 // indirect + golang.org/x/sys v0.0.0-20210415045647-66c3f260301c // indirect + golang.org/x/term v0.0.0-20210406210042-72f3dc4e9b72 // indirect ) diff --git a/go.sum b/go.sum index 61c4902..3ddc822 100644 --- a/go.sum +++ b/go.sum @@ -2,5 +2,10 @@ github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw= github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/xtaci/gaio v1.2.9 h1:EuVc7Q2JDzIY2mk5mjtq4K5BgTuO+kj5LXzCwjOK+mo= github.com/xtaci/gaio v1.2.9/go.mod h1:rJMerwiLCLnKa14YTM/sRggTPrnBZrlCg9U3DnV5VBE= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0 h1:g9s1Ppvvun/fI+BptTMj909BBIcGrzQ32k9FNlcevOE= golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210415045647-66c3f260301c h1:6L+uOeS3OQt/f4eFHXZcTxeZrGCuz+CLElgEBjbcTA4= +golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20210406210042-72f3dc4e9b72 h1:VqE9gduFZ4dbR7XoL77lHFp0/DyDUBKSXK7CMFkVcV0= +golang.org/x/term v0.0.0-20210406210042-72f3dc4e9b72/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/main.go b/main.go index c25c71c..2d47a6e 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "flag" "io" "log" @@ -15,6 +16,7 @@ import ( "github.com/creack/pty" "github.com/xtaci/gaio" + "golang.org/x/term" "versestudios.com/go-telnet-asyncio-test/openconn" ) @@ -312,6 +314,13 @@ func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync. // Make sure to close the pty at the end. defer func() { _ = ptmx.Close() }() // Best effort. + // Can I do this? + if term.IsTerminal(int(ptmx.Fd())) { + log.Println("Making cmd terminal raw") + oldState, _ := term.MakeRaw(int(ptmx.Fd())) + defer term.Restore(int(ptmx.Fd()), oldState) + } + Terminator := make(chan bool) wg.Add(1) @@ -348,17 +357,19 @@ func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync. default: if !waitingForInput { log.Println("o") + ptmx.SetReadDeadline(time.Now().Add(1 * time.Second)) var l int - outBuf := make([]byte, 2048) - ptmx.SetReadDeadline(time.Now().Add(time.Millisecond)) + outBuf := make([]byte, 4096) if l, err = ptmx.Read(outBuf); err != nil { if err == io.EOF { log.Println("EOF on cmd stdout - outta here") break IOLoop - } else if err == os.ErrDeadlineExceeded { - // do nothing but be quiet about it ffs - } else if strings.TrimSpace(err.Error()) == "input/output error" { - // program exited methinks - get outta here + } else if errors.Is(err, os.ErrDeadlineExceeded) { + // output deadline exceeded - get some input instead + w.Read(ctx, c, nil) + waitingForInput = true + } else if errors.Is(err, os.ErrClosed) { + // program exited - get outta here break IOLoop } else { log.Printf("err reading from cmd term: %v\n", err) @@ -368,13 +379,10 @@ func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync. if l > 0 { w.Write(ctx, c, outBuf[:l]) log.Printf("wrote %d bytes to watcher: %v\n", l, string(outBuf[:l])) - if outBuf[l-1] == byte('>') { - // looks like a prompt to me, I hope! - // let's get some input about it! - waitingForInput = true - } + waitingForInput = true } else { // zero output? let's get some input? + waitingForInput = true w.Read(ctx, c, nil) } }