godoor/godoor.go

496 lines
11 KiB
Go
Raw Normal View History

2022-04-02 18:14:21 +00:00
package main
2022-04-03 17:34:46 +00:00
import (
2023-07-28 22:22:26 +00:00
"bufio"
2023-07-29 17:04:19 +00:00
"bytes"
2023-07-28 13:09:54 +00:00
"context"
2022-04-03 17:34:46 +00:00
"encoding/json"
"fmt"
2023-08-06 13:48:17 +00:00
"github.com/joho/godotenv"
2023-08-06 19:02:14 +00:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
2022-04-03 17:34:46 +00:00
"io"
2023-07-29 17:04:19 +00:00
"log"
2022-04-03 17:34:46 +00:00
"net/http"
2023-07-28 13:09:54 +00:00
"os"
"os/signal"
"runtime/debug"
2023-08-06 13:48:17 +00:00
"strconv"
2023-07-28 22:22:26 +00:00
"strings"
"sync"
2023-07-28 13:09:54 +00:00
"syscall"
2023-07-30 10:38:47 +00:00
"time"
"godoor/hash"
2022-04-03 17:34:46 +00:00
)
2022-04-02 18:14:21 +00:00
2023-08-06 13:48:17 +00:00
const wiegand_a_default = 17
const wiegand_b_default = 18
2022-04-03 13:50:21 +00:00
const wiegand_bit_timeout = time.Millisecond * 8
2023-08-06 13:48:17 +00:00
const solenoid_default = 21
2022-04-02 18:14:21 +00:00
2022-04-03 17:34:46 +00:00
type card struct {
UidHash string `json:"uid_hash"`
}
2023-07-28 13:09:54 +00:00
type cardList struct {
AllowedUids []struct {
Token card `json:"token"`
} `json:"allowed_uids"`
2023-07-29 22:03:52 +00:00
KeepOpenUntil *time.Time `json:"keep_open_until,omitempty"`
2022-04-03 17:34:46 +00:00
}
type ValidUids map[string]bool // bool has no meaning
2023-07-28 13:09:54 +00:00
type Config struct {
2023-07-29 17:04:19 +00:00
door string
uidSalt string
2023-08-06 13:48:17 +00:00
doorOpenTime int
2023-07-29 17:04:19 +00:00
mock bool
api struct {
2023-07-28 13:09:54 +00:00
allowed string
longpoll string
swipe string
key string
}
2023-08-06 13:48:17 +00:00
pins struct {
wiegandA int
wiegandB int
solenoid int
}
2023-08-06 19:02:14 +00:00
prometheusMetricsBind string
2023-07-28 13:09:54 +00:00
}
2023-07-29 22:03:52 +00:00
type KeepDoorOpen struct {
until time.Time
timer *time.Timer
}
2023-07-30 10:38:47 +00:00
type OpenedTimestamp struct {
Opened time.Time
Closed *time.Time
}
var Commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}
return ""
}()
var Version string
2023-07-28 13:09:54 +00:00
var config Config
2023-07-28 22:22:26 +00:00
var globalLock sync.Mutex
var validUids ValidUids
var wiegand Wiegand
2023-07-29 22:03:52 +00:00
var keepDoorOpen KeepDoorOpen
2023-07-28 13:09:54 +00:00
2023-08-06 19:02:14 +00:00
var (
godoorBuildInfo = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "godoor_build_info",
Help: "Build Information",
}, []string{"version", "revision"})
lastSyncTimestamp = promauto.NewGauge(prometheus.GaugeOpts{
Name: "godoor_last_allow_list_sync_timestamp_seconds",
Help: "Last time list of card hashes was pulled from the server",
})
apiFailuresCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "godoor_api_request_failures_total",
Help: "HTTP API request failures count",
}, []string{"api", "endpoint"})
nrCardsInAllowList = promauto.NewGauge(prometheus.GaugeOpts{
Name: "godoor_allowed_card_hashes_total",
Help: "Number of card hashes in memory that can open the door",
})
doorOpenedCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "godoor_door_opens_total",
Help: "Number of times door was opened",
}, []string{"source"})
cardSwipesCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "godoor_swipes_total",
Help: "Number of times a card has been swiped",
}, []string{"status"})
)
2023-07-30 10:38:47 +00:00
2022-04-02 18:14:21 +00:00
func main() {
log.Printf("GoDoor ver: %s (%s)", Version, Commit)
2023-07-28 13:09:54 +00:00
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
2023-08-06 13:48:17 +00:00
loadConfig()
2023-07-29 17:04:19 +00:00
2023-08-06 19:02:14 +00:00
godoorBuildInfo.WithLabelValues(Version, Commit).Set(1)
2023-07-29 17:04:19 +00:00
go func() {
setup()
}()
<-ctx.Done()
log.Printf("Cleanup\n")
2023-07-28 13:09:54 +00:00
2023-07-29 17:04:19 +00:00
// cleanup
}
2023-07-30 10:38:47 +00:00
2023-08-06 13:48:17 +00:00
func loadConfig() {
var err error
log.Println("Loading .env config")
err = godotenv.Load()
if err != nil {
log.Println("Failed to load .env config, using internal defaults")
}
config.door = os.Getenv("KDOORPI_DOOR")
config.api.allowed = os.Getenv("KDOORPI_API_ALLOWED")
config.api.longpoll = os.Getenv("KDOORPI_API_LONGPOLL")
config.api.swipe = os.Getenv("KDOORPI_API_SWIPE")
config.api.key = os.Getenv("KDOORPI_API_KEY")
config.uidSalt = os.Getenv("KDOORPI_UID_SALT")
config.doorOpenTime, err = strconv.Atoi(os.Getenv("KDOORPI_OPEN_TIME"))
if err != nil {
config.doorOpenTime = 3
}
_, config.mock = os.LookupEnv("KDOORPI_MOCK_HW")
2023-08-06 19:02:14 +00:00
config.prometheusMetricsBind = os.Getenv("KDOORPI_PROMETHEUS_METRICS_BIND")
if config.prometheusMetricsBind == "" {
config.prometheusMetricsBind = ":3334"
}
2023-08-06 13:48:17 +00:00
config.pins.wiegandA, err = strconv.Atoi(os.Getenv("KDOORPI_PIN_WIEGAND_A"))
if err != nil {
config.pins.wiegandA = wiegand_a_default
}
config.pins.wiegandB, err = strconv.Atoi(os.Getenv("KDOORPI_PIN_WIEGAND_B"))
if err != nil {
config.pins.wiegandB = wiegand_b_default
}
config.pins.solenoid, err = strconv.Atoi(os.Getenv("KDOORPI_PIN_SOLENOID"))
if err != nil {
config.pins.solenoid = solenoid_default
}
}
2023-07-29 17:04:19 +00:00
func setup() {
log.Println("Started Setup")
if config.mock {
log.Println("MOCK mode enabled")
2023-07-29 22:03:52 +00:00
if config.door == "" {
config.door = "mockdoor"
}
2023-07-28 22:22:26 +00:00
wiegand = &WiegandMock{}
} else {
2023-08-06 13:48:17 +00:00
wiegand = WiegandSetup(config.pins.wiegandA, config.pins.wiegandB, wiegand_bit_timeout, config.pins.solenoid)
2023-07-28 22:22:26 +00:00
}
2023-07-29 17:04:19 +00:00
log.Println("HW Setup done")
2023-08-06 19:02:14 +00:00
go runHttpServer()
2023-07-30 10:38:47 +00:00
http.DefaultClient.Timeout = 120 * time.Second
2023-07-28 22:22:26 +00:00
for {
2023-07-29 17:04:19 +00:00
log.Println("Start initial token population")
2023-07-28 22:22:26 +00:00
err := reloadTokens()
if err == nil {
break
}
2023-08-06 19:02:14 +00:00
apiFailuresCount.WithLabelValues("allowed", config.api.allowed).Inc()
2023-07-29 17:04:19 +00:00
log.Printf("Initial token population failed. err: %v", err)
log.Println("Retrying in 10 seconds...")
2023-07-28 22:22:26 +00:00
time.Sleep(10 * time.Second)
}
2023-07-28 13:09:54 +00:00
2023-07-29 17:04:19 +00:00
log.Println("Initial token population success")
go func() {
for {
err := waitEvents()
if err != nil {
apiFailuresCount.WithLabelValues("longpoll", config.api.longpoll).Inc()
log.Printf("LongPoll for events failed: %v", err)
log.Println("Will try to LongPoll again soon")
}
time.Sleep(1 * time.Second)
go func() {
err := reloadTokens()
if err != nil {
log.Printf("ReloadTokens failed: %q", err)
apiFailuresCount.WithLabelValues("allowed", config.api.allowed).Inc()
}
}()
}
}()
log.Println("Initialized longpoll event loop")
2023-07-28 22:22:26 +00:00
go cardRunner(wiegand)
2023-07-28 13:09:54 +00:00
2023-07-29 17:04:19 +00:00
log.Println("Setup completed")
}
2023-07-28 13:09:54 +00:00
2023-07-30 10:38:47 +00:00
func runHttpServer() {
2023-08-06 19:02:14 +00:00
http.Handle("/metrics", promhttp.Handler())
log.Printf("Running prometheus metrics on http://%s/metrics", config.prometheusMetricsBind)
log.Fatal(http.ListenAndServe(config.prometheusMetricsBind, nil))
2023-07-30 10:38:47 +00:00
}
2023-07-29 17:04:19 +00:00
func OpenAndCloseDoor(w Wiegand) error {
2023-07-30 10:38:47 +00:00
err := OpenDoor(w)
2023-07-29 17:04:19 +00:00
if err != nil {
return err
}
2023-07-29 22:03:52 +00:00
2023-07-30 10:38:47 +00:00
if keepDoorOpen.until.After(time.Now()) {
2023-07-29 22:03:52 +00:00
fmt.Println("Door is already open")
return nil
}
2023-07-29 17:04:19 +00:00
fmt.Println("Door is now open")
2023-07-28 13:09:54 +00:00
2023-08-06 13:48:17 +00:00
time.Sleep(time.Duration(config.doorOpenTime) * time.Second)
2023-07-28 13:09:54 +00:00
2023-07-30 10:38:47 +00:00
err = CloseDoor(w)
2023-07-29 17:04:19 +00:00
if err != nil {
return err
}
fmt.Println("Door is now closed")
return nil
2023-07-28 13:09:54 +00:00
}
2023-07-30 10:38:47 +00:00
func OpenDoor(w Wiegand) error {
open, _ := w.IsDoorOpen()
if open {
return nil
}
w.OpenDoor()
return nil
}
func CloseDoor(w Wiegand) error {
open, _ := w.IsDoorOpen()
if !open {
return nil
}
w.CloseDoor()
return nil
}
2023-07-28 22:22:26 +00:00
func cardRunner(w Wiegand) {
for {
card, err := w.GetCardUid()
if err != nil {
continue
}
//printCardId(card)
2023-07-30 10:38:47 +00:00
hashedHex := hash.HashCardUid(card)
2023-07-29 17:04:19 +00:00
log.Println(hashedHex)
2023-07-29 22:03:52 +00:00
globalLock.Lock()
ok := validUids[hashedHex]
globalLock.Unlock()
2023-07-29 17:04:19 +00:00
go func() {
2023-07-29 22:03:52 +00:00
err := sendSwipeEvent(hashedHex, ok)
2023-07-29 17:04:19 +00:00
if err != nil {
2023-08-06 19:02:14 +00:00
apiFailuresCount.WithLabelValues("swipe", config.api.swipe).Inc()
2023-07-29 17:04:19 +00:00
log.Println("Failed to send swipe event: %v", err)
}
}()
2023-07-28 22:22:26 +00:00
if ok {
2023-07-29 17:04:19 +00:00
log.Println("Opening door")
err := OpenAndCloseDoor(w)
2023-08-06 19:02:14 +00:00
cardSwipesCount.WithLabelValues("accepted").Inc()
doorOpenedCount.WithLabelValues("card").Inc()
2023-07-29 17:04:19 +00:00
if err != nil {
log.Println("There was an error opening and closing the Door")
}
2023-07-28 22:22:26 +00:00
} else {
2023-08-06 19:02:14 +00:00
cardSwipesCount.WithLabelValues("denied").Inc()
2023-07-29 17:04:19 +00:00
log.Println("Unknown card")
2023-07-28 22:22:26 +00:00
}
}
}
func ParseNextMessage(r *bufio.Reader) (string, error) {
var message string
for {
s, err := r.ReadString('\n')
if err != nil {
return message, err
}
message = message + s
nextBytes, err := r.ReadByte()
if err != nil {
return message, err
}
if nextBytes == '\n' {
return message, nil
}
r.UnreadByte()
}
}
func waitEvents() error {
2023-07-28 13:09:54 +00:00
req, err := http.NewRequest(http.MethodGet, config.api.longpoll, nil)
2022-04-03 17:34:46 +00:00
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2022-04-03 17:34:46 +00:00
}
2023-07-28 13:09:54 +00:00
req.Header.Add("KEY", config.api.key)
resp, err := http.DefaultClient.Do(req)
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2023-07-28 13:09:54 +00:00
}
2023-07-29 17:04:19 +00:00
log.Printf("%v\n", resp)
2023-07-28 13:09:54 +00:00
2023-07-28 22:22:26 +00:00
reader := bufio.NewReader(resp.Body)
for {
msg, err := ParseNextMessage(reader)
if err != nil {
2023-08-10 17:07:39 +00:00
if err == io.EOF {
return nil
}
2023-07-28 22:22:26 +00:00
return err
}
for _, line := range strings.Split(msg, "\n") {
data, found_data := strings.CutPrefix(line, "data:")
if !found_data {
continue
}
2023-07-29 17:04:19 +00:00
log.Printf("got server data: %q\n", data)
2023-07-28 22:22:26 +00:00
if strings.TrimSpace(data) == config.door {
2023-07-29 17:04:19 +00:00
err := OpenAndCloseDoor(wiegand)
2023-08-06 19:02:14 +00:00
doorOpenedCount.WithLabelValues("api").Inc()
2023-07-29 17:04:19 +00:00
if err != nil {
log.Println("There was an error opening and closing the Door")
}
2023-07-28 22:22:26 +00:00
}
}
2023-07-28 13:09:54 +00:00
}
2023-07-29 17:04:19 +00:00
log.Printf("%v\n", resp)
2023-07-28 22:22:26 +00:00
return nil
2023-07-28 13:09:54 +00:00
}
2023-07-28 22:22:26 +00:00
func reloadTokens() error {
2023-07-28 13:09:54 +00:00
req, err := http.NewRequest(http.MethodGet, config.api.allowed, nil)
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2023-07-28 13:09:54 +00:00
}
req.Header.Add("KEY", config.api.key)
resp, err := http.DefaultClient.Do(req)
2022-04-03 17:34:46 +00:00
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2022-04-03 17:34:46 +00:00
}
2023-08-03 08:03:43 +00:00
if resp.StatusCode != 200 {
log.Printf("%v\n", resp)
}
2022-04-03 17:34:46 +00:00
var cl cardList
body, err := io.ReadAll(resp.Body)
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2022-04-03 17:34:46 +00:00
}
err = json.Unmarshal(body, &cl)
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2022-04-03 17:34:46 +00:00
}
2023-07-28 22:22:26 +00:00
globalLock.Lock()
defer globalLock.Unlock()
validUids = make(ValidUids)
var totalCardCount int = 0
2022-04-03 17:34:46 +00:00
for i, val := range cl.AllowedUids {
2023-08-03 08:03:43 +00:00
//log.Printf("%d: %+v\n", i, val.Token.UidHash)
2023-07-28 22:22:26 +00:00
validUids[val.Token.UidHash] = true
totalCardCount = i
2022-04-03 17:34:46 +00:00
}
log.Printf("Got %d cards from server", totalCardCount)
2023-08-06 19:02:14 +00:00
nrCardsInAllowList.Set(float64(totalCardCount))
2023-07-28 22:22:26 +00:00
2023-07-29 22:03:52 +00:00
if cl.KeepOpenUntil != nil {
updateKeepOpenDoor(*cl.KeepOpenUntil)
}
2023-08-06 19:02:14 +00:00
lastSyncTimestamp.SetToCurrentTime()
2023-07-30 10:38:47 +00:00
2023-07-28 22:22:26 +00:00
return nil
2022-04-02 18:14:21 +00:00
}
2023-07-29 17:04:19 +00:00
2023-07-29 22:03:52 +00:00
func updateKeepOpenDoor(newKeepOpenTime time.Time) {
// is there one active?
if keepDoorOpen.timer != nil {
keepDoorOpen.timer.Stop()
keepDoorOpen = KeepDoorOpen{}
}
if newKeepOpenTime.After(time.Now()) {
log.Printf("Keeping door open until %v", newKeepOpenTime)
2023-07-30 10:38:47 +00:00
OpenDoor(wiegand)
2023-07-29 22:03:52 +00:00
timer := time.AfterFunc(time.Until(newKeepOpenTime), handleKeepDoorOpenCloseCleanup)
keepDoorOpen = KeepDoorOpen{
timer: timer,
until: newKeepOpenTime,
}
} else {
2023-07-30 10:38:47 +00:00
CloseDoor(wiegand)
2023-07-29 22:03:52 +00:00
}
}
func handleKeepDoorOpenCloseCleanup() {
fmt.Println("Keep door open time is reached!")
2023-07-30 10:38:47 +00:00
CloseDoor(wiegand)
2023-07-29 22:03:52 +00:00
keepDoorOpen = KeepDoorOpen{}
}
func sendSwipeEvent(cardUidHash string, success bool) error {
swipeEvent := map[string]any{
2023-07-29 17:04:19 +00:00
"uid_hash": cardUidHash,
"door": config.door,
"timestamp": time.Now().Format(time.RFC3339),
"success": success,
2023-07-29 17:04:19 +00:00
}
data, err := json.Marshal(swipeEvent)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, config.api.swipe, bytes.NewReader(data))
if err != nil {
return err
}
req.Header.Add("KEY", config.api.key)
req.Header.Add("Content-Type", "application/json")
2023-07-29 17:04:19 +00:00
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return fmt.Errorf("Server responded with %d", resp.StatusCode)
}
return nil
}