-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
697 lines (624 loc) · 19.1 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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
package main
import (
"bufio"
"container/list"
"encoding/base64"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"sort"
"strconv"
"strings"
"time"
"unicode"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/crypto/sha3"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
func init() {
messageCache = list.New()
}
func main() {
// whitelist, err := loadPubkeyList("whitelist.txt")
// if err != nil {
// log.Printf("Error loading whitelist: %v", err)
// return
// }
// blacklist, err := loadPubkeyList("blacklist.txt")
// if err != nil {
// log.Printf("Error loading blacklist: %v", err)
// return
// }
db := initSqliteDB()
if db == nil {
log.Panic("couldn't load main db")
return
}
defer db.Close()
initBoardSchema(db)
var privateKeyPath string
flag.StringVar(&privateKeyPath, "key", "./keys/ssh_host_ed25519_key", "Path to the private key")
flag.Parse()
if _, err := os.Stat("./keys"); os.IsNotExist(err) {
fmt.Println("Error: ./keys directory does not exist. Please create it and generate an ed25519 keypair.")
return
}
if _, err := os.Stat(privateKeyPath); os.IsNotExist(err) {
fmt.Printf("Error: private key file %s does not exist. Please generate an ed25519 keypair.\n", privateKeyPath)
return
}
users = make(map[string]*user)
if len(os.Args) != 2 {
fmt.Printf("Usage: %s <port>\n", os.Args[0])
return
}
config, err := configureSSHServer(privateKeyPath)
if err != nil {
fmt.Println("Error configuring SSH server:", err.Error())
return
}
listener, err := net.Listen("tcp", ":"+os.Args[1])
if err != nil {
fmt.Println("Error listening:", err.Error())
return
}
defer listener.Close()
fmt.Println("Listening on :" + os.Args[1])
go api(db)
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting:", err.Error())
continue
}
go func(conn net.Conn) {
defer conn.Close()
sshConn, chans, reqs, err := ssh.NewServerConn(conn, config)
if err != nil {
fmt.Println("Error upgrading connection to SSH:", err.Error())
return
}
defer sshConn.Close()
go ssh.DiscardRequests(reqs)
for newChannel := range chans {
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel, requests, err := newChannel.Accept()
if err != nil {
fmt.Println("Error accepting channel:", err.Error())
return
}
// go handleConnection(db, channel, sshConn, requests, whitelist, blacklist)
go handleConnection(db, channel, sshConn, requests)
}
}(conn)
}
}
// func handleConnection(db *sqlx.DB, channel ssh.Channel, sshConn *ssh.ServerConn, requests <-chan *ssh.Request, whitelist map[string]bool, blacklist map[string]bool) {
func handleConnection(db *sqlx.DB, channel ssh.Channel, sshConn *ssh.ServerConn, requests <-chan *ssh.Request) {
defer channel.Close()
if sshConn.Permissions == nil || sshConn.Permissions.Extensions == nil {
fmt.Fprintln(channel, "Unable to retrieve your public key.")
return
}
pubkey, ok := sshConn.Permissions.Extensions["pubkey"]
if !ok {
fmt.Fprintln(channel, "Unable to retrieve your public key.")
return
}
hash := formatUsernameFromPubkey(pubkey)
// if _, ok := whitelist[hash]; !ok {
// fmt.Fprintln(channel, "You are not authorized to connect to this server.")
// disconnect(hash)
// return
// }
// if _, ok := blacklist[hash]; ok {
// fmt.Fprintln(channel, "You have been banned from this server.")
// disconnect(hash)
// return
// }
addUser(hash, &user{Pubkey: pubkey, Hash: hash, Conn: channel})
term := term.NewTerminal(channel, "")
term.SetPrompt("")
saveCursorPos(channel)
restoreCursorPos(channel)
welcome := welcomeMessageAscii()
term.Write([]byte(welcome))
printMOTD(loadMOTD(motdFilePath), term)
printCachedMessages(term)
term.Write([]byte("\nWelcome :) You are " + hash + "\n"))
for {
input, err := term.ReadLine()
if err != nil {
if err == io.EOF {
disconnect(hash)
return
}
readlineErrCheck(term, err, hash)
return
}
switch {
case strings.HasPrefix(input, "/ignore"):
handleIgnore(input, term, hash)
case strings.HasPrefix(input, "/help") || strings.HasPrefix(input, "/?"):
writeHelpMenu(term)
case strings.HasPrefix(input, "/license"):
writeLicenseProse(term)
case strings.HasPrefix(input, "/version"):
writeVersionInfo(term)
case strings.HasPrefix(input, "/users"):
writeUsersOnline(term)
case strings.HasPrefix(input, "/bulletin") || strings.HasPrefix(input, "/motd"):
printMOTD(motdFilePath, term)
case strings.HasPrefix(input, "/pubkey"):
term.Write([]byte("Your pubkey hash: " + hash + "\n"))
case strings.HasPrefix(input, "/message"):
handleMessage(input, term, hash)
case strings.HasPrefix(input, "/post"):
handlePost(input, term, db, hash)
case strings.HasPrefix(input, "/list"):
listDiscussions(db, term)
case strings.HasPrefix(input, "/history"):
printCachedMessages(term)
case strings.HasPrefix(input, "/tokens new"):
handleTokenNew(db, term, hash)
case strings.HasPrefix(input, "/tokens list"):
handleTokenList(db, term, hash)
case strings.HasPrefix(input, "/tokens revoke"):
handleTokenRevoke(db, input, term, hash)
case strings.HasPrefix(input, "/quit") || strings.HasPrefix(input, "/q") ||
strings.HasPrefix(input, "/exit") || strings.HasPrefix(input, "/x") ||
strings.HasPrefix(input, "/leave") || strings.HasPrefix(input, "/part"):
disconnect(hash)
case strings.HasPrefix(input, "/replies"):
handleReplies(input, term, db)
case strings.HasPrefix(input, "/reply"):
handleReply(input, term, db, hash)
default:
if len(input) > 0 {
if strings.HasPrefix(input, "/") {
term.Write([]byte("Unrecognized command. Type /help for available commands.\n"))
} else {
message := fmt.Sprintf("[%s] %s: %s", time.Now().String()[11:16], hash, input)
broadcast(hash, message+"\r")
}
}
}
}
}
// func loadPubkeyList(filename string) (map[string]bool, error) {
// file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
// if err != nil {
// return nil, fmt.Errorf("unable to open %s: %v", filename, err)
// }
// defer file.Close()
// pubkeyList := make(map[string]bool)
// scanner := bufio.NewScanner(file)
// for scanner.Scan() {
// pubkey := scanner.Text()
// pubkeyList[pubkey] = true
// }
// if err := scanner.Err(); err != nil {
// return nil, fmt.Errorf("error reading %s: %v", filename, err)
// }
// return pubkeyList, nil
// }
func saveCursorPos(channel ssh.Channel) {
writeString(channel, "\033[s")
}
func restoreCursorPos(channel ssh.Channel) {
writeString(channel, "\033[u")
}
func moveCursorUp(channel ssh.Channel, n int) {
writeString(channel, fmt.Sprintf("\033[%dA", n))
}
func moveCursorDown(w io.Writer, lines int) {
fmt.Fprintf(w, "\033[%dB", lines)
}
func writeString(channel ssh.Channel, s string) {
channel.Write([]byte(s))
}
func generateHash(pubkey string) string {
h := sha3.NewShake256()
h.Write([]byte(pubkey))
checksum := make([]byte, 16)
h.Read(checksum)
return base64.StdEncoding.EncodeToString(checksum)
}
func disconnect(hash string) {
usersMutex.Lock()
user, exists := users[hash]
usersMutex.Unlock()
if exists {
user.Conn.Close()
}
removeUser(hash)
}
func broadcast(senderHash, message string) {
addToCache(message)
for _, user := range getAllUsers() {
if user.Hash == senderHash {
continue
}
saveCursorPos(user.Conn)
moveCursorUp(user.Conn, 1)
fmt.Fprintln(user.Conn, message)
restoreCursorPos(user.Conn)
moveCursorDown(user.Conn, 1)
fmt.Fprint(user.Conn, "\n")
if user.Conn == nil {
log.Printf("broadcast: user with hash %v has nil connection\n", user.Hash)
continue
}
log.Printf("Broadcasted message to user with hash %v\n", user.Hash)
}
}
func addDiscussion(db *sqlx.DB, author, message string) int {
res, err := db.Exec("INSERT INTO discussions (author, message) VALUES (?, ?)", author, message)
if err != nil {
log.Println(err)
return -1
}
id, err := res.LastInsertId()
if err != nil {
log.Println(err)
return -1
}
return int(id)
}
func addReply(db *sqlx.DB, postNumber int, author, message string) bool {
_, err := db.Exec("INSERT INTO replies (discussion_id, author, message) VALUES (?, ?, ?)", postNumber, author, message)
if err != nil {
log.Println(err)
return false
}
return true
}
func listDiscussions(db *sqlx.DB, term *term.Terminal) {
var discussions []struct {
ID int `db:"id"`
Author string `db:"author"`
Message string `db:"message"`
ReplyCount int `db:"reply_count"`
}
err := db.Select(&discussions, `
SELECT d.id, d.author, d.message, COUNT(r.id) as reply_count
FROM discussions d
LEFT JOIN replies r ON d.id = r.discussion_id
GROUP BY d.id
`)
if err != nil {
log.Printf("Error retrieving discussions: %v", err)
term.Write([]byte("Error retrieving discussions.\n"))
return
}
sort.Slice(discussions, func(i, j int) bool {
weightID := 0.3
weightReplyCount := 0.7
scoreI := weightID*float64(discussions[i].ID) + weightReplyCount*float64(discussions[i].ReplyCount)
scoreJ := weightID*float64(discussions[j].ID) + weightReplyCount*float64(discussions[j].ReplyCount)
return scoreI > scoreJ
})
term.Write([]byte("Discussions:\n\n[id.]\t[💬replies]\t[topic]\n\n"))
for _, disc := range discussions {
term.Write([]byte(fmt.Sprintf("%d.\t💬%d\t[%s] %s\n", disc.ID, disc.ReplyCount, disc.Author, disc.Message)))
}
}
func listReplies(db *sqlx.DB, postNumber int, term *term.Terminal) {
var disc discussion
err := db.Get(&disc, "SELECT id, author, message FROM discussions WHERE id = ?", postNumber)
if err != nil {
log.Printf("Error retrieving discussion: %v", err)
term.Write([]byte("Invalid post number.\n"))
return
}
term.Write([]byte(fmt.Sprintf("Replies to post %d [%s]:\n", disc.ID, disc.Author)))
var replies []*reply
err = db.Select(&replies, "SELECT author, message FROM replies WHERE discussion_id = ?", postNumber)
if err != nil {
log.Printf("Error retrieving replies: %v", err)
term.Write([]byte("Error retrieving replies.\n"))
return
}
for i, rep := range replies {
term.Write([]byte(fmt.Sprintf("%d. [%s] %s\n", i+1, rep.Author, rep.Message)))
}
}
func addToCache(message string) {
messageCache.PushBack(message)
if messageCache.Len() > 100 {
messageCache.Remove(messageCache.Front())
}
}
func printCachedMessages(term *term.Terminal) {
for e := messageCache.Front(); e != nil; e = e.Next() {
term.Write([]byte(e.Value.(string) + "\r\n"))
}
}
func printMOTD(motd string, term *term.Terminal) {
if motd != "" {
term.Write([]byte(motd + "\r\n"))
}
}
func configureSSHServer(privateKeyPath string) (*ssh.ServerConfig, error) {
privateKeyBytes, err := os.ReadFile(privateKeyPath)
if err != nil {
return nil, fmt.Errorf("failed to read private key: %w", err)
}
privateKey, err := ssh.ParsePrivateKey(privateKeyBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %w", err)
}
config := &ssh.ServerConfig{
PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
fmt.Printf("Received public key of type %s from user %s\n", key.Type(), conn.User())
return &ssh.Permissions{
Extensions: map[string]string{
"pubkey": string(key.Marshal()),
},
}, nil
},
}
config.AddHostKey(privateKey)
return config, nil
}
func addUser(hash string, u *user) {
usersMutex.Lock()
defer usersMutex.Unlock()
u.Ignored = make(map[string]bool)
users[hash] = u
}
func removeUser(hash string) {
usersMutex.Lock()
defer usersMutex.Unlock()
delete(users, hash)
}
func getAllUsers() []*user {
usersMutex.Lock()
defer usersMutex.Unlock()
allUsers := make([]*user, 0, len(users))
for _, user := range users {
allUsers = append(allUsers, user)
}
return allUsers
}
func cleanString(dirtyString string) (string, error) {
var clean strings.Builder
for _, r := range dirtyString {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
clean.WriteRune(r)
}
}
if clean.Len() < 8 {
return "", errors.New("not enough characters after cleaning")
}
return clean.String()[:8], nil
}
func sendMessage(senderHash, recipientHash, message string, term *term.Terminal) {
usersMutex.Lock()
recipient, ok := users[recipientHash]
usersMutex.Unlock()
if !ok {
fmt.Fprintf(users[senderHash].Conn, "\n\rUser with hash %s not found\n", recipientHash)
return
}
if recipient.Ignored[senderHash] {
return
}
message = "\r\n[DirectMessage][" + senderHash + "] " + message + "\r\n"
fmt.Fprintln(recipient.Conn, message)
term.Write([]byte(message))
}
func loadMOTD(motdFilePath string) string {
var motdMessage string
file, err := os.Open(motdFilePath)
if err != nil {
if os.IsNotExist(err) {
os.Create(motdFilePath)
if err != nil {
log.Println("we weren't able to create it either: " + err.Error())
return ""
}
log.Println("motd didn't exist: " + err.Error())
return ""
}
log.Println("error opening motdFilePath: " + err.Error())
return ""
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "#") {
motdMessage += line + "\n"
}
}
return motdMessage
}
func handleReply(input string, term *term.Terminal, db *sqlx.DB, hash string) error {
parts := strings.SplitN(input, " ", 3)
if len(parts) < 3 {
return fmt.Errorf("usage: /reply <post number> <reply body>")
}
postNum, err := strconv.Atoi(parts[1])
if err != nil {
return fmt.Errorf("invalid post number. Usage: /reply <post number> <reply body>")
}
replyBody := parts[2]
replySuccess := addReply(db, postNum, hash, replyBody)
if !replySuccess {
return fmt.Errorf("failed to reply to post. Please check the post number and try again")
} else {
term.Write([]byte("Reply successfully added to post.\n"))
return nil
}
}
func handleReplies(input string, term *term.Terminal, db *sqlx.DB) {
parts := strings.SplitN(input, " ", 2)
if len(parts) < 2 {
term.Write([]byte("Usage: /replies <post number>\n"))
return
}
postNum, err := strconv.Atoi(parts[1])
if err != nil {
term.Write([]byte("Invalid post number. Usage: /replies <post number>\n"))
return
}
listReplies(db, postNum, term)
}
func handleIgnore(input string, term *term.Terminal, hash string) {
parts := strings.Split(input, " ")
if len(parts) != 2 {
term.Write([]byte("Usage: /ignore <user hash>\n"))
return
}
ignoredUser := parts[1]
usersMutex.Lock()
_, exists := users[ignoredUser]
usersMutex.Unlock()
if !exists {
term.Write([]byte("User " + ignoredUser + " not found.\n"))
} else if ignoredUser == hash {
term.Write([]byte("You cannot ignore yourself.\n"))
} else {
users[hash].Ignored[ignoredUser] = true
term.Write([]byte("User " + ignoredUser + " is now ignored.\n"))
}
}
func readlineErrCheck(term *term.Terminal, err error, hash string) {
term.Write([]byte("Error reading input: "))
term.Write([]byte(err.Error()))
term.Write([]byte("\n"))
disconnect(hash)
}
func handlePost(input string, term *term.Terminal, db *sqlx.DB, hash string) {
parts := strings.SplitN(input, " ", 2)
if len(parts) < 2 {
term.Write([]byte("Usage: /post <message>\n"))
} else {
postNumber := addDiscussion(db, hash, parts[1])
term.Write([]byte(fmt.Sprintf("Posted new discussion with post number %d.\n", postNumber)))
}
}
func handleMessage(input string, term *term.Terminal, hash string) {
parts := strings.Split(input, " ")
if len(parts) < 3 {
term.Write([]byte("Usage: /message <user hash> <direct message text>\n"))
} else {
recipientHash := parts[1]
message := strings.Join(parts[2:], " ")
sendMessage(hash, recipientHash, message, term)
}
}
func formatUsernameFromPubkey(pubkey string) string {
hash, err := cleanString(generateHash(pubkey))
if err != nil {
log.Println("error generating username: ", err)
}
hash = "@" + hash
return hash
}
func welcomeMessageAscii() string {
welcome := `
BB BB BB BB BB
,adPPYba, BB,dPPYba, BB,dPPYba, BB,dPPYba, BB,dPPYba, BB,dPPYba,
I8[ "" BBP' "8a BBP' "8a BBP' "8a BBP' "8a BBP' "8a
'"Y8ba, BB BB BB BB BB BB BB d8 BB d8
aa ]8I BB BB BB BB BB BB BBb, ,a8" BBb, ,a8"
'"YbbdP"' BB BB BB BB BB BB 8Y"Ybbd8"' 8Y"Ybbd8"' BBS
> MIT 2023, https://github.com/donuts-are-good/shhhbb ` + semverInfo + `
[RULES] [GOALS]
- your words are your own - a space for hackers & devs
- your eyes are your own - make cool things
- no chat logs are kept - collaborate & share
- have fun :) - evolve
Say hello and press [enter] to chat
Type /help for more commands.
`
return welcome
}
func writeUsersOnline(term *term.Terminal) {
term.Write([]byte("Connected users:\n"))
for _, user := range users {
term.Write([]byte("- " + user.Hash + "\n"))
}
}
func writeHelpMenu(term *term.Terminal) {
term.Write([]byte(`
[General Commands]
/help
- show this help message
/pubkey
- show your pubkey hash, which is also your username
/users
- list all online users
/message <user hash> <body>
- ex: /message @A1Gla593 hey there friend
- send a direct message to a user
/quit, /q, /exit, /x
- disconnect, exit, goodbye
[Chat commands]
/history
- reloads the last 100 lines of chat history
[Message Board]
/post <message>
- ex: /post this is my cool title
- posts a new discussion topic
/list
- list all discussions
/replies <post number>
- ex: /replies 1
- list all replies to a discussion
/reply <post number> <reply body>
- ex: /reply 1 hello everyone
- reply to a discussion
[API Commands]
/tokens new
- create a new shhhbb API token
/tokens list
- view your shhhbb API tokens
/tokens revoke <token>
- revoke an shhhbb API token
[Misc. Commands]
/license
- display the license text for shhhbb
/version
- display the shhhbb version information
` + "\n"))
}
func writeLicenseProse(term *term.Terminal) {
term.Write([]byte(`
MIT License
Copyright (c) 2023 donuts-are-good https://github.com/donuts-are-good
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`))
}
func writeVersionInfo(term *term.Terminal) {
term.Write([]byte(`
shhhbb bbs ` + semverInfo + `
MIT License 2023 donuts-are-good
https://github.com/donuts-are-good/shhhbb
`))
}