clean up door i/o a bit

This commit is contained in:
Sundog Jones 2021-04-16 13:42:17 -07:00
parent 292e2a95da
commit 10a3c0a4e9
3 changed files with 27 additions and 13 deletions

5
go.mod
View File

@ -3,7 +3,8 @@ module versestudios.com/go-telnet-asyncio-test
go 1.16 go 1.16
require ( require (
github.com/creack/pty v1.1.11 // indirect github.com/creack/pty v1.1.11
github.com/xtaci/gaio v1.2.9 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
) )

5
go.sum
View File

@ -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/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 h1:EuVc7Q2JDzIY2mk5mjtq4K5BgTuO+kj5LXzCwjOK+mo=
github.com/xtaci/gaio v1.2.9/go.mod h1:rJMerwiLCLnKa14YTM/sRggTPrnBZrlCg9U3DnV5VBE= 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 h1:g9s1Ppvvun/fI+BptTMj909BBIcGrzQ32k9FNlcevOE=
golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 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=

30
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"errors"
"flag" "flag"
"io" "io"
"log" "log"
@ -15,6 +16,7 @@ import (
"github.com/creack/pty" "github.com/creack/pty"
"github.com/xtaci/gaio" "github.com/xtaci/gaio"
"golang.org/x/term"
"versestudios.com/go-telnet-asyncio-test/openconn" "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. // Make sure to close the pty at the end.
defer func() { _ = ptmx.Close() }() // Best effort. 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) Terminator := make(chan bool)
wg.Add(1) wg.Add(1)
@ -348,17 +357,19 @@ func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync.
default: default:
if !waitingForInput { if !waitingForInput {
log.Println("o") log.Println("o")
ptmx.SetReadDeadline(time.Now().Add(1 * time.Second))
var l int var l int
outBuf := make([]byte, 2048) outBuf := make([]byte, 4096)
ptmx.SetReadDeadline(time.Now().Add(time.Millisecond))
if l, err = ptmx.Read(outBuf); err != nil { if l, err = ptmx.Read(outBuf); err != nil {
if err == io.EOF { if err == io.EOF {
log.Println("EOF on cmd stdout - outta here") log.Println("EOF on cmd stdout - outta here")
break IOLoop break IOLoop
} else if err == os.ErrDeadlineExceeded { } else if errors.Is(err, os.ErrDeadlineExceeded) {
// do nothing but be quiet about it ffs // output deadline exceeded - get some input instead
} else if strings.TrimSpace(err.Error()) == "input/output error" { w.Read(ctx, c, nil)
// program exited methinks - get outta here waitingForInput = true
} else if errors.Is(err, os.ErrClosed) {
// program exited - get outta here
break IOLoop break IOLoop
} else { } else {
log.Printf("err reading from cmd term: %v\n", err) 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 { if l > 0 {
w.Write(ctx, c, outBuf[:l]) w.Write(ctx, c, outBuf[:l])
log.Printf("wrote %d bytes to watcher: %v\n", l, string(outBuf[:l])) log.Printf("wrote %d bytes to watcher: %v\n", l, string(outBuf[:l]))
if outBuf[l-1] == byte('>') { waitingForInput = true
// looks like a prompt to me, I hope!
// let's get some input about it!
waitingForInput = true
}
} else { } else {
// zero output? let's get some input? // zero output? let's get some input?
waitingForInput = true
w.Read(ctx, c, nil) w.Read(ctx, c, nil)
} }
} }