forked from Kuantanamo-Bay-Labs/trashgang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
499 lines (435 loc) · 11.4 KB
/
Copy pathmain.go
File metadata and controls
499 lines (435 loc) · 11.4 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package main
import (
"fmt"
"hash/fnv"
"log"
"os"
"sort"
"strings"
"sync"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/ssh" // Charmbracelet ssh~
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/activeterm"
wishbubble "github.com/charmbracelet/wish/bubbletea"
"github.com/charmbracelet/wish/logging"
)
// Server chat states
type client struct {
name string
out chan string
}
var (
mu sync.Mutex
clients = map[string]*client{}
joinMsg = "* %s joined (%d online)"
leaveMsg = "* %s left (%d online)"
bus = make(chan string, 256) // broadcast stuff.
)
func init() {
go func() {
for msg := range bus {
mu.Lock()
for _, c := range clients {
select {
case c.out <- msg:
default:
}
}
mu.Unlock()
}
}()
}
func register(c *client) {
mu.Lock()
clients[c.name] = c
n := len(clients)
mu.Unlock()
bus <- fmt.Sprintf(joinMsg, c.name, n)
}
func unregister(c *client) {
mu.Lock()
if _, ok := clients[c.name]; ok {
delete(clients, c.name)
}
n := len(clients)
mu.Unlock()
close(c.out)
bus <- fmt.Sprintf(leaveMsg, c.name, n)
}
func listNames() []string {
mu.Lock()
defer mu.Unlock()
names := make([]string, 0, len(clients))
for k := range clients {
names = append(names, k)
}
sort.Strings(names)
return names
}
// Allow for anons too!
func uniqueName(base string) string {
name := strings.TrimSpace(base)
if name == "" {
name = "anon"
}
i := 1
mu.Lock()
defer mu.Unlock()
for {
if _, ok := clients[name]; !ok {
return name
}
name = fmt.Sprintf("%s-%d", base, i)
i++
}
}
// Styles assign a color per user.
var sysStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("245"))
// Nice readable 256-color codes
var palette = []string{
"39", "45", "51", "69", "75", "81",
"99", "105", "111", "147", "141", "135",
"118", "154", "192", "177",
}
func nameStyleFor(user string) lipgloss.Style {
h := fnv.New32a()
_, _ = h.Write([]byte(user))
idx := int(h.Sum32() % uint32(len(palette)))
return lipgloss.NewStyle().Foreground(lipgloss.Color(palette[idx])).Bold(true)
}
// TUI - Terminal User Interface
type model struct {
username string
cl *client
vp viewport.Model
input textinput.Model
help string
quitting bool
logBuf []string
nameStyled string
}
type msgIncoming string
func newModel(username string) *model {
ti := textinput.New()
ti.Placeholder = "Type message. Use | to send multiple lines. /help for commands."
ti.Prompt = "› "
ti.Focus()
ti.CharLimit = 0 // have to set 0 for no limits. plus ultra.
vp := viewport.New(80, 20)
vp.SetContent("")
cl := &client{name: username, out: make(chan string, 256)}
nameStyled := nameStyleFor(username).Render(username)
m := &model{
username: username,
cl: cl,
vp: vp,
input: ti,
help: "[Enter] send • /nick, /list, /help • Tip: separate lines with |",
logBuf: make([]string, 0, 256),
nameStyled: nameStyled,
}
// Local banner, can modify too based on client ig.
m.appendBlock(banner())
return m
}
func (m *model) Init() tea.Cmd {
register(m.cl)
return waitIncoming(m.cl.out)
}
func waitIncoming(ch <-chan string) tea.Cmd {
return func() tea.Msg {
if msg, ok := <-ch; ok {
return msgIncoming(msg)
}
return tea.Quit()
}
}
func (m *model) appendBlock(block string) {
m.logBuf = append(m.logBuf, block)
if len(m.logBuf) > 1000 {
m.logBuf = m.logBuf[len(m.logBuf)-1000:]
}
m.vp.SetContent(strings.Join(m.logBuf, "\n"))
m.vp.GotoBottom()
}
func (m *model) View() string {
header := fmt.Sprintf("TRASH GANG — %d online\n", len(listNames()))
footer := "\n" + m.help + "\n"
return strings.Join([]string{
header,
m.vp.View(),
footer,
m.input.View(),
}, "\n")
}
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case msgIncoming:
line := string(msg)
// Gray broadcasted simple lines
if strings.HasPrefix(line, "* ") {
m.appendBlock(sysStyle.Render(line))
} else {
m.appendBlock(line)
}
return m, waitIncoming(m.cl.out)
case tea.WindowSizeMsg:
h := msg.Height - 6
if h < 5 {
h = 5
}
m.vp.Width = msg.Width
m.vp.Height = h
m.input.Width = msg.Width
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
m.quitting = true
unregister(m.cl)
return m, tea.Quit
case "enter":
raw := strings.TrimSpace(m.input.Value())
if raw == "" {
return m, nil
}
// Commands
if strings.HasPrefix(raw, "/") {
if quit := m.runCmd(raw); quit {
m.quitting = true
unregister(m.cl)
return m, tea.Quit
}
} else {
// CHANGE THIS LATER -> Allow for a /ascii command.
// split on '|' separators -> Mainly for ASCII
parts := splitSpam(raw)
for _, p := range parts {
if p == "" {
continue
}
bus <- fmt.Sprintf("[%s] %s", m.nameStyled, p)
}
}
m.input.SetValue("")
return m, nil
}
}
var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)
return m, cmd
}
func splitSpam(s string) []string {
s = strings.ReplaceAll(s, "\\n", "|")
chunks := strings.Split(s, "|")
out := make([]string, 0, len(chunks))
for _, c := range chunks {
c = strings.TrimSpace(c)
out = append(out, c)
}
return out
}
// 1) Put this helper anywhere in main.go (e.g., above runCmd):
// splitArgs splits a command line into args, respecting "quotes".
func splitArgs(line string) []string {
var args []string
var cur strings.Builder
inQuotes := false
escape := false
for _, r := range line {
switch {
case inQuotes && escape:
if r == '"' {
cur.WriteRune('"')
} else {
cur.WriteRune('\\')
cur.WriteRune(r)
}
escape = false
case inQuotes && r == '\\':
escape = true
case r == '"':
inQuotes = !inQuotes
case !inQuotes && (r == ' ' || r == '\t'):
if cur.Len() > 0 {
args = append(args, cur.String())
cur.Reset()
}
default:
cur.WriteRune(r)
}
}
if cur.Len() > 0 {
args = append(args, cur.String())
}
return args
}
func (m *model) runCmd(line string) (quit bool) {
parts := splitArgs(line) // <-- instead of strings.Fields(line)
if len(parts) == 0 {
return false
}
cmd := strings.ToLower(parts[0])
switch cmd {
case "/help":
m.appendBlock(sysStyle.Render("* Commands: /help /list /nick <name> /ascii <path|url> [--w=80] [--color] [--invert] [--charset=\"@%#*+=-:. \"] /quit"))
m.appendBlock(sysStyle.Render("* Tip: Use `|` between phrases to send multiple lines at once."))
case "/list":
m.appendBlock(sysStyle.Render("* users: " + strings.Join(listNames(), ", ")))
case "/nick":
if len(parts) < 2 {
m.appendBlock(sysStyle.Render("* usage: /nick <newname>"))
break
}
newName := uniqueName(strings.TrimSpace(parts[1]))
mu.Lock()
if _, ok := clients[m.cl.name]; ok {
delete(clients, m.cl.name)
}
old := m.cl.name
m.cl.name = newName
clients[newName] = m.cl
mu.Unlock()
m.username = newName
m.nameStyled = nameStyleFor(newName).Render(newName)
bus <- fmt.Sprintf("* %s is now known as %s", old, newName)
case "/ascii":
if len(parts) < 2 {
m.appendBlock(sysStyle.Render("* usage: /ascii <path-or-url> [--w=80] [--color] [--invert] [--charset=\"@%#*+=-:. \"]"))
break
}
// Defaults
src := ""
width := 80
fit := false
colorize := false
invert := false
charset := "@%#*+=-:. "
// Parse args
for _, p := range parts[1:] {
if strings.HasPrefix(p, "--w=") {
var w int
if _, err := fmt.Sscanf(p, "--w=%d", &w); err == nil && w >= 8 && w <= 400 {
width = w
}
continue
}
if p == "--color" {
colorize = true
continue
}
if p == "--invert" {
invert = true
continue
}
if p == "--fit" {
fit = true
continue
}
if strings.HasPrefix(p, "--charset=") {
cs := strings.TrimPrefix(p, "--charset=")
cs = strings.Trim(cs, `"`)
if cs != "" {
charset = cs
}
continue
}
if !strings.HasPrefix(p, "--") && src == "" {
src = p
}
}
if src == "" {
m.appendBlock(sysStyle.Render("* usage: /ascii <path-or-url> [--w=80] [--color] [--invert] [--charset=...]"))
break
}
if (width == 80 || fit) && m.vp.Width > 0 {
width = m.vp.Width
if width > 200 {
width = 200 // keep sane for huge terminals
}
}
// Hard clamp to viewport minus a tiny margin (prevents wrap)
if m.vp.Width > 0 {
max := m.vp.Width - 2 // 1–2 cols for safety
if max < 8 {
max = 8
}
if width > max {
width = max
}
}
// Render without blocking UI
go func() {
bus <- sysStyle.Render("* rendering ascii…")
block, meta, err := asciiFromSource(src, width, colorize, invert, charset)
if err != nil {
bus <- sysStyle.Render("* ascii error: " + err.Error())
return
}
header := sysStyle.Render(
fmt.Sprintf("* %s (%dx%d → %dx%d, color:%v, invert:%v)",
meta.Source, meta.OrigW, meta.OrigH, meta.OutW, meta.OutH, colorize, invert,
),
)
bus <- header + "\n" + block
}()
case "/quit", "/exit":
return true
default:
m.appendBlock(sysStyle.Render("* Unknown command. Try /help"))
}
return false
}
// BubbleTea/Wish integration
func teaHandler(sess ssh.Session) (tea.Model, []tea.ProgramOption) {
user := strings.TrimSpace(sess.User())
md := newModel(uniqueName(user))
opts := wishbubble.MakeOptions(sess)
return md, opts
}
// Mian
func main() {
addr := getEnv("ADDR", ":2022")
keyPath := getEnv("HOSTKEY", "./.trashgang_keys/ed25519")
if _, err := os.Stat(keyPath); err != nil {
log.Printf("Host key not found at %s.\nGenerate with:\n ssh-keygen -t ed25519 -f %s -N ''", keyPath, keyPath)
}
s, err := wish.NewServer(
wish.WithAddress(addr),
wish.WithHostKeyPath(keyPath),
wish.WithMiddleware(
wishbubble.Middleware(teaHandler),
logging.Middleware(),
activeterm.Middleware(),
),
)
if err != nil {
log.Fatal(err)
}
log.Printf("TrashGang (Wish) listening on %s (SSH)", addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
func getEnv(k, def string) string {
if v := os.Getenv(k); v != "" {
return v
}
return def
}
// Banner!
func banner() string {
return `
████████╗██████╗ █████╗ ███████╗██╗ ██╗ ██████╗ █████╗ ███╗ ██╗ ██████╗
╚══██╔══╝██╔══██╗██╔══██╗██╔════╝██║ ██║ ██╔════╝ ██╔══██╗████╗ ██║██╔════╝
██║ ██████╔╝███████║███████╗███████║ ██║ ███╗███████║██╔██╗ ██║██║ ███╗
██║ ██╔══██╗██╔══██║╚════██║██╔══██║ ██║ ██║██╔══██║██║╚██╗██║██║ ██║
██║ ██║ ██║██║ ██║███████║██║ ██║ ╚██████╔╝██║ ██║██║ ╚████║╚██████╔╝
╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝
Welcome! Type /help for commands.
`
}