2023-07-28 13:09:54 +00:00
|
|
|
// really stupid test door server
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-29 22:03:52 +00:00
|
|
|
"context"
|
2023-07-28 13:09:54 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-07-29 22:03:52 +00:00
|
|
|
"log"
|
2023-07-28 13:09:54 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2023-07-29 22:03:52 +00:00
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-07-28 22:22:26 +00:00
|
|
|
"time"
|
2023-07-28 13:09:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DoorBoyServer struct {
|
|
|
|
longPollers map[string]chan string
|
|
|
|
}
|
|
|
|
|
|
|
|
type card struct {
|
|
|
|
UidHash string `json:"uid_hash"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type cardToken struct {
|
|
|
|
Token card `json:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type cardList struct {
|
2023-07-29 22:03:52 +00:00
|
|
|
AllowedUids []cardToken `json:"allowed_uids"`
|
|
|
|
KeepOpenUntil *time.Time `json:"keep_open_until,omitempty"`
|
2023-07-28 13:09:54 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 22:03:52 +00:00
|
|
|
var keys []cardToken = []cardToken{
|
|
|
|
//{card{UidHash: "d72c87d0f077c7766f2985dfab30e8955c373a13a1e93d315203939f542ff86e73ee37c31f4c4b571f4719fa8e3589f12db8dcb57ea9f56764bb7d58f64cf705"}},
|
|
|
|
{card{UidHash: "873636abbcf597a4835afc1c9b16a72eb6175b7ab278a8f18ab13c50172c90ea97cc3e258efd9cc1d885d7ea32d87fd006907892793e7cd6c468417bd8b8421a"}},
|
|
|
|
{card{UidHash: "a08094343b4057777af4935b79df586d7eb999117883a53393e8b46f1ab19577b12039a5d2e0b9d0364bbed5c82d83a507492fea47ace633acf23da2dcf1560e"}},
|
|
|
|
{card{UidHash: "d78271547cde009726b159dca09e53bee72feebe90b3eb7cb6e394caafc30bdb1f1567efc2f19bbdf3c6922e0bebed910ee4fa4f5b13bd379651da4f620f3559"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyLock sync.RWMutex
|
|
|
|
|
|
|
|
var keepOpenUntil *time.Time
|
|
|
|
|
2023-07-28 13:09:54 +00:00
|
|
|
func getRoot(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Printf("got / request\n")
|
|
|
|
io.WriteString(w, "This is my website!\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllowed(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-07-29 22:03:52 +00:00
|
|
|
keyLock.RLock()
|
2023-07-28 13:09:54 +00:00
|
|
|
keys := cardList{
|
2023-07-29 22:03:52 +00:00
|
|
|
AllowedUids: keys,
|
|
|
|
KeepOpenUntil: keepOpenUntil,
|
2023-07-28 13:09:54 +00:00
|
|
|
}
|
|
|
|
respJson, _ := json.Marshal(keys)
|
2023-07-29 22:03:52 +00:00
|
|
|
keyLock.RUnlock()
|
2023-07-28 13:09:54 +00:00
|
|
|
_, err := w.Write(respJson)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeAndFlush(writer http.ResponseWriter, buffer []byte) error {
|
|
|
|
_, err := writer.Write(buffer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if f, ok := writer.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (doorboyserver *DoorBoyServer) getLongPoll(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := writeAndFlush(w, []byte("data: response-generator-started\n\n"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
events := make(chan string)
|
|
|
|
doorboyserver.longPollers[r.RemoteAddr] = events
|
2023-07-28 22:22:26 +00:00
|
|
|
defer delete(doorboyserver.longPollers, r.RemoteAddr)
|
|
|
|
|
2023-07-28 13:09:54 +00:00
|
|
|
err = writeAndFlush(w, []byte("data: watch-stream-opened\n\n"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d := <-events // get door open event
|
|
|
|
_, err = w.Write([]byte("data: "))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = w.Write([]byte(d))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = writeAndFlush(w, []byte("\n\n"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-07-28 22:22:26 +00:00
|
|
|
}
|
2023-07-28 13:09:54 +00:00
|
|
|
|
2023-07-28 22:22:26 +00:00
|
|
|
func (doorboyserver *DoorBoyServer) getLongPollJson(w http.ResponseWriter, r *http.Request) {
|
|
|
|
flusher, ok := w.(http.Flusher)
|
|
|
|
if !ok {
|
|
|
|
fmt.Println("Failed to get flusher for http response")
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
flusher.Flush()
|
|
|
|
respJson, _ := json.Marshal([]string{"123456789", "aabbccddeeff", "aabbcc"})
|
|
|
|
for i := 1; i <= 10; i++ {
|
|
|
|
w.Write(respJson)
|
|
|
|
w.Write([]byte("\n"))
|
|
|
|
fmt.Println("send")
|
|
|
|
flusher.Flush() // Trigger "chunked" encoding and send a chunk...
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
2023-07-28 13:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (doobserver *DoorBoyServer) postOpenDoor(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Println("longPollers:")
|
|
|
|
for key, value := range doobserver.longPollers {
|
|
|
|
fmt.Println(" Key:", key, "Value:", value)
|
|
|
|
value <- "workshop"
|
|
|
|
}
|
|
|
|
w.Write([]byte("OK"))
|
|
|
|
}
|
|
|
|
|
2023-07-29 17:04:19 +00:00
|
|
|
func postCardSwipe(w http.ResponseWriter, r *http.Request) {
|
2023-07-29 22:03:52 +00:00
|
|
|
data, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Got swipe request without data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("Swipe Request with key:", string(data))
|
|
|
|
w.Write([]byte("OK"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (doobserver *DoorBoyServer) postAddCard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Got swipe request without data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cardHex := string(data)
|
|
|
|
if len(cardHex) != 128 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("Key invalid"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keyLock.RLock()
|
|
|
|
found := false
|
|
|
|
for _, key := range keys {
|
|
|
|
if key.Token.UidHash == cardHex {
|
|
|
|
// key is already in there!
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keyLock.RUnlock()
|
|
|
|
if found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keyLock.Lock()
|
|
|
|
keys = append(keys, cardToken{Token: card{UidHash: cardHex}})
|
|
|
|
keyLock.Unlock()
|
|
|
|
|
|
|
|
for _, datastream := range doobserver.longPollers {
|
|
|
|
datastream <- "update"
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Added new key:", string(data))
|
|
|
|
}
|
2023-07-29 17:04:19 +00:00
|
|
|
|
2023-07-29 22:03:52 +00:00
|
|
|
func (doobserver *DoorBoyServer) postRemoveCard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Got swipe request without data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cardHex := string(data)
|
|
|
|
if len(cardHex) != 128 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("Key invalid"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keyLock.Lock()
|
|
|
|
foundIndex := -1
|
|
|
|
for i, key := range keys {
|
|
|
|
if key.Token.UidHash == cardHex {
|
|
|
|
// key is already in there!
|
|
|
|
foundIndex = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if foundIndex == -1 {
|
|
|
|
keyLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if foundIndex == len(keys)-1 {
|
|
|
|
keys = keys[:foundIndex]
|
|
|
|
} else {
|
|
|
|
keys = append(keys[:foundIndex], keys[foundIndex+1:]...)
|
|
|
|
}
|
|
|
|
keyLock.Unlock()
|
|
|
|
|
|
|
|
for _, datastream := range doobserver.longPollers {
|
|
|
|
datastream <- "update"
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Remove key:", string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (doobserver *DoorBoyServer) postKeepDoorOpen(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rs, _ := io.ReadAll(r.Body)
|
|
|
|
parsedTime, err := time.Parse(time.RFC3339, strings.TrimSpace(string(rs)))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error with parsing time: %v", err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
keepOpenUntil = &parsedTime
|
|
|
|
doobserver.postOpenDoor(w, r)
|
2023-07-29 17:04:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 13:09:54 +00:00
|
|
|
func main() {
|
2023-07-29 22:03:52 +00:00
|
|
|
fmt.Println("Running...")
|
|
|
|
|
|
|
|
var dumpKeys []cardToken
|
|
|
|
savedKeys, err := os.ReadFile("keys.json")
|
|
|
|
|
|
|
|
err = json.Unmarshal(savedKeys, &dumpKeys)
|
|
|
|
if err == nil {
|
|
|
|
for _, k := range dumpKeys {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
log.Println("Loaded keys successfully!")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-07-28 13:09:54 +00:00
|
|
|
doorboyserver := DoorBoyServer{longPollers: map[string]chan string{}}
|
|
|
|
http.HandleFunc("/", getRoot)
|
|
|
|
http.HandleFunc("/allowed", getAllowed)
|
|
|
|
http.HandleFunc("/longpoll", doorboyserver.getLongPoll)
|
2023-07-28 22:22:26 +00:00
|
|
|
http.HandleFunc("/jspoll", doorboyserver.getLongPollJson)
|
2023-07-28 13:09:54 +00:00
|
|
|
http.HandleFunc("/open", doorboyserver.postOpenDoor)
|
2023-07-29 17:04:19 +00:00
|
|
|
http.HandleFunc("/cardswipe", postCardSwipe)
|
2023-07-29 22:03:52 +00:00
|
|
|
http.HandleFunc("/keepdooropen", doorboyserver.postKeepDoorOpen)
|
|
|
|
http.HandleFunc("/addcard", doorboyserver.postAddCard)
|
|
|
|
http.HandleFunc("/removecard", doorboyserver.postRemoveCard)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := http.ListenAndServe(":3333", nil)
|
2023-07-28 13:09:54 +00:00
|
|
|
|
2023-07-29 22:03:52 +00:00
|
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
|
|
fmt.Printf("server closed\n")
|
|
|
|
} else if err != nil {
|
|
|
|
fmt.Printf("error starting server: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
2023-07-28 13:09:54 +00:00
|
|
|
|
2023-07-29 22:03:52 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
log.Println("Shutting down!")
|
|
|
|
keyLock.Lock()
|
|
|
|
data, err := json.Marshal(keys)
|
|
|
|
if err == nil {
|
|
|
|
os.WriteFile("keys.json", data, 0600)
|
|
|
|
log.Println("Saved keys successfully!")
|
2023-07-28 13:09:54 +00:00
|
|
|
}
|
2023-07-29 22:03:52 +00:00
|
|
|
keyLock.Unlock()
|
2023-07-28 13:09:54 +00:00
|
|
|
}
|