From 5342c7933caa2d860533319d6bbd6dab5531b255 Mon Sep 17 00:00:00 2001 From: Sundog Date: Fri, 16 Apr 2021 18:30:56 -0700 Subject: [PATCH] add gitignore, add first pass at info screen handler --- .gitignore | 3 +++ go.mod | 2 +- main.go | 51 ++++++++++++++++++++++++++++++++++++++++-------- screens/info.ans | 32 ++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c661f55 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +go-bbs +getTubesPeers.bash + diff --git a/go.mod b/go.mod index 791026b..e15389b 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module versestudios.com/go-telnet-asyncio-test +module versestudios.com/go-bbs go 1.16 diff --git a/main.go b/main.go index 1cde3e0..68186c4 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ import ( "github.com/creack/pty" "github.com/xtaci/gaio" "golang.org/x/term" - "versestudios.com/go-telnet-asyncio-test/openconn" + "versestudios.com/go-bbs/openconn" ) type InChanKey string @@ -104,7 +104,7 @@ func main() { log.Printf("new context for client %v: %v\n", conn.RemoteAddr(), ctx) // submit the first async write IO request - welcomeScreen, err := screenHandler("welcome") + welcomeScreen, err := screenHandler("welcome", nil) if err != nil { log.Printf("err loading initial welcome screen: %v\n", err) } @@ -185,7 +185,7 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) { switch string(strings.TrimSpace(string(res.Buffer[:res.Size-2]))) { case "welcome": - welcomeStr, err := screenHandler("welcome") + welcomeStr, err := screenHandler("welcome", nil) if err != nil { log.Printf("err loading welcome screen: %v\n", err) } @@ -194,7 +194,7 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) { } case "help": - helpStr, err := screenHandler("help") + helpStr, err := screenHandler("help", nil) if err != nil { log.Printf("err loading help screen: %v\n", err) } @@ -203,7 +203,7 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) { } case "operator": - opStr, err := screenHandler("operator") + opStr, err := screenHandler("operator", nil) if err != nil { log.Printf("err loading operator screen: %v\n", err) } @@ -253,8 +253,23 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) { log.Printf("error writing to connection: %v", err) } + 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) + } + case "exit": - exitStr, err := screenHandler("exit") + exitStr, err := screenHandler("exit", nil) if err != nil { log.Printf("err loading exit screen: %v\n", err) } @@ -295,13 +310,13 @@ func menuHandler(ctx context.Context, conn net.Conn, w *gaio.Watcher) { log.Println("terminating menucontrol for client") } -func screenHandler(screenName string) (screen []byte, err error) { +func screenHandler(screenName string, tplData interface{}) (screen []byte, err error) { 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) - tmpl.Execute(screenBuf, nil) + tmpl.Execute(screenBuf, tplData) } return screenBuf.Bytes(), err @@ -416,3 +431,23 @@ func doorHandler(ctx context.Context, c net.Conn, w *gaio.Watcher, menuwg *sync. log.Println("leaving door handler") return nil } + +// 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) +} diff --git a/screens/info.ans b/screens/info.ans index e69de29..c140d03 100644 --- a/screens/info.ans +++ b/screens/info.ans @@ -0,0 +1,32 @@ + + + _____ __ ____ ____  + / ___/__ _______/ /____ ____ ___ / _/___ / __/___  + \__ \/ / / / ___/ __/ _ \/ __ '__ \ / // __ \/ /_/ __ \  + ___/ / /_/ (__ ) /_/ __/ / / / / / _/ // / / / __/ /_/ /  +/____/\__, /____/\__/\___/_/ /_/ /_/ /___/_/ /_/_/ \____/  + /____/ ` + +` + +Version: Saturday morning + +This bulletin board system is written in Go leveraging the gaio async i/o library. +It is hosted on a  Raspberry Pi 4 running Ubuntu 20.04 +The web-based ANSI terminal is provided by  rtty and  rttys +ASCII art banners generated at  https://www.ascii-art-generator.org/ +ANSI escape codes from  https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 + +Current system uptime is  {{ .Uptime }} + +This system is also hosting multiple Wireguard peer connections with other operators in +an experimental distributed mesh overlay network we call  the Tubes + +Current peer tunnels + +iface peer public key bytes sent bytes rec'd + +{{ .TubesInfo }} + + +> \ No newline at end of file