vail/cmd/vail/main.go

161 lines
3.4 KiB
Go
Raw Normal View History

2020-04-09 23:09:33 -06:00
package main
import (
2022-06-06 09:54:55 -06:00
"context"
"encoding/json"
2022-06-06 13:49:52 -06:00
"fmt"
2020-04-09 23:09:33 -06:00
"log"
"net/http"
2022-05-15 15:57:12 -06:00
"os"
"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-06-06 09:54:55 -06:00
const JsonProtocol = "json.vail.woozle.org"
const BinaryProtocol = "binary.vail.woozle.org"
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-06 09:54:55 -06:00
messageType, buf, err := c.Read(context.Background())
if err != nil {
return m, err
}
if messageType == websocket.MessageText {
err = json.Unmarshal(buf, &m)
2022-06-05 13:34:03 -06:00
} else {
2022-06-06 09:54:55 -06:00
err = m.UnmarshalBinary(buf)
2022-06-05 13:34:03 -06:00
}
2022-05-15 21:12:36 -06:00
return m, err
}
func (c *VailWebSocketConnection) Send(m Message) error {
2022-06-06 09:54:55 -06:00
var err error
var buf []byte
var messageType websocket.MessageType
2022-06-05 13:34:03 -06:00
if c.usingJSON {
2022-06-06 09:54:55 -06:00
messageType = websocket.MessageText
buf, err = json.Marshal(m)
2022-06-05 13:34:03 -06:00
} else {
2022-06-06 09:54:55 -06:00
messageType = websocket.MessageBinary
buf, err = m.MarshalBinary()
}
if err != nil {
return err
2022-06-05 13:34:03 -06:00
}
2022-06-06 09:54:55 -06:00
return c.Write(context.Background(), messageType, buf)
2022-05-15 21:12:36 -06:00
}
2022-06-06 09:54:55 -06:00
func ChatHandler(w http.ResponseWriter, r *http.Request) {
2022-06-06 13:49:52 -06:00
forwardedFor := r.Header.Get("X-Forwarded-For")
client := fmt.Sprintf("<%s|%s>", forwardedFor, r.RemoteAddr)
2022-06-06 09:54:55 -06:00
// Set up websocket
ws, err := websocket.Accept(
w, r,
&websocket.AcceptOptions{
Subprotocols: []string{JsonProtocol, BinaryProtocol},
},
)
if err != nil {
log.Println(err)
return
}
defer ws.Close(websocket.StatusInternalError, "Internal error")
2020-04-09 23:09:33 -06:00
2022-06-06 09:54:55 -06:00
// Create our Vail websocket connection for books to send to
sock := VailWebSocketConnection{
Conn: ws,
2022-06-05 13:34:03 -06:00
}
2022-06-06 09:54:55 -06:00
// websockets apparently sends a subprotocol string, so we can ignore Accept headers!
switch ws.Subprotocol() {
case JsonProtocol:
sock.usingJSON = true
case BinaryProtocol:
sock.usingJSON = false
default:
ws.Close(websocket.StatusPolicyViolation, "client must speak a vail protocol")
return
}
// Join the repeater
repeaterName := r.FormValue("repeater")
book.Join(repeaterName, &sock)
defer book.Part(repeaterName, &sock)
2022-05-15 15:57:12 -06:00
2022-06-06 13:49:52 -06:00
log.Println(client, repeaterName, "connect")
for {
2022-06-06 09:54:55 -06:00
// Read a packet
2022-05-15 21:12:36 -06:00
m, err := sock.Receive()
if err != nil {
2022-06-06 09:54:55 -06:00
ws.Close(websocket.StatusInvalidFramePayloadData, err.Error())
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 := time.Duration(time.Now().UnixMilli()-m.Timestamp) * time.Millisecond
2022-05-15 15:57:12 -06:00
if timeDelta < 0 {
timeDelta = -timeDelta
}
if timeDelta > 10*time.Second {
2022-06-06 09:54:55 -06:00
log.Println(err)
ws.Close(websocket.StatusInvalidFramePayloadData, "Your clock is off by too much")
break
2022-05-15 15:57:12 -06:00
}
2022-06-06 09:54:55 -06:00
book.Send(repeaterName, m)
2020-04-09 23:09:33 -06:00
}
2022-06-06 13:49:52 -06:00
log.Println(client, repeaterName, "disconnect")
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())
}
}