From 730b6c34096b222dbc9fe2e43bbb838558cd47ce Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 14 Apr 2022 23:06:24 -0400 Subject: [PATCH] chore: add ssh example --- {_example => _examples/local}/main.go | 0 _examples/ssh/main.go | 40 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) rename {_example => _examples/local}/main.go (100%) create mode 100644 _examples/ssh/main.go diff --git a/_example/main.go b/_examples/local/main.go similarity index 100% rename from _example/main.go rename to _examples/local/main.go diff --git a/_examples/ssh/main.go b/_examples/ssh/main.go new file mode 100644 index 0000000..13456ae --- /dev/null +++ b/_examples/ssh/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "log" + + "github.com/aymanbagabas/go-osc52" + "github.com/charmbracelet/wish" + "github.com/gliderlabs/ssh" +) + +func main() { + s, err := wish.NewServer( + wish.WithAddress(":2222"), + wish.WithHostKeyPath("ssh_host_key"), + wish.WithMiddleware( + middleware(), + ), + ) + if err != nil { + log.Fatal(err) + } + fmt.Printf("SSH into %s\n", s.Addr) + s.ListenAndServe() +} + +func middleware() wish.Middleware { + return func(h ssh.Handler) ssh.Handler { + return func(s ssh.Session) { + environ := s.Environ() + pty, _, _ := s.Pty() + // Put TERM environment variable into environ. + environ = append(environ, fmt.Sprintf("TERM=%s", pty.Term)) + out := osc52.NewOutput(s, environ) + str := "hello world" + out.Copy(str) + s.Write([]byte(fmt.Sprintf("Copied %q!\n", str))) + } + } +}