Skip to content

Commit

Permalink
exiting is solved i think
Browse files Browse the repository at this point in the history
  • Loading branch information
donuts-are-good committed Apr 5, 2023
1 parent f6c3253 commit 60feda0
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 65 deletions.
13 changes: 2 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@ module github.com/donuts-are-good/shhhbb
go 1.20

require (
github.com/gdamore/tcell/v2 v2.6.0
github.com/jmoiron/sqlx v1.3.5
github.com/mattn/go-sqlite3 v1.14.16
github.com/rivo/tview v0.0.0-20230330183452-5796b0cd5c1f
golang.org/x/crypto v0.7.0
golang.org/x/term v0.6.0
)

require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
)
require golang.org/x/sys v0.6.0 // indirect
41 changes: 0 additions & 41 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,56 +1,15 @@
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/rivo/tview v0.0.0-20230330183452-5796b0cd5c1f h1:vpjWdGBgikHYD4ruBvDINMxwdh5UWVck9yOyrwFktMo=
github.com/rivo/tview v0.0.0-20230330183452-5796b0cd5c1f/go.mod h1:nVwGv4MP47T0jvlk7KuTTjjuSmrGO4JF0iaiNt4bufE=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
105 changes: 92 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"container/list"
"encoding/base64"
"errors"
Expand All @@ -25,17 +26,21 @@ func init() {
messageCache = list.New()
}
func main() {

db := initSqliteDB()
if db == nil {
log.Panic("couldn't load main db")
return
}
defer db.Close()

initBoardSchema(db)

adminDB := initAdminDB()
if adminDB == nil {
log.Println("couldn't load admin db")
return
}
defer adminDB.Close()

initAdminSchema(adminDB)

go adminAPI()
Expand Down Expand Up @@ -99,6 +104,16 @@ func main() {
}(conn)
}
}
func clearScreen(term *term.Terminal) {
term.Write([]byte("\033[2J"))
term.Write([]byte("\033[H"))
}
func saveCursorPosition(term *term.Terminal) {
term.Write([]byte("\033[s"))
}
func restoreCursorPosition(term *term.Terminal) {
term.Write([]byte("\033[u"))
}

func generateHash(pubkey string) string {
h := sha3.NewShake256()
Expand All @@ -109,6 +124,14 @@ func generateHash(pubkey string) string {
}

func disconnect(hash string) {
usersMutex.Lock()
user, exists := users[hash]
usersMutex.Unlock()

if exists {
user.Conn.Close()
}

removeUser(hash)
}

Expand Down Expand Up @@ -195,6 +218,12 @@ func printCachedMessages(term *term.Terminal) {
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)
Expand Down Expand Up @@ -273,6 +302,34 @@ func sendMessage(senderHash, recipientHash, message string, term *term.Terminal)
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 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 {
Expand All @@ -284,17 +341,14 @@ func handleConnection(db *sqlx.DB, channel ssh.Channel, sshConn *ssh.ServerConn,
fmt.Fprintln(channel, "Unable to retrieve your public key.")
return
}
makeUsername:
hash, err := cleanString(generateHash(pubkey))
if err != nil {
goto makeUsername // yolo, im not sorry for using goto
}
hash = "@" + hash
hash := formatUsernameFromPubkey(pubkey)
addUser(hash, &user{Pubkey: pubkey, Hash: hash, Conn: channel})
term := term.NewTerminal(channel, "\r\n> ")
welcome := welcomeMessageAscii()
printCachedMessages(term)

term.Write([]byte(welcome))
printMOTD(loadMOTD(motdFilePath), term)
printCachedMessages(term)
term.Write([]byte("\nWelcome :) You are " + hash))
for {
input, err := term.ReadLine()
Expand Down Expand Up @@ -354,7 +408,19 @@ makeUsername:
postNumber := addDiscussion(db, hash, parts[1])
term.Write([]byte(fmt.Sprintf("Posted new discussion with post number %d.\n", postNumber)))
} else if strings.HasPrefix(input, "/list") {
listDiscussions(db, term)

} else if strings.HasPrefix(input, "/quit") {
disconnect(hash)
} else if strings.HasPrefix(input, "/q") {
disconnect(hash)
} else if strings.HasPrefix(input, "/exit") {
disconnect(hash)
} else if strings.HasPrefix(input, "/x") {
disconnect(hash)
} else if strings.HasPrefix(input, "/leave") {
disconnect(hash)
} else if strings.HasPrefix(input, "/part") {
disconnect(hash)
} else if strings.HasPrefix(input, "/replies") {
parts := strings.SplitN(input, " ", 2)
if len(parts) < 2 {
Expand Down Expand Up @@ -386,14 +452,27 @@ makeUsername:
term.Write([]byte("Reply successfully added to post.\n"))
}
} else {
message := fmt.Sprintf("[%s]: %s", hash, input)
if len(input) > 0 || !strings.HasPrefix(input, "/") {
broadcast(message + "\r")
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", hash, input)
broadcast(message + "\r")
}
}
}
}
}

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 := `
Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var users = make(map[string]*user)
var usersMutex sync.Mutex
var messageCache *list.List
var semverInfo = "v0.3.0"
var motdFilePath = "./general-motd.txt"

type user struct {
Pubkey string `json:"pubkey" db:"pubkey"`
Expand Down

0 comments on commit 60feda0

Please sign in to comment.