2021-04-14 08:51:34 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-16 17:41:35 -04:00
|
|
|
"bytes"
|
2021-04-14 17:58:02 -04:00
|
|
|
"context"
|
2021-04-16 16:42:17 -04:00
|
|
|
"errors"
|
2021-04-14 08:51:34 -04:00
|
|
|
"flag"
|
2021-04-14 20:42:07 -04:00
|
|
|
"io"
|
2021-04-14 08:51:34 -04:00
|
|
|
"log"
|
|
|
|
"net"
|
2021-04-14 20:42:07 -04:00
|
|
|
"os"
|
2021-04-14 18:27:45 -04:00
|
|
|
"os/exec"
|
2021-04-14 08:51:34 -04:00
|
|
|
"strconv"
|
2021-04-14 11:25:41 -04:00
|
|
|
"strings"
|
2021-04-14 08:51:34 -04:00
|
|
|
"sync"
|
2021-04-16 17:41:35 -04:00
|
|
|
"text/template"
|
2021-04-14 08:51:34 -04:00
|
|
|
"time"
|
|
|
|
|
2021-04-14 20:42:07 -04:00
|
|
|
"github.com/creack/pty"
|
2021-04-14 08:51:34 -04:00
|
|
|
"github.com/xtaci/gaio"
|
2021-04-16 16:42:17 -04:00
|
|
|
"golang.org/x/term"
|
2021-04-16 21:30:56 -04:00
|
|
|
"versestudios.com/go-bbs/openconn"
|
2021-04-14 08:51:34 -04:00
|
|
|
)
|
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
type InChanKey string
|
|
|
|
type OutChanKey string
|
|
|
|
|
|
|
|
func ioHandler(w *gaio.Watcher, WatcherControl *chan string) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-*WatcherControl:
|
|
|
|
switch msg {
|
|
|
|
case "":
|
|
|
|
log.Println("empty WatcherControl command")
|
|
|
|
case "stop":
|
|
|
|
log.Println("stopping ioHandler via WatcherControl stop command")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
log.Println("unknown WatcherControl command:", msg)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// loop wait for any IO events
|
|
|
|
results, err := w.WaitIO()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
IOLoop:
|
|
|
|
for _, res := range results {
|
|
|
|
if res.Context != nil && nil != res.Context.(context.Context) {
|
|
|
|
if inChan, _, ok := openconn.FromContext(res.Context.(context.Context)); ok {
|
|
|
|
switch res.Operation {
|
|
|
|
case gaio.OpRead: // read completion event
|
|
|
|
inChan <- res
|
|
|
|
// queue next read
|
|
|
|
w.Read(res.Context, res.Conn, nil)
|
|
|
|
default: // anything else (meaning write completions)
|
|
|
|
continue IOLoop
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("error getting inChan and outChan from context: %v\n", res.Context)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("nil context! res: %v\n", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
func main() {
|
|
|
|
port := flag.Int("port", 3333, "Port to accept connections on.")
|
|
|
|
host := flag.String("host", "127.0.0.1", "Host or IP to bind to")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
w, err := gaio.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer w.Close()
|
|
|
|
l, err := net.Listen("tcp", *host+":"+strconv.Itoa(*port))
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
log.Println("Listening to connections at '"+*host+"' on port", strconv.Itoa(*port))
|
2021-04-16 21:34:57 -04:00
|
|
|
log.Println("(If you want to run this on a different address or port, the command line flags -host and -port are available to you.)")
|
2021-04-14 08:51:34 -04:00
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2021-04-14 11:25:41 -04:00
|
|
|
log.Println("new client: ", conn.RemoteAddr())
|
2021-04-14 17:58:02 -04:00
|
|
|
// chan to terminate ioHandler when neccessary
|
|
|
|
WatcherControl := make(chan string)
|
|
|
|
|
|
|
|
// get new context with inChan and outChan for this client connection
|
|
|
|
ctx := openconn.NewContext(context.Background())
|
|
|
|
log.Printf("new context for client %v: %v\n", conn.RemoteAddr(), ctx)
|
2021-04-14 08:51:34 -04:00
|
|
|
|
|
|
|
// submit the first async write IO request
|
2021-04-16 21:30:56 -04:00
|
|
|
welcomeScreen, err := screenHandler("welcome", nil)
|
2021-04-16 17:41:35 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("err loading initial welcome screen: %v\n", err)
|
|
|
|
}
|
|
|
|
err = w.Write(ctx, conn, welcomeScreen)
|
2021-04-14 08:51:34 -04:00
|
|
|
if err != nil {
|
2021-04-14 11:25:41 -04:00
|
|
|
log.Printf("err sending welcomeHandler: %v\n", err)
|
2021-04-14 08:51:34 -04:00
|
|
|
return
|
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
// now that a prompt is (or will be) displayed, go ahead and listen for input
|
|
|
|
err = w.Read(ctx, conn, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("err queueing w.Read: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2021-04-14 11:25:41 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
time.Sleep(time.Millisecond)
|
2021-04-14 11:25:41 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
// menu handler for this connection
|
|
|
|
go menuHandler(ctx, conn, w)
|
|
|
|
// io handler for this connection
|
|
|
|
go ioHandler(w, &WatcherControl)
|
2021-04-14 08:51:34 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
log.Println("main thread looping")
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 11:25:41 -04:00
|
|
|
}
|
2021-04-14 08:51:34 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
MenuControl := make(chan string)
|
2021-04-14 08:51:34 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
var inChan, outChan chan gaio.OpResult
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if inChan, outChan, ok = openconn.FromContext(ctx); !ok {
|
|
|
|
log.Println("Could not get inChan/outChan from context!", ok, ctx)
|
|
|
|
}
|
2021-04-14 11:25:41 -04:00
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
wg.Add(1)
|
2021-04-14 17:58:02 -04:00
|
|
|
go func(inChan, outChan chan gaio.OpResult, MenuControl chan string) {
|
2021-04-14 08:51:34 -04:00
|
|
|
defer wg.Done()
|
2021-04-14 17:58:02 -04:00
|
|
|
log.Println("starting menu loop")
|
|
|
|
paused := false
|
2021-04-14 11:25:41 -04:00
|
|
|
ControlLoop:
|
|
|
|
for {
|
|
|
|
select {
|
2021-04-14 17:58:02 -04:00
|
|
|
case msg := <-MenuControl:
|
|
|
|
log.Println("msg on MenuControl:", msg)
|
|
|
|
switch string(msg) {
|
|
|
|
|
|
|
|
case "stop":
|
|
|
|
log.Println("closing menu loop")
|
|
|
|
paused = true
|
2021-04-14 11:25:41 -04:00
|
|
|
break ControlLoop
|
2021-04-14 17:58:02 -04:00
|
|
|
|
|
|
|
case "pause":
|
|
|
|
log.Println("pausing menu loop via menucontrol")
|
|
|
|
paused = true
|
|
|
|
|
|
|
|
case "unpause":
|
|
|
|
log.Println("unpausing menu loop via menucontrol")
|
|
|
|
paused = false
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Println("menu received unknown command: ", msg)
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
default:
|
2021-04-14 17:58:02 -04:00
|
|
|
if !paused {
|
|
|
|
select {
|
|
|
|
case res := <-inChan:
|
|
|
|
if res.Error != nil {
|
|
|
|
log.Println("error on inChan: ", res.Error)
|
|
|
|
}
|
|
|
|
if res.Operation == gaio.OpRead && res.Size > 0 && res.Conn == conn {
|
|
|
|
log.Printf("received on inChan: conn: %v, buffer size: %v\n", res.Conn.RemoteAddr(), res.Size)
|
|
|
|
log.Println("menu receive: ", strings.TrimSpace(string(res.Buffer[:res.Size-2])))
|
|
|
|
switch string(strings.TrimSpace(string(res.Buffer[:res.Size-2]))) {
|
2021-04-14 11:25:41 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
case "welcome":
|
2021-04-16 21:30:56 -04:00
|
|
|
welcomeStr, err := screenHandler("welcome", nil)
|
2021-04-16 17:41:35 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("err loading welcome screen: %v\n", err)
|
|
|
|
}
|
|
|
|
if err := w.Write(ctx, conn, welcomeStr); err != nil {
|
2021-04-16 19:04:50 -04:00
|
|
|
log.Printf("error sending screenHandler from cmd `welcome`: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "help":
|
2021-04-16 21:30:56 -04:00
|
|
|
helpStr, err := screenHandler("help", nil)
|
2021-04-16 19:04:50 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("err loading help screen: %v\n", err)
|
|
|
|
}
|
|
|
|
if err := w.Write(ctx, conn, helpStr); err != nil {
|
|
|
|
log.Printf("error sending screenHandler from cmd `help`: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "operator":
|
2021-04-16 21:30:56 -04:00
|
|
|
opStr, err := screenHandler("operator", nil)
|
2021-04-16 19:04:50 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("err loading operator screen: %v\n", err)
|
|
|
|
}
|
|
|
|
var lineNum int
|
|
|
|
var line string
|
|
|
|
baudrate := 40 // 300 baud is ~ 30 characters per second; I'll be nice and bump it to 400
|
|
|
|
for lineNum, line = range strings.Split(string(opStr), "\n") {
|
|
|
|
// changed my mind - I actually like it at a nice steady 400 baud, tyvm
|
|
|
|
// if baudrate < 120 && i > 1 { // 1200 baud is still very readable in realtime without being too fast
|
|
|
|
// baudrate += 1
|
|
|
|
//}
|
|
|
|
if lineNum < 10 { // spit the banner text out quickly
|
|
|
|
if err := w.Write(ctx, conn, []byte(line+"\r\n")); err != nil {
|
|
|
|
log.Printf("error sending rune from cmd `operator`: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ms := float64(1) / float64(baudrate)
|
|
|
|
for _, c := range line {
|
|
|
|
if err := w.Write(ctx, conn, []byte{byte(c)}); err != nil {
|
|
|
|
log.Printf("error sending rune from cmd `operator`: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(ms*1000) * time.Millisecond)
|
|
|
|
}
|
|
|
|
if err := w.Write(ctx, conn, []byte("\r\n")); err != nil {
|
|
|
|
log.Printf("error sending rune from cmd `operator`: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add prompt at end
|
|
|
|
if err := w.Write(ctx, conn, []byte("\r\n> ")); err != nil {
|
|
|
|
log.Printf("error sending rune from cmd `operator`: %v", err)
|
2021-04-14 11:25:41 -04:00
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
|
|
|
|
case "adventure":
|
|
|
|
// start the door and wait
|
2021-04-14 18:27:45 -04:00
|
|
|
var wg sync.WaitGroup
|
2021-04-14 21:04:17 -04:00
|
|
|
log.Println("starting door handler...")
|
2021-04-14 18:27:45 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go doorHandler(ctx, conn, w, &wg)
|
|
|
|
log.Println("menu handler waiting for wg to return from door")
|
|
|
|
wg.Wait()
|
|
|
|
log.Println("returning from door")
|
2021-04-14 21:04:17 -04:00
|
|
|
// welcome back and prompt
|
|
|
|
err := w.Write(ctx, conn, []byte("Welcome back from your adventure!\n\n> "))
|
2021-04-14 11:25:41 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error writing to connection: %v", err)
|
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
|
2021-04-16 21:30:56 -04:00
|
|
|
case "info":
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(2)
|
|
|
|
uptime := getUptime(wg)
|
|
|
|
tubes := getTubePeers(wg)
|
|
|
|
wg.Wait()
|
|
|
|
data := struct{ Uptime, TubesInfo string }{uptime, tubes}
|
|
|
|
infoStr, err := screenHandler("info", data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("err loading info screen: %v\n", err)
|
|
|
|
}
|
|
|
|
if err := w.Write(ctx, conn, infoStr); err != nil {
|
|
|
|
log.Printf("error sending screenHandler from cmd `info`: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
case "exit":
|
2021-04-16 21:30:56 -04:00
|
|
|
exitStr, err := screenHandler("exit", nil)
|
2021-04-16 17:41:35 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("err loading exit screen: %v\n", err)
|
2021-04-14 17:58:02 -04:00
|
|
|
}
|
2021-04-16 17:41:35 -04:00
|
|
|
if err := w.Write(ctx, conn, exitStr); err != nil {
|
|
|
|
log.Printf("error sending exitHandler from cmd `exit`: %v", err)
|
|
|
|
}
|
|
|
|
// wait a second before really closing the connection
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
w.Free(conn)
|
2021-04-14 17:58:02 -04:00
|
|
|
|
2021-04-16 17:41:35 -04:00
|
|
|
default:
|
2021-04-14 17:58:02 -04:00
|
|
|
err := w.Write(ctx, conn, []byte("huh?\n\n> "))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error writing to connection: %v", err)
|
2021-04-14 11:25:41 -04:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// noop
|
|
|
|
log.Printf("received on inChan: conn: %v, buffer: %v\n", res.Conn.RemoteAddr(), string(res.Buffer))
|
2021-04-14 11:25:41 -04:00
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
default:
|
|
|
|
// noop - wait a lil bit
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
log.Println("conn is nil!")
|
2021-04-14 11:25:41 -04:00
|
|
|
}
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
time.Sleep(time.Millisecond)
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 17:58:02 -04:00
|
|
|
log.Println("menu controlloop closed!")
|
|
|
|
}(inChan, outChan, MenuControl)
|
2021-04-14 08:51:34 -04:00
|
|
|
|
2021-04-14 17:58:02 -04:00
|
|
|
log.Println("menu waiting on waitgroup")
|
|
|
|
wg.Wait()
|
|
|
|
log.Println("terminating menucontrol for client")
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
|
|
|
|
2021-04-16 21:30:56 -04:00
|
|
|
func screenHandler(screenName string, tplData interface{}) (screen []byte, err error) {
|
2021-04-16 17:41:35 -04:00
|
|
|
var screenBuf *bytes.Buffer
|
|
|
|
if tmpl, err := template.ParseFiles("screens/" + screenName + ".ans"); err != nil {
|
|
|
|
log.Printf("err loading welcome template: %v\n", err)
|
|
|
|
} else {
|
|
|
|
screenBuf = new(bytes.Buffer)
|
2021-04-16 21:30:56 -04:00
|
|
|
tmpl.Execute(screenBuf, tplData)
|
2021-04-16 17:41:35 -04:00
|
|
|
}
|
2021-04-14 08:51:34 -04:00
|
|
|
|
2021-04-16 17:41:35 -04:00
|
|
|
return screenBuf.Bytes(), err
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
|
|
|
|
2021-04-14 18:27:45 -04:00
|
|
|
func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync.WaitGroup) error {
|
|
|
|
defer menuwg.Done()
|
|
|
|
|
2021-04-14 20:42:07 -04:00
|
|
|
var wg sync.WaitGroup
|
2021-04-14 08:51:34 -04:00
|
|
|
|
2021-04-14 18:27:45 -04:00
|
|
|
var inChan, _ chan gaio.OpResult
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if inChan, _, ok = openconn.FromContext(ctx); !ok {
|
|
|
|
log.Println("Could not get inChan/outChan from context!", ok, ctx)
|
|
|
|
}
|
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
cmd := exec.Command("/usr/games/adventure")
|
|
|
|
|
2021-04-14 20:42:07 -04:00
|
|
|
ptmx, err := pty.Start(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Make sure to close the pty at the end.
|
|
|
|
defer func() { _ = ptmx.Close() }() // Best effort.
|
|
|
|
|
2021-04-16 16:42:17 -04:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-04-14 20:42:07 -04:00
|
|
|
Terminator := make(chan bool)
|
2021-04-14 18:27:45 -04:00
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
wg.Add(1)
|
2021-04-14 20:42:07 -04:00
|
|
|
go func() {
|
2021-04-14 08:51:34 -04:00
|
|
|
defer wg.Done()
|
2021-04-14 20:42:07 -04:00
|
|
|
waitingForInput := false
|
|
|
|
IOLoop:
|
2021-04-14 08:51:34 -04:00
|
|
|
for {
|
|
|
|
select {
|
2021-04-14 18:27:45 -04:00
|
|
|
case result, ok := <-inChan:
|
2021-04-14 20:42:07 -04:00
|
|
|
var l int
|
|
|
|
log.Println("i")
|
2021-04-14 18:27:45 -04:00
|
|
|
if ok {
|
2021-04-14 20:42:07 -04:00
|
|
|
if result.Operation == gaio.OpRead && result.Size > 0 {
|
|
|
|
l, err = ptmx.Write(result.Buffer[:result.Size])
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
log.Println("EOF on cmd.write!!!")
|
|
|
|
break IOLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if l > 0 {
|
|
|
|
log.Printf("wrote %d bytes to cmd stdin: %v\n", l, string(result.Buffer[:result.Size]))
|
|
|
|
waitingForInput = false
|
|
|
|
}
|
|
|
|
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 18:27:45 -04:00
|
|
|
} else {
|
|
|
|
log.Println("yikes, problem getting a result from inChan in doorHandler!")
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 20:42:07 -04:00
|
|
|
case <-Terminator:
|
|
|
|
log.Println("Terminator'd!!!")
|
|
|
|
break IOLoop
|
2021-04-14 18:27:45 -04:00
|
|
|
default:
|
2021-04-14 20:42:07 -04:00
|
|
|
if !waitingForInput {
|
|
|
|
log.Println("o")
|
2021-04-16 16:42:17 -04:00
|
|
|
ptmx.SetReadDeadline(time.Now().Add(1 * time.Second))
|
2021-04-14 20:42:07 -04:00
|
|
|
var l int
|
2021-04-16 16:42:17 -04:00
|
|
|
outBuf := make([]byte, 4096)
|
2021-04-14 20:42:07 -04:00
|
|
|
if l, err = ptmx.Read(outBuf); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
log.Println("EOF on cmd stdout - outta here")
|
|
|
|
break IOLoop
|
2021-04-16 16:42:17 -04:00
|
|
|
} 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
|
2021-04-14 20:42:07 -04:00
|
|
|
break IOLoop
|
|
|
|
} else {
|
|
|
|
log.Printf("err reading from cmd term: %v\n", err)
|
|
|
|
break IOLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if l > 0 {
|
|
|
|
w.Write(ctx, c, outBuf[:l])
|
|
|
|
log.Printf("wrote %d bytes to watcher: %v\n", l, string(outBuf[:l]))
|
2021-04-16 17:48:56 -04:00
|
|
|
if strings.Contains(string(outBuf[:l]), ">") {
|
|
|
|
// a prompt appeared!
|
|
|
|
waitingForInput = true
|
|
|
|
}
|
2021-04-14 20:42:07 -04:00
|
|
|
} else {
|
|
|
|
// zero output? let's get some input?
|
2021-04-16 17:48:56 -04:00
|
|
|
// waitingForInput = true
|
2021-04-14 20:42:07 -04:00
|
|
|
w.Read(ctx, c, nil)
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 20:42:07 -04:00
|
|
|
time.Sleep(time.Millisecond)
|
2021-04-14 08:51:34 -04:00
|
|
|
}
|
2021-04-14 20:42:07 -04:00
|
|
|
}()
|
2021-04-14 08:51:34 -04:00
|
|
|
wg.Wait()
|
|
|
|
log.Println("waiting for cmd")
|
|
|
|
exitErr := cmd.Wait()
|
|
|
|
if exitErr != nil {
|
|
|
|
log.Printf("cmd exited with err: %v\n", exitErr)
|
|
|
|
}
|
2021-04-14 18:27:45 -04:00
|
|
|
log.Println("leaving door handler")
|
2021-04-14 08:51:34 -04:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-16 21:30:56 -04:00
|
|
|
|
|
|
|
// returns output of `uptime` as a string
|
|
|
|
func getUptime(wg *sync.WaitGroup) string {
|
|
|
|
defer wg.Done()
|
|
|
|
out, err := exec.Command("uptime").Output()
|
|
|
|
if err != nil {
|
|
|
|
return string(err.Error()) // might as well pass on the love to my future co-workers for debugging assistance
|
|
|
|
}
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTubePeers(wg *sync.WaitGroup) string {
|
|
|
|
defer wg.Done()
|
|
|
|
out, err := exec.Command("/usr/local/bin/getTubesPeers.bash").Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("err loading getTubePeers: %v\n", err)
|
|
|
|
return string(err.Error()) // again, might as well pass on the love to my future co-workers for debugging assistance
|
|
|
|
}
|
|
|
|
return string(out)
|
|
|
|
}
|