-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (52 loc) · 1.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/aymanbagabas/nyancatsh/bubble"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
lm "github.com/charmbracelet/wish/logging"
)
var port = flag.Int("port", 2226, "port to listen on")
func main() {
flag.Parse()
s, err := wish.NewServer(
wish.WithAddress(fmt.Sprintf("0.0.0.0:%d", *port)),
wish.WithHostKeyPath(".ssh/nyancatsh"),
wish.WithMiddleware(
bm.Middleware(teaHandler),
lm.Middleware(),
),
)
if err != nil {
log.Fatalln(err)
}
log.Printf("Starting SSH server on 0.0.0.0:%d", *port)
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
if err = s.ListenAndServe(); err != nil {
log.Fatalln(err)
}
}()
<-done
if err := s.Close(); err != nil {
log.Fatalln(err)
}
}
func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
pty, _, active := s.Pty()
if !active {
s.Write([]byte("not active"))
s.Exit(0)
return nil, nil
}
w, h := pty.Window.Width, pty.Window.Height
return bubble.New(w, h), []tea.ProgramOption{tea.WithAltScreen()}
}