add gitignore, add first pass at info screen handler

This commit is contained in:
Sundog Jones 2021-04-16 18:30:56 -07:00
parent fb4aa62f16
commit 5342c7933c
4 changed files with 79 additions and 9 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
go-bbs
getTubesPeers.bash

2
go.mod
View File

@ -1,4 +1,4 @@
module versestudios.com/go-telnet-asyncio-test module versestudios.com/go-bbs
go 1.16 go 1.16

51
main.go
View File

@ -19,7 +19,7 @@ import (
"github.com/creack/pty" "github.com/creack/pty"
"github.com/xtaci/gaio" "github.com/xtaci/gaio"
"golang.org/x/term" "golang.org/x/term"
"versestudios.com/go-telnet-asyncio-test/openconn" "versestudios.com/go-bbs/openconn"
) )
type InChanKey string type InChanKey string
@ -104,7 +104,7 @@ func main() {
log.Printf("new context for client %v: %v\n", conn.RemoteAddr(), ctx) log.Printf("new context for client %v: %v\n", conn.RemoteAddr(), ctx)
// submit the first async write IO request // submit the first async write IO request
welcomeScreen, err := screenHandler("welcome") welcomeScreen, err := screenHandler("welcome", nil)
if err != nil { if err != nil {
log.Printf("err loading initial welcome screen: %v\n", err) 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]))) { switch string(strings.TrimSpace(string(res.Buffer[:res.Size-2]))) {
case "welcome": case "welcome":
welcomeStr, err := screenHandler("welcome") welcomeStr, err := screenHandler("welcome", nil)
if err != nil { if err != nil {
log.Printf("err loading welcome screen: %v\n", err) 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": case "help":
helpStr, err := screenHandler("help") helpStr, err := screenHandler("help", nil)
if err != nil { if err != nil {
log.Printf("err loading help screen: %v\n", err) 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": case "operator":
opStr, err := screenHandler("operator") opStr, err := screenHandler("operator", nil)
if err != nil { if err != nil {
log.Printf("err loading operator screen: %v\n", err) 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) 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": case "exit":
exitStr, err := screenHandler("exit") exitStr, err := screenHandler("exit", nil)
if err != nil { if err != nil {
log.Printf("err loading exit screen: %v\n", err) 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") 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 var screenBuf *bytes.Buffer
if tmpl, err := template.ParseFiles("screens/" + screenName + ".ans"); err != nil { if tmpl, err := template.ParseFiles("screens/" + screenName + ".ans"); err != nil {
log.Printf("err loading welcome template: %v\n", err) log.Printf("err loading welcome template: %v\n", err)
} else { } else {
screenBuf = new(bytes.Buffer) screenBuf = new(bytes.Buffer)
tmpl.Execute(screenBuf, nil) tmpl.Execute(screenBuf, tplData)
} }
return screenBuf.Bytes(), err 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") log.Println("leaving door handler")
return nil 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)
}

View File

@ -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 }}

>