Files
slidetogether/main.go

671 lines
13 KiB
Go
Raw Normal View History

2020-05-03 22:51:16 +00:00
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"path"
"sync"
"time"
_ "net/http/pprof"
"github.com/google/uuid"
)
type activeRequest struct {
RoomId string `json:"room_id"`
AdminSecret string `json:"admin_secret"`
2020-11-25 21:15:34 +00:00
ClientId string `json:"client_id"`
2020-05-03 22:51:16 +00:00
Active bool `json:"active"`
}
type adminRequest struct {
RoomId string `json:"room_id"`
AdminSecret string `json:"admin_secret"`
2020-11-25 21:15:34 +00:00
ClientId string `json:"client_id"`
2020-05-03 22:51:16 +00:00
}
2020-06-27 21:18:19 +00:00
type resetRequest struct {
RoomId string `json:"room_id"`
AdminSecret string `json:"admin_secret"`
}
2020-05-03 22:51:16 +00:00
type announceRequest struct {
RoomId string `json:"room_id"`
ClientId string `json:"client_id"`
AdminSecret string `json:"admin_secret"`
Name string `json:"name"`
}
type controlRequest struct {
RoomId string `json:"room_id"`
ClientId string `json:"client_id"`
Control string `json:"control"`
}
type createResponse struct {
RoomId string `json:"room_id"`
AdminSecret string `json:"admin_secret"`
}
type removeRequest struct {
RoomId string `json:"room_id"`
ClientId string `json:"client_id"`
}
type client struct {
2020-11-25 21:15:34 +00:00
ClientId string `json:"client_id"`
2020-05-03 22:51:16 +00:00
Name string `json:"name"`
Admin bool `json:"admin"`
Active bool `json:"active"`
2020-06-27 21:48:27 +00:00
ActiveStart int64 `json:"active_start"`
2020-05-03 22:51:16 +00:00
room *room
lastSeen time.Time
eventChan chan *event
}
type event struct {
AdminEvent *adminEvent `json:"admin_event"`
StandardEvent *standardEvent `json:"standard_event"`
}
type adminEvent struct {
Client *client `json:"client"`
Remove bool `json:"remove"`
}
type standardEvent struct {
Active bool `json:"active"`
2020-06-27 21:48:27 +00:00
ActiveStart int64 `json:"active_start"`
2020-06-27 21:18:19 +00:00
TimerStart int64 `json:"timer_start"`
2020-05-03 22:51:16 +00:00
AdminSecret string `json:"admin_secret"`
}
type controlEvent struct {
Control string `json:"control"`
}
type room struct {
roomId string
2020-06-27 21:18:19 +00:00
timerStart time.Time
2020-05-03 22:51:16 +00:00
clientById map[string]*client
present map[*presentState]bool
}
type presentState struct {
responseWriter http.ResponseWriter
flusher http.Flusher
room *room
controlChan chan *controlEvent
}
var key []byte
var roomById = map[string]*room{}
var mu = sync.Mutex{}
func main() {
rand.Seed(time.Now().UnixNano())
keyFlag := flag.String("key", "", "secret key")
bindFlag := flag.String("bind", ":2000", "host:port to listen on")
2020-05-03 22:51:16 +00:00
flag.Parse()
if *keyFlag == "" {
log.Fatalf("please specify --key (suggestion: %x)", rand.Uint64())
}
key = []byte(*keyFlag)
go scanLoop()
registerFile("/", "index.html")
registerFile("/remote.js", "remote.js")
registerFile("/remote.css", "remote.css")
http.HandleFunc("/api/active", active)
http.HandleFunc("/api/admin", admin)
http.HandleFunc("/api/announce", announce)
http.HandleFunc("/api/control", control)
http.HandleFunc("/api/create", create)
http.HandleFunc("/api/present", present)
http.HandleFunc("/api/remove", remove)
2020-06-27 21:18:19 +00:00
http.HandleFunc("/api/reset", reset)
2020-05-03 22:51:16 +00:00
http.HandleFunc("/api/watch", watch)
server := http.Server{
Addr: *bindFlag,
2020-05-03 22:51:16 +00:00
}
err := server.ListenAndServe()
if err != nil {
log.Fatalf("ListenAndServe(): %s", err)
}
}
func registerFile(urlPath, filename string) {
http.HandleFunc(urlPath, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == urlPath {
serveStatic(w, r, path.Join("static", filename))
} else {
w.WriteHeader(404)
}
})
}
func serveStatic(resp http.ResponseWriter, req *http.Request, path string) {
resp.Header().Set("Cache-Control", "public, max-age=3600")
http.ServeFile(resp, req, path)
}
func scanLoop() {
ticker := time.NewTicker(5 * time.Second)
for {
<-ticker.C
scan()
}
}
func scan() {
mu.Lock()
defer mu.Unlock()
grace := 10 * time.Second
2020-05-03 22:51:16 +00:00
for _, rm := range roomById {
for _, c := range rm.clientById {
2020-11-25 20:13:39 +00:00
if time.Now().Sub(c.lastSeen) > grace {
2020-05-03 22:51:16 +00:00
c.remove()
}
}
}
}
func active(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
req := &activeRequest{}
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rm := getRoom(req.RoomId)
if req.AdminSecret != rm.adminSecret() {
http.Error(w, "invalid admin_secret", http.StatusBadRequest)
return
}
2020-11-25 21:15:34 +00:00
c := rm.clientById[req.ClientId]
2020-05-03 22:51:16 +00:00
if c == nil {
2020-11-25 21:15:34 +00:00
http.Error(w, "invalid client_id", http.StatusBadRequest)
2020-05-03 22:51:16 +00:00
return
}
c.Active = req.Active
2020-06-27 21:48:27 +00:00
if c.Active {
c.ActiveStart = time.Now().Unix()
} else {
c.ActiveStart = 0
}
2020-05-03 22:51:16 +00:00
c.update()
rm.sendAdminEvent(&adminEvent{
Client: c,
})
}
func admin(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
req := &adminRequest{}
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rm := getRoom(req.RoomId)
if req.AdminSecret != rm.adminSecret() {
http.Error(w, "invalid admin_secret", http.StatusBadRequest)
return
}
2020-11-25 21:15:34 +00:00
c := rm.clientById[req.ClientId]
2020-05-03 22:51:16 +00:00
if c == nil {
2020-11-25 21:15:34 +00:00
http.Error(w, "invalid client_id", http.StatusBadRequest)
2020-05-03 22:51:16 +00:00
return
}
c.Admin = true
c.update()
rm.sendAdminEvent(&adminEvent{
Client: c,
})
}
func announce(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
req := &announceRequest{}
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rm := getRoom(req.RoomId)
admin := false
if req.AdminSecret != "" {
if req.AdminSecret == rm.adminSecret() {
admin = true
} else {
http.Error(w, "invalid admin_secret", http.StatusBadRequest)
return
}
}
c := rm.getClient(req.ClientId)
changed := false
if c.Name != req.Name {
c.Name = req.Name
changed = true
}
if c.Admin != admin {
c.Admin = admin
changed = true
}
if changed {
rm.sendAdminEvent(&adminEvent{
Client: c,
})
}
}
func control(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
req := &controlRequest{}
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rm := getRoom(req.RoomId)
c := rm.getClient(req.ClientId)
if !c.Active {
http.Error(w, "client is not active", http.StatusBadRequest)
2020-11-25 22:17:29 +00:00
return
2020-05-03 22:51:16 +00:00
}
rm.sendControlEvent(&controlEvent{
Control: req.Control,
})
}
func create(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
w.Header().Set("Content-Type", "application/json")
resp := &createResponse{
RoomId: uuid.New().String(),
}
rm := newRoom(resp.RoomId)
resp.AdminSecret = rm.adminSecret()
enc := json.NewEncoder(w)
err := enc.Encode(resp)
if err != nil {
log.Fatal(err)
}
}
func present(w http.ResponseWriter, r *http.Request) {
ps := newPresentState(w, r)
if ps == nil {
return
}
closeChan := w.(http.CloseNotifier).CloseNotify()
ticker := time.NewTicker(5 * time.Second)
2020-05-03 22:51:16 +00:00
for {
select {
case <-closeChan:
ps.close()
return
case <-ticker.C:
mu.Lock()
ps.sendHeartbeat()
mu.Unlock()
case ctrl := <-ps.controlChan:
mu.Lock()
ps.sendEvent(ctrl)
mu.Unlock()
}
}
}
func remove(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
req := &removeRequest{}
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rm := getRoom(req.RoomId)
c := rm.getClient(req.ClientId)
c.remove()
}
2020-06-27 21:18:19 +00:00
func reset(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
req := &resetRequest{}
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rm := getRoom(req.RoomId)
if req.AdminSecret != rm.adminSecret() {
http.Error(w, "invalid admin_secret", http.StatusBadRequest)
return
}
rm.timerStart = time.Now()
rm.updateAllClients()
}
2020-05-03 22:51:16 +00:00
func watch(w http.ResponseWriter, r *http.Request) {
2020-11-25 21:52:30 +00:00
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming unsupported", http.StatusBadRequest)
return
}
client, eventChan := registerWatch(w, r)
if client == nil {
2020-05-03 22:51:16 +00:00
return
}
closeChan := w.(http.CloseNotifier).CloseNotify()
ticker := time.NewTicker(5 * time.Second)
2020-05-03 22:51:16 +00:00
2020-11-25 21:52:30 +00:00
writeInitial(client, w, flusher)
2020-05-03 22:51:16 +00:00
for {
select {
case <-closeChan:
2020-11-25 21:52:30 +00:00
close(eventChan)
2020-05-03 22:51:16 +00:00
mu.Lock()
2020-11-25 21:52:30 +00:00
if client.eventChan == eventChan {
client.eventChan = nil
}
2020-05-03 22:51:16 +00:00
mu.Unlock()
2020-11-25 21:52:30 +00:00
case <-ticker.C:
writeHeartbeat(w, flusher)
case event, ok := <-eventChan:
if ok {
writeEvent(event, w, flusher)
} else {
return
}
2020-05-03 22:51:16 +00:00
}
}
}
func (c *client) sendEvent(e *event) {
if c.eventChan != nil {
c.eventChan <- e
}
}
func (c *client) remove() {
if c.eventChan != nil {
close(c.eventChan)
}
2020-11-25 21:15:34 +00:00
delete(c.room.clientById, c.ClientId)
2020-05-03 22:51:16 +00:00
c.room.sendAdminEvent(&adminEvent{
Client: c,
Remove: true,
})
}
func (c *client) update() {
e := &event{
StandardEvent: &standardEvent{
Active: c.Active,
2020-06-27 21:48:27 +00:00
ActiveStart: c.ActiveStart,
2020-06-27 21:18:19 +00:00
TimerStart: c.room.timerStart.Unix(),
2020-05-03 22:51:16 +00:00
},
}
if c.Admin {
e.StandardEvent.AdminSecret = c.room.adminSecret()
}
c.sendEvent(e)
}
func newRoom(roomId string) *room {
return &room{
roomId: roomId,
2020-06-27 21:18:19 +00:00
timerStart: time.Now(),
2020-05-03 22:51:16 +00:00
clientById: map[string]*client{},
present: map[*presentState]bool{},
}
}
func getRoom(roomId string) *room {
r := roomById[roomId]
if r == nil {
r = newRoom(roomId)
roomById[roomId] = r
}
return r
}
func (rm *room) adminSecret() string {
h := hmac.New(sha256.New, key)
return base64.StdEncoding.EncodeToString(h.Sum([]byte(rm.roomId)))
}
func (rm *room) getClient(clientId string) *client {
c := rm.clientById[clientId]
if c == nil {
c = &client{
2020-11-25 21:15:34 +00:00
ClientId: clientId,
2020-05-03 22:51:16 +00:00
room: rm,
}
rm.clientById[clientId] = c
rm.sendAdminEvent(&adminEvent{
Client: c,
})
}
c.lastSeen = time.Now().UTC()
return c
}
func (rm *room) sendAdminEvent(ae *adminEvent) {
for _, client := range rm.clientById {
if !client.Admin {
continue
}
client.sendEvent(&event{
AdminEvent: ae,
})
}
}
func (rm *room) sendControlEvent(ce *controlEvent) {
for present, _ := range rm.present {
present.sendEvent(ce)
}
}
2020-06-27 21:18:19 +00:00
func (rm *room) updateAllClients() {
for _, client := range rm.clientById {
client.update()
}
}
2020-11-25 21:52:30 +00:00
func registerWatch(w http.ResponseWriter, r *http.Request) (*client, chan *event) {
2020-05-03 22:51:16 +00:00
mu.Lock()
defer mu.Unlock()
roomId := r.URL.Query().Get("room_id")
2020-11-25 21:52:30 +00:00
room := getRoom(roomId)
2020-05-03 22:51:16 +00:00
clientId := r.URL.Query().Get("client_id")
2020-11-25 21:52:30 +00:00
client := room.getClient(clientId)
2020-05-03 22:51:16 +00:00
adminSecret := r.URL.Query().Get("admin_secret")
if adminSecret != "" {
2020-11-25 21:52:30 +00:00
if adminSecret == room.adminSecret() {
client.Admin = true
2020-05-03 22:51:16 +00:00
} else {
http.Error(w, "invalid admin_secret", http.StatusBadRequest)
2020-11-25 21:52:30 +00:00
return nil, nil
2020-05-03 22:51:16 +00:00
}
}
2020-11-25 21:52:30 +00:00
if client.eventChan != nil {
close(client.eventChan)
}
client.eventChan = make(chan *event, 100)
client.update()
2020-05-03 22:51:16 +00:00
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
2020-11-25 21:52:30 +00:00
// Return eventChan because we're reading it with the lock held
return client, client.eventChan
2020-05-03 22:51:16 +00:00
}
2020-11-25 21:52:30 +00:00
func writeInitial(client *client, w http.ResponseWriter, flusher http.Flusher) {
2020-05-03 22:51:16 +00:00
mu.Lock()
defer mu.Unlock()
2020-11-25 21:52:30 +00:00
if !client.Admin {
2020-05-03 22:51:16 +00:00
return
}
2020-11-25 21:52:30 +00:00
for _, iter := range client.room.clientById {
writeEvent(&event{
2020-05-03 22:51:16 +00:00
AdminEvent: &adminEvent{
2020-11-25 21:52:30 +00:00
Client: iter,
2020-05-03 22:51:16 +00:00
},
2020-11-25 21:52:30 +00:00
}, w, flusher)
2020-05-03 22:51:16 +00:00
}
}
2020-11-25 21:52:30 +00:00
func writeHeartbeat(w http.ResponseWriter, flusher http.Flusher) {
fmt.Fprintf(w, ":\n\n")
flusher.Flush()
2020-05-03 22:51:16 +00:00
}
2020-11-25 21:52:30 +00:00
func writeEvent(e *event, w http.ResponseWriter, flusher http.Flusher) {
2020-05-03 22:51:16 +00:00
j, err := json.Marshal(e)
if err != nil {
log.Fatal(err)
}
2020-11-25 21:52:30 +00:00
fmt.Fprintf(w, "data: %s\n\n", j)
flusher.Flush()
2020-05-03 22:51:16 +00:00
}
func newPresentState(w http.ResponseWriter, r *http.Request) *presentState {
mu.Lock()
defer mu.Unlock()
ps := &presentState{
responseWriter: w,
controlChan: make(chan *controlEvent, 100),
}
var ok bool
ps.flusher, ok = w.(http.Flusher)
if !ok {
http.Error(ps.responseWriter, "streaming unsupported", http.StatusBadRequest)
return nil
}
roomId := r.URL.Query().Get("room_id")
ps.room = getRoom(roomId)
ps.room.present[ps] = true
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
return ps
}
func (ps *presentState) sendHeartbeat() {
fmt.Fprintf(ps.responseWriter, ":\n\n")
ps.flusher.Flush()
}
func (ps *presentState) sendEvent(e *controlEvent) {
j, err := json.Marshal(e)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(ps.responseWriter, "data: %s\n\n", j)
ps.flusher.Flush()
}
func (ps *presentState) close() {
mu.Lock()
defer mu.Unlock()
delete(ps.room.present, ps)
close(ps.controlChan)
}