Works sorta

This commit is contained in:
Arti Zirk 2022-04-03 20:34:46 +03:00
parent 092e44cfa5
commit 846a610c58
4 changed files with 80 additions and 5 deletions

3
go.mod
View File

@ -4,5 +4,6 @@ go 1.18
require (
github.com/warthog618/gpiod v0.8.0 // indirect
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
)

4
go.sum
View File

@ -1,4 +1,8 @@
github.com/warthog618/gpiod v0.8.0 h1:qxH9XVvWHpTxzWFSndBcujFyNH5zVRzHM63tcmm85o4=
github.com/warthog618/gpiod v0.8.0/go.mod h1:a7Csa+IJtDBZ39++zC/6Srjo01qWejt/5velrDWuNkY=
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 h1:tkVvjkPTB7pnW3jnid7kNyAMPVWllTNOf/qKDze4p9o=
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -1,6 +1,11 @@
package main
import "fmt"
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
import "time"
const wiegand_a = 17
@ -8,12 +13,56 @@ const wiegand_b = 18
const wiegand_bit_timeout = time.Millisecond * 8
const solenoid = 21
type card struct {
UidHash string `json:"uid_hash"`
}
type cardToken struct {
Token card `json:"token"`
}
type cardList struct {
AllowedUids []cardToken `json:"allowed_uids"`
}
type ValidUids map[string]bool // bool has no meaning
func main() {
wiegand := WiegandSetup(wiegand_a, wiegand_b, wiegand_bit_timeout, solenoid)
//openDoor(solenoid_line)
go wiegand.cardRunner()
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, "urlrul", nil)
if err != nil {
panic(err)
}
req.Header.Add("KEY", "keykey")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", resp)
var cl cardList
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
err = json.Unmarshal(body, &cl)
if err != nil {
panic(err)
}
validUids := make(ValidUids)
for i, val := range cl.AllowedUids {
fmt.Printf("%d: %+v\n", i, val.Token.UidHash)
validUids[val.Token.UidHash] = false
}
go wiegand.cardRunner(validUids)
fmt.Printf("Sleeping\n")
for {

View File

@ -1,7 +1,10 @@
package main
import (
"encoding/binary"
"encoding/hex"
"fmt"
"golang.org/x/crypto/scrypt"
"time"
)
@ -36,7 +39,7 @@ func printCardId(card uint64) {
fmt.Printf("\n")
}
func (w *Wiegand) cardRunner() {
func (w *Wiegand) cardRunner(validUids ValidUids) {
for {
// Wait for bit timeout
fmt.Printf("Waiting for bit timeout\n")
@ -56,6 +59,24 @@ func (w *Wiegand) cardRunner() {
printCardId(card)
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, card)
hashed, err := scrypt.Key(b, []byte("hashsah"), 16384, 8, 1, 64)
if err != nil {
panic(err)
}
hashedHex := hex.EncodeToString(hashed)
fmt.Println(hashedHex)
_, ok := validUids[hashedHex]
if ok {
fmt.Println("Opening door")
w.OpenDoor()
} else {
fmt.Println("Unknown card")
}
w.bitNr = 0
}
}