-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.go
110 lines (97 loc) · 2.78 KB
/
server.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
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"
"github.com/jon4hz/fztea/flipperui"
"github.com/jon4hz/fztea/recfz"
"github.com/muesli/coral"
)
var serverFlags struct {
listen string
authorizedKeys string
}
var serverCmd = &coral.Command{
Use: "server",
Short: "Start an ssh server serving the flipper zero TUI",
Run: server,
}
func init() {
serverCmd.Flags().StringVarP(&serverFlags.listen, "listen", "l", "127.0.0.1:2222", "address to listen on")
serverCmd.Flags().StringVarP(&serverFlags.authorizedKeys, "authorized-keys", "k", "", "authorized_keys file for public key authentication")
}
func server(cmd *coral.Command, _ []string) {
// parse screenshot resolution
screenshotResolution, err := parseScreenshotResolution()
if err != nil {
log.Fatalf("failed to parse screenshot resolution: %s", err)
}
screenUpdates := make(chan flipperui.ScreenMsg)
fz, err := recfz.NewFlipperZero(
recfz.WithPort(rootFlags.port),
recfz.WithStreamScreenCallback(flipperui.UpdateScreen(screenUpdates)),
recfz.WithContext(cmd.Context()),
)
if err != nil {
log.Fatal(err)
}
defer fz.Close()
if err := fz.Connect(); err != nil {
log.Fatal(err)
}
cl := newConnLimiter(1)
sshOpts := []ssh.Option{
wish.WithAddress(serverFlags.listen),
wish.WithHostKeyPath(".ssh/flipperzero_tea_ed25519"),
wish.WithMiddleware(
bm.Middleware(func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
_, _, active := s.Pty()
if !active {
wish.Fatalln(s, "no active terminal, skipping")
return nil, nil
}
m := model{
flipper: flipperui.New(fz, screenUpdates,
flipperui.WithScreenshotResolution(screenshotResolution.width, screenshotResolution.height),
flipperui.WithFgColor(rootFlags.fgColor),
flipperui.WithBgColor(rootFlags.bgColor)),
}
return m, []tea.ProgramOption{tea.WithAltScreen(), tea.WithMouseCellMotion()}
}),
lm.Middleware(),
connLimit(cl),
),
}
if serverFlags.authorizedKeys != "" {
sshOpts = append(sshOpts, wish.WithAuthorizedKeys(serverFlags.authorizedKeys))
}
s, err := wish.NewServer(
sshOpts...,
)
if err != nil {
log.Fatalln(err)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Printf("Starting SSH server on %s", serverFlags.listen)
go func() {
if err = s.ListenAndServe(); err != nil {
log.Fatalln(err)
}
}()
<-done
log.Println("Stopping SSH server")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := s.Shutdown(ctx); err != nil {
log.Fatalln(err)
}
}