vail/cmd/vail/main.go

137 lines
2.6 KiB
Go
Raw Normal View History

2020-04-09 23:09:33 -06:00
package main
import (
2020-05-30 22:23:53 -06:00
"fmt"
2020-04-09 23:09:33 -06:00
"log"
"net/http"
2022-05-15 15:57:12 -06:00
"os"
2022-06-05 13:34:03 -06:00
"strings"
2022-05-15 15:57:12 -06:00
"time"
2022-06-05 13:34:03 -06:00
"nhooyr.io/websocket"
2020-04-09 23:09:33 -06:00
)
var book Book
2020-04-12 20:40:52 -06:00
2022-05-15 17:38:57 -06:00
// Clock defines an interface for getting the current time.
//
// We use this in testing to provide a fixed value for the current time, so we
// can still compare clocks.
type Clock interface {
Now() time.Time
}
2022-06-05 13:34:03 -06:00
// WallClock is a Clock which provides the actual time
2022-05-15 17:38:57 -06:00
type WallClock struct{}
func (WallClock) Now() time.Time {
return time.Now()
}
2022-05-15 21:12:36 -06:00
// VailWebSocketConnection reads and writes Message structs
type VailWebSocketConnection struct {
*websocket.Conn
2022-06-05 13:34:03 -06:00
usingJSON bool
2022-05-15 21:12:36 -06:00
}
func (c *VailWebSocketConnection) Receive() (Message, error) {
var m Message
2022-06-05 13:34:03 -06:00
var err error
if c.usingJSON {
err = websocket.JSON.Receive(c.Conn, &m)
} else {
buf := make([]byte, 64)
if err := websocket.Message.Receive(c.Conn, &buf); err != nil {
return m, err
}
if err := m.UnmarshalBinary(buf)
}
2022-05-15 21:12:36 -06:00
return m, err
}
func (c *VailWebSocketConnection) Send(m Message) error {
2022-06-05 13:34:03 -06:00
if c.usingJSON {
return websocket.JSON.Send(c.Conn, m)
} else {
return websocket.Message.Send(c.Conn, m)
}
}
func (c *VailWebSocketConnection) Error(err error) {
msg := fmt.Sprintf("Error: %#v", err)
websocket.JSON.Send(c.Conn, msg)
2022-05-15 21:12:36 -06:00
}
2020-04-09 23:09:33 -06:00
type Client struct {
repeaterName string
2022-06-05 13:34:03 -06:00
usingJSON bool
2020-04-09 23:09:33 -06:00
}
func (c Client) Handle(ws *websocket.Conn) {
2022-06-05 13:34:03 -06:00
sock := &VailWebSocketConnection{
Conn: ws,
usingJSON: c.usingJSON,
}
2022-05-15 15:57:12 -06:00
nowMilli := time.Now().UnixMilli()
ws.MaxPayloadBytes = 50
2022-05-15 21:12:36 -06:00
book.Join(c.repeaterName, sock)
defer book.Part(c.repeaterName, sock)
2022-05-15 15:57:12 -06:00
for {
2022-05-15 21:12:36 -06:00
m, err := sock.Receive()
if err != nil {
2022-06-05 13:34:03 -06:00
sock.Error(err)
break
}
2022-05-15 15:57:12 -06:00
2022-05-15 21:12:36 -06:00
// If it's empty, skip it
if len(m.Duration) == 0 {
continue
2022-05-15 15:57:12 -06:00
}
// If it's wildly out of time, reject it
timeDelta := (nowMilli - m.Timestamp)
if timeDelta < 0 {
timeDelta = -timeDelta
}
if timeDelta > 9999 {
fmt.Fprintln(ws, "Bad timestamp")
ws.Close()
return
}
book.Send(c.repeaterName, m)
2020-04-09 23:09:33 -06:00
}
}
func ChatHandler(w http.ResponseWriter, r *http.Request) {
2022-05-15 15:57:12 -06:00
c := Client{
repeaterName: r.FormValue("repeater"),
2020-04-09 23:09:33 -06:00
}
2022-06-05 13:34:03 -06:00
accept := r.Header.Get("Accept")
if strings.Contains(accept, "json") {
c.usingJSON = true
}
// This API is confusing as hell.
// I suspect there's a better way to do this.
websocket.Handler(c.Handle).ServeHTTP(w, r)
2020-04-09 23:09:33 -06:00
}
func main() {
book = NewBook()
http.Handle("/chat", http.HandlerFunc(ChatHandler))
2020-04-09 23:09:33 -06:00
http.Handle("/", http.FileServer(http.Dir("static")))
go book.Run()
2020-05-17 12:52:25 -06:00
2020-05-03 09:37:50 -06:00
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Println("Listening on port", port)
2022-05-15 15:57:12 -06:00
err := http.ListenAndServe(":"+port, nil)
2020-04-09 23:09:33 -06:00
if err != nil {
log.Fatal(err.Error())
}
}