go-chat/server/main.go

473 lines
12 KiB
Go
Raw Permalink Normal View History

2024-01-14 17:59:51 +00:00
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
2024-05-31 14:11:30 +00:00
"slices"
2024-01-14 17:59:51 +00:00
"strings"
"sync"
"time"
c "go-chat/common"
"github.com/alexflint/go-arg"
2024-05-31 13:34:54 +00:00
"github.com/jmoiron/sqlx"
_ "modernc.org/sqlite"
2024-01-14 17:59:51 +00:00
ws "nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
type user struct {
room string
nick string
}
type conns struct {
sm sync.Mutex
cm map[*ws.Conn]user
}
type server struct {
admin string
2024-05-31 13:34:54 +00:00
dbase *sqlx.DB
2024-01-14 17:59:51 +00:00
logFn func(string, ...interface{})
conns *conns
2024-05-31 13:34:54 +00:00
rooms map[string]string
2024-01-14 17:59:51 +00:00
rhist map[string][]c.SMsg
rhlen int
2024-05-31 13:34:54 +00:00
logCh chan<- logMsg
2024-01-14 17:59:51 +00:00
nickm map[string]string
}
2024-05-31 13:34:54 +00:00
type logMsg struct {
Ch string
Msg c.SMsg
}
2024-01-15 00:58:16 +00:00
type args struct {
Admin string `arg:"-a" default:"8bit" help:"admin user nick, allows access to /sudo" placeholder:"NICK"`
2024-05-31 13:34:54 +00:00
DB string `arg:"-d" default:"./go-chat.db" help:"sqlite database to store server data" placeholder:"FILE"`
2024-01-15 00:58:16 +00:00
HistLen uint `arg:"-l" default:"10" help:"set message history size" placeholder:"N"`
Port uint `arg:"positional" default:"0" help:"port to listen on, random available port if not set"`
NickMap *string `arg:"-n" help:"path to nick:pass JSON file" placeholder:"FILE"`
}
2024-05-31 14:11:30 +00:00
const createRoomTable = "CREATE TABLE IF NOT EXISTS %s (tim DATETIME, id TEXT, msg TEXT)"
const insertRoomMsg = "INSERT INTO %v (tim, id, msg) VALUES (:tim, :id, :msg)"
2024-01-15 00:58:16 +00:00
func (a *args) Version() string {
return "v0.2.3"
2024-01-15 00:58:16 +00:00
}
func (a *args) Description() string {
return "Go, chat! Server\nA basic irc-style chat server, written in Go using websockets"
}
2024-01-14 17:59:51 +00:00
func main() {
log := log.New(os.Stderr, "ws server 🚀 ", log.LstdFlags|log.Lshortfile|log.Lmsgprefix)
2024-01-15 00:58:16 +00:00
var args args
2024-01-14 17:59:51 +00:00
arg.MustParse(&args)
nickMap, err := loadNickMap(args.NickMap)
if err != nil {
log.Fatal(err)
}
2024-05-31 13:34:54 +00:00
err = run("localhost:"+fmt.Sprint(args.Port), nickMap, args.Admin, int(args.HistLen), log, args.DB)
2024-01-14 17:59:51 +00:00
if err != nil {
log.Fatal(err)
}
}
2024-05-31 13:34:54 +00:00
func run(addr string, nickMap map[string]string, admin string, rhlen int, log *log.Logger, dbPath string) error {
2024-01-14 17:59:51 +00:00
listener, err := net.Listen("tcp4", addr)
if err != nil {
return err
}
log.Printf("listening on ws://%v", listener.Addr())
2024-05-31 13:34:54 +00:00
db, rooms, rhist, err := loadDb(dbPath, rhlen)
if err != nil {
return err
}
logCh := make(chan logMsg, 128)
2024-05-31 13:57:24 +00:00
defer close(logCh)
2024-05-31 13:34:54 +00:00
go logMessage(db, rooms, logCh, log)
2024-01-14 17:59:51 +00:00
server := &http.Server{
Handler: server{
admin: admin,
2024-05-31 13:34:54 +00:00
dbase: db,
2024-01-14 17:59:51 +00:00
logFn: log.Printf,
conns: &conns{cm: make(map[*ws.Conn]user)},
2024-05-31 13:34:54 +00:00
rooms: rooms,
rhist: rhist,
2024-01-14 17:59:51 +00:00
rhlen: rhlen,
2024-05-31 13:34:54 +00:00
logCh: logCh,
2024-01-14 17:59:51 +00:00
nickm: nickMap,
},
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
errch := make(chan error, 1)
go func() {
errch <- server.Serve(listener)
}()
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
select {
case err := <-errch:
log.Printf("failed to serve: %v", err)
case signal := <-signals:
log.Printf("quitting: %v", signal)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
return server.Shutdown(ctx)
}
func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.ProtoAtLeast(1, 1) && !hasUpgradeHeader(r.Header) {
http.Redirect(w, r, "https://github.com/supleed2/go-chat", http.StatusSeeOther)
return
}
2024-01-14 17:59:51 +00:00
ctx := r.Context()
conn, err := ws.Accept(w, r, nil)
if err != nil {
s.logFn("%v", err)
return
}
defer conn.CloseNow()
if conn.Subprotocol() != "" {
return
}
port := strings.Split(r.RemoteAddr, ":")[1]
s.conns.sm.Lock()
s.conns.cm[conn] = user{room: "general", nick: port}
s.conns.sm.Unlock()
defer func() {
s.conns.sm.Lock()
delete(s.conns.cm, conn)
s.logFn("Remaining connections: %v", len(s.conns.cm))
s.conns.sm.Unlock()
}()
s.logFn("connected: %v", r.RemoteAddr)
for i := range s.rhist["general"] {
wsjson.Write(ctx, conn, s.rhist["general"][i])
}
cmsg := c.CMsg{}
smsg := c.SMsg{Id: port}
for {
err := func(ctx context.Context, conn *ws.Conn) error {
err := wsjson.Read(ctx, conn, &cmsg)
if err != nil {
return err
}
switch cmsg.Typ {
case c.Sudo:
s.logFn("(%v) sudo: %v", smsg.Id, cmsg.Msg)
if smsg.Id == s.admin {
cmd := strings.Split(cmsg.Msg, " ")
if len(cmd) == 2 {
2024-05-31 13:35:50 +00:00
if cmd[0] == "mk" && cmd[1] != "rooms" {
2024-01-14 17:59:51 +00:00
if _, ok := s.rooms[cmd[1]]; ok {
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("Room exists: %v", cmd)})
} else {
2024-05-31 13:34:54 +00:00
s.dbase.Exec("INSERT INTO rooms (name) VALUES ($1)", cmd[1])
2024-05-31 14:11:30 +00:00
s.dbase.Exec(fmt.Sprintf(createRoomTable, cmd[1]))
s.rooms[cmd[1]] = fmt.Sprintf(insertRoomMsg, cmd[1])
2024-05-31 13:34:54 +00:00
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("Created room: %v", cmd[1])})
2024-01-14 17:59:51 +00:00
}
} else if cmd[0] == "rm" {
if _, ok := s.rooms[cmd[1]]; ok && cmd[1] != "general" {
delete(s.rooms, cmd[1])
2024-05-31 13:34:54 +00:00
s.dbase.Exec("DELETE FROM rooms WHERE name = $1", cmd[1])
2024-01-14 17:59:51 +00:00
s.rhist[cmd[1]] = []c.SMsg{}
tim := time.Now()
s.conns.sm.Lock()
for cn, r := range s.conns.cm {
if r.room == cmd[1] {
r.room = "general"
s.conns.cm[cn] = r
wsjson.Write(ctx, cn, c.SMsg{Tim: tim, Id: "system", Msg: "room deleted, reconnected to general"})
}
}
s.conns.sm.Unlock()
2024-05-31 13:35:50 +00:00
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("Deleted room: %v", cmd[1])})
2024-01-14 17:59:51 +00:00
} else {
2024-05-31 13:35:50 +00:00
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("Room does not exist: %v", cmd[1])})
2024-01-14 17:59:51 +00:00
}
} else if cmd[0] == "yeet" {
found := false
s.conns.sm.Lock()
for c, r := range s.conns.cm {
if r.nick == cmd[1] {
c.Close(ws.StatusNormalClosure, "Kicked")
found = true
break
}
}
s.conns.sm.Unlock()
if found {
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("Yeet: %v", cmd[1])})
} else {
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("Not found: %v", cmd[1])})
}
} else {
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("Invalid command: %v", cmd)})
}
} else if cmd[0] == "wc" {
s.conns.sm.Lock()
wc := len(s.conns.cm)
s.conns.sm.Unlock()
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("Online: %v", wc)})
} else if cmd[0] == "man" {
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: "Available commands: man, mk, rm, wc, yeet"})
} else {
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("Invalid command: %v", cmd)})
}
} else {
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: "Unrecognised command, use /man for more info"})
}
case c.Echo:
s.logFn("(%v) echo: %v", smsg.Id, cmsg.Msg)
s.conns.sm.Lock()
room := s.conns.cm[conn].room
s.conns.sm.Unlock()
smsg.Tim = time.Now()
smsg.Msg = cmsg.Msg
2024-05-31 13:34:54 +00:00
s.logCh <- logMsg{room, smsg}
2024-01-14 17:59:51 +00:00
if len(s.rhist[room]) < s.rhlen {
s.rhist[room] = append(s.rhist[room], smsg)
} else {
s.rhist[room] = append(s.rhist[room][1:], smsg)
}
s.conns.sm.Lock()
for c, r := range s.conns.cm {
if r.room == room {
wsjson.Write(ctx, c, &smsg)
}
}
s.conns.sm.Unlock()
case c.Mv:
switch nick, valid := verifyNick(&s, cmsg.Msg); valid {
case nickOk:
s.logFn("(%v) mv: %v", smsg.Id, cmsg.Msg)
smsg.Id = nick
s.conns.sm.Lock()
u := s.conns.cm[conn]
u.nick = smsg.Id
s.conns.cm[conn] = u
s.conns.sm.Unlock()
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("nick set: %v", nick)})
case nickUsed:
s.logFn("(%v) mv used: %v", smsg.Id, cmsg.Msg)
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("nick in use: %v", cmsg.Msg)})
case nickInvalid:
s.logFn("(%v) mv invalid: %v", smsg.Id, cmsg.Msg)
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("invalid nick: %v", cmsg.Msg)})
}
case c.Ls:
s.logFn("(%v) ls", smsg.Id)
s.conns.sm.Lock()
room := s.conns.cm[conn].room
s.conns.sm.Unlock()
avRooms := ""
for r := range s.rooms {
avRooms += r + ", "
}
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("connected to: %v, available: %v", room, avRooms[:len(avRooms)-2])})
case c.Cd:
if _, ok := s.rooms[cmsg.Msg]; ok {
s.logFn("(%v) cd: %v", smsg.Id, cmsg.Msg)
s.conns.sm.Lock()
u := s.conns.cm[conn]
u.room = cmsg.Msg
s.conns.cm[conn] = u
s.conns.sm.Unlock()
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("connected to: %v", u.room)})
2024-05-31 13:35:50 +00:00
recentHistory := s.rhist[u.room]
2024-01-14 17:59:51 +00:00
for i := range recentHistory {
wsjson.Write(ctx, conn, recentHistory[i])
}
} else {
s.logFn("(%v) cd invalid: %v", smsg.Id, cmsg.Msg)
wsjson.Write(ctx, conn, c.SMsg{Tim: time.Now(), Id: "system", Msg: fmt.Sprintf("unchanged, invalid room: %v", cmsg.Msg)})
}
case c.Who:
s.conns.sm.Lock()
room := s.conns.cm[conn].room
s.logFn("(%v) who: %v", smsg.Id, room)
users := fmt.Sprintf("users in %v: ", room)
for _, r := range s.conns.cm {
if r.room == room {
users += fmt.Sprintf("%v, ", r.nick)
}
}
s.conns.sm.Unlock()
wsjson.Write(ctx, conn, &c.SMsg{Tim: time.Now(), Id: "system", Msg: users[:len(users)-2]})
}
return nil
}(ctx, conn)
if ws.CloseStatus(err) == ws.StatusNormalClosure {
s.logFn("disconnected: %v", r.RemoteAddr)
return
}
if err != nil {
s.logFn("failed, addr %v: %v", r.RemoteAddr, err)
return
}
}
}
2024-05-31 13:34:54 +00:00
func loadDb(path string, rhlen int) (*sqlx.DB, map[string]string, map[string][]c.SMsg, error) {
2024-05-31 14:11:30 +00:00
db, err := sqlx.Connect("sqlite", path)
if err != nil {
return nil, nil, nil, err
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS rooms (name TEXT)")
if err != nil {
return nil, nil, nil, err
}
roomList := []string{}
err = db.Select(&roomList, "SELECT * FROM rooms")
if err != nil {
return nil, nil, nil, err
}
if len(roomList) == 0 {
roomList = []string{"general", "test1", "test2"}
_, err = db.Exec("INSERT INTO rooms (name) VALUES ('general'), ('test1'), ('test2')")
if err != nil {
return nil, nil, nil, err
}
}
rooms := make(map[string]string)
rhist := make(map[string][]c.SMsg)
for _, room := range roomList {
rooms[room] = fmt.Sprintf(insertRoomMsg, room)
_, err = db.Exec(fmt.Sprintf(createRoomTable, room))
if err != nil {
return nil, nil, nil, err
}
roomHistory := []c.SMsg{}
err = db.Select(&roomHistory, fmt.Sprintf("SELECT * FROM %s ORDER BY tim DESC LIMIT %d", room, rhlen))
if err != nil {
return nil, nil, nil, err
}
slices.Reverse(roomHistory)
rhist[room] = roomHistory
}
return db, rooms, rhist, nil
2024-05-31 13:34:54 +00:00
}
2024-01-14 17:59:51 +00:00
type nickErr int
const (
nickOk nickErr = iota
nickUsed
nickInvalid
)
func verifyNick(s *server, n string) (string, nickErr) {
nick, pass, _ := strings.Cut(n, ":")
s.conns.sm.Lock()
defer s.conns.sm.Unlock()
for _, u := range s.conns.cm {
if u.nick == nick {
return "", nickUsed
}
}
expPass, needAuth := s.nickm[nick]
if (!needAuth || pass == expPass) && alphanumeric(nick) {
return nick, nickOk
} else {
return "", nickInvalid
}
}
func loadNickMap(m *string) (map[string]string, error) {
nm := make(map[string]string)
if m == nil {
return nm, nil
}
path, err := filepath.Abs(*m)
if err != nil {
return nil, err
}
file, err := os.ReadFile(path)
if err != nil {
return nil, err
}
err = json.Unmarshal(file, &nm)
if err != nil {
return nil, err
}
return nm, nil
}
func alphanumeric(s string) bool {
for _, r := range s {
if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') && (r < '0' || r > '9') {
return false
}
}
return true
}
func hasUpgradeHeader(h http.Header) bool {
for _, v := range h["Connection"] {
v = strings.TrimSpace(v)
for _, t := range strings.Split(v, ",") {
t = strings.TrimSpace(t)
if strings.EqualFold(t, "Upgrade") {
return true
}
}
}
return false
}
2024-05-31 13:34:54 +00:00
func logMessage(db *sqlx.DB, rooms map[string]string, logCh <-chan logMsg, log *log.Logger) {
for msg := range logCh {
if _, err := db.NamedExec(rooms[msg.Ch], msg.Msg); err != nil {
log.Println("logMessage:", err)
}
}
}