/
main.go
143 lines (124 loc) · 3.59 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"errors"
"flag"
"fmt"
"github.com/BigJk/end_of_eden/system/settings"
"github.com/BigJk/end_of_eden/ui/menus/mainmenu"
"github.com/BigJk/end_of_eden/ui/root"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
"os"
"os/signal"
"sync"
"syscall"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
lm "github.com/charmbracelet/wish/logging"
"github.com/muesli/termenv"
)
var mtx sync.Mutex
var instanceLimit int
var instances int
func main() {
fmt.Println("End Of Eden :: SSH Server")
fmt.Println("Each SSH session creates it's own game session. Modding and audio not supported.")
fmt.Println()
bind := flag.String("bind", ":8273", "ip and port to bind to")
timeout := flag.Int("timeout", 0, "ssh idle timeout")
maxInstance := flag.Int("max_inst", 10, "maximum of game instances")
help := flag.Bool("help", false, "show help")
flag.Parse()
if *help {
flag.PrintDefaults()
return
}
if err := os.Setenv("NO_CLIPBOARD", "true"); err != nil {
panic(err)
}
termenv.SetDefaultOutput(termenv.NewOutput(os.Stdout, termenv.WithProfile(termenv.TrueColor)))
options := []ssh.Option{
wish.WithAddress(*bind),
wish.WithHostKeyPath(".ssh/term_info_ed25519"),
wish.WithMiddleware(
func(handler ssh.Handler) ssh.Handler {
return func(session ssh.Session) {
mtx.Lock()
instances -= 1
mtx.Unlock()
handler(session)
}
},
gameMiddleware(),
func(handler ssh.Handler) ssh.Handler {
return func(session ssh.Session) {
mtx.Lock()
if instanceLimit > 0 && instances >= instanceLimit {
mtx.Unlock()
log.Warn("Denying instance because of limit!")
_, _ = session.Write([]byte("Too many instances... Please try again later."))
time.Sleep(time.Second * 2)
_ = session.Close()
return
}
instances += 1
mtx.Unlock()
handler(session)
}
},
lm.Middleware(),
),
}
if *timeout > 0 {
options = append(options, wish.WithIdleTimeout(time.Duration(*timeout)*time.Minute))
}
instanceLimit = *maxInstance
s, err := wish.NewServer(options...)
if err != nil {
log.Error("could not start server", "error", err)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Info("Starting SSH server:", "bind", *bind, "max_inst", *maxInstance, "timeout", *timeout)
go func() {
if err = s.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
log.Error("could not start server", "error", err)
}
}()
<-done
log.Info("Stopping SSH server")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := s.Shutdown(ctx); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
log.Error("could not stop server", "error", err)
}
}
func gameMiddleware() wish.Middleware {
newProg := func(m tea.Model, opts ...tea.ProgramOption) *tea.Program {
p := tea.NewProgram(m, opts...)
lipgloss.SetColorProfile(termenv.TrueColor)
return p
}
teaHandler := func(s ssh.Session) *tea.Program {
_, _, active := s.Pty()
if !active {
wish.Fatalln(s, "no active terminal, skipping")
return nil
}
zones := zone.New()
return newProg(
root.New(zones, mainmenu.NewModel(zones, settings.GetGlobal(), nil, nil)),
tea.WithInput(s),
tea.WithOutput(s),
tea.WithANSICompressor(),
tea.WithMouseAllMotion(),
tea.WithAltScreen(),
)
}
return bm.MiddlewareWithProgramHandler(teaHandler, termenv.ANSI256)
}