package main
import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/matrix-org/gomatrix"
)
type Status struct {
FuzIsOpen bool `json:"fuzIsOpen"`
LastSeenAsOpen bool `json:"lastSeenAsOpen"`
LastSeen time.Time `json:"lastSeen"`
LastOpened time.Time `json:"lastOpened"`
LastClosed time.Time `json:"lastClosed"`
ProcessUptime string `json:"processUptime"`
}
type Config struct {
PORT string
MATRIXROOM string
MATRIXOPENINGMESSAGE string
MATRIXCLOSINGMESSAGE string
MATRIXACCESSTOKEN string
MATRIXUSERNAME string
ESPUSERNAME string
ESPPASSWORD string
}
const (
dbPath = "./.data/data.json"
defaultClosingTimeout = 1 * time.Minute
espMaxBlinkDuration = 5 * time.Minute
)
var (
status Status
config = Config{
PORT: "8080",
}
startTime = time.Now()
imgs = map[bool]string{
// https://www.iconfinder.com/icons/1871431/online_open_shop_shopping_sign_icon
// formerly https://www.flaticon.com/free-icon/open_1234189, maybe try https://flaticons.net/customize.php?dir=Miscellaneous&icon=Open.png without attribution
true: ``,
// https://www.iconfinder.com/icons/1871435/closed_online_shop_shopping_sign_icon
// formerly https://www.flaticon.com/free-icon/closed_1234190, maybe try https://flaticons.net/customize.php?dir=Miscellaneous&icon=Closed.png without attribution
false: ``,
}
db *os.File
matrix *gomatrix.Client
espIP string
espBlinkDeadline = time.Now().Add(-espMaxBlinkDuration)
)
func init() {
port := os.Getenv("PORT")
if val, _ := strconv.Atoi(port); val > 0 {
config.PORT = port
}
config.MATRIXUSERNAME = os.Getenv("MATRIXUSERNAME")
config.MATRIXACCESSTOKEN = os.Getenv("MATRIXACCESSTOKEN")
config.MATRIXROOM = os.Getenv("MATRIXROOM")
config.MATRIXOPENINGMESSAGE = os.Getenv("MATRIXOPENINGMESSAGE")
config.MATRIXCLOSINGMESSAGE = os.Getenv("MATRIXCLOSINGMESSAGE")
config.ESPUSERNAME = os.Getenv("ESPUSERNAME")
config.ESPPASSWORD = os.Getenv("ESPPASSWORD")
if config.MATRIXUSERNAME == "" {
panic("MATRIXUSERNAME is empty")
}
if config.MATRIXACCESSTOKEN == "" {
panic("MATRIXACCESSTOKEN is empty")
}
var err error
matrix, err = gomatrix.NewClient(fmt.Sprintf("https://%s", config.MATRIXUSERNAME[strings.Index(config.MATRIXUSERNAME, ":")+1:]), config.MATRIXUSERNAME, config.MATRIXACCESSTOKEN)
if err != nil {
panic(fmt.Sprintf("error creating matrix client: %s", err))
}
if _, err := matrix.GetOwnStatus(); err != nil { // a way to quickly check if access token is valid
panic(fmt.Sprintf("error getting matrix status: %s", err))
}
if config.MATRIXROOM == "" {
panic("MATRIXROOM is empty")
}
if config.MATRIXOPENINGMESSAGE == "" {
panic("MATRIXOPENINGMESSAGE is empty")
}
if config.MATRIXCLOSINGMESSAGE == "" {
panic("MATRIXCLOSINGMESSAGE is empty")
}
if config.ESPUSERNAME == "" {
panic("ESPUSERNAME is empty")
}
if config.ESPPASSWORD == "" {
panic("ESPPASSWORD is empty")
}
err = os.MkdirAll(filepath.Dir(dbPath), 0755)
if err != nil {
panic(err)
}
db, err = os.OpenFile(dbPath, os.O_RDWR|os.O_CREATE, 0600)
if err != nil && !os.IsNotExist(err) {
panic(err)
}
err = json.NewDecoder(db).Decode(&status)
if err != nil {
fmt.Println("error unmarshalling db:", err)
}
}
func updateUptime() {
for range time.Tick(time.Second) {
status.ProcessUptime = time.Since(startTime).Truncate(time.Second).String()
}
}
func checkClosure() {
time.Sleep(time.Minute) // give some time for presence button to show up
for {
if status.LastSeen.Add(defaultClosingTimeout).Before(time.Now()) && status.LastClosed.Before(status.LastSeen) {
// the Fuz is newly closed, notify on matrix and write file to survive reboot
fmt.Println("the Fuz is newly closed, notify on matrix and write file to survive reboot")
msg := config.MATRIXCLOSINGMESSAGE
if !status.FuzIsOpen {
msg = fmt.Sprintf("%s : passage bref", msg)
}
_, err := matrix.SendText(config.MATRIXROOM, msg)
if err != nil {
fmt.Println("err:", err)
time.Sleep(10 * time.Second)
continue
}
status.LastClosed = time.Now()
status.FuzIsOpen = false
db.Truncate(0)
db.Seek(0, 0)
e := json.NewEncoder(db)
e.SetIndent("", " ")
e.Encode(status)
}
time.Sleep(10 * time.Second)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
fmt.Fprintf(w, `Fuz presence button public API
This API provides the current opening status of the hackerspace. This server also posts messages on Matrix to notify when the space opens and closes.
Usage:
/ Shows help
/api Serves some JSON with lax CORS headers to get the current opening status programatically. The properties are the following:
* fuzIsOpen: (boolean) reflects if the space is currently open
* lastSeenAsOpen: (boolean) reflects if the last ping by the ESP was after being pushed (space officially opened)
* lastSeen: (date) last ESP ping timestamp
* lastOpened: (date) last space opening timestamp
* lastClosed: (date) last space closing timestamp
* processUptime: (duration) API process uptime
/img Serves an svg image showing if the space is open or closed.
/status Private endpoint used by the ESP (physical button) to regularly ping/update the opening status.
Source code: https://github.com/Lomanic/presence-button-web
Source code mirror: https://git.interhacker.space/Lomanic/presence-button-web
Documentation: https://wiki.fuz.re/doku.php?id=projets:fuz:presence_button`)
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
e := json.NewEncoder(w)
e.SetIndent("", " ")
e.Encode(status)
}
func imgHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
fmt.Fprintf(w, imgs[status.FuzIsOpen])
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
fmt.Printf("status notification by button... ")
if !ok || user != config.ESPUSERNAME || pass != config.ESPPASSWORD {
fmt.Printf("bad authentication: user:%v pass:%v\n", user, pass)
w.Header().Set("WWW-Authenticate", `Basic realm="Authentication required"`)
http.Error(w, "Authentication required", 401)
return
}
// handle being behind proxy https://stackoverflow.com/a/33301173
espIP, _, _ = net.SplitHostPort(r.RemoteAddr)
for _, ifaceIP := range ifacesIPs() {
if espIP == ifaceIP {
for _, header := range []string{"X-Real-Ip", "X-Forwarded-For"} {
if r.Header.Get(header) != "" {
espIP = strings.Split(r.Header.Get(header), ", ")[0]
}
}
}
}
fmt.Printf("espIP %s ", espIP)
q := r.URL.Query()
fuzIsOpen := q.Get("fuzisopen") == "1"
fmt.Printf("button pushed: %v\n", fuzIsOpen)
status.FuzIsOpen = fuzIsOpen
status.LastSeenAsOpen = fuzIsOpen
status.LastSeen = time.Now()
fmt.Fprintf(w, "OK")
db.Truncate(0)
db.Seek(0, 0)
e := json.NewEncoder(db)
e.SetIndent("", " ")
e.Encode(status)
if status.FuzIsOpen && (status.LastOpened.Equal(status.LastClosed) || status.LastOpened.Before(status.LastClosed)) {
// the Fuz is newly opened, notify on matrix and write file to survive reboot
fmt.Println("the Fuz is newly opened, notify on matrix and write file to survive reboot")
_, err := matrix.SendText(config.MATRIXROOM, config.MATRIXOPENINGMESSAGE)
if err != nil {
fmt.Println("err:", err)
return
}
status.LastOpened = time.Now()
db.Truncate(0)
db.Seek(0, 0)
e := json.NewEncoder(db)
e.SetIndent("", " ")
e.Encode(status)
}
}
// https://github.com/qbit/mcchunkie/blob/53eb25ae3394f326e24782f273584e3e6a4c0e8c/plugins/plugins.go#L52-L66
// NameRE matches the "friendly" name. This is typically used in tab
// completion.
var nameRE = regexp.MustCompile(`@(.+):.+$`)
// ToMe returns true of the message pertains to the bot
func toMe(user, message string) bool {
u := nameRE.ReplaceAllString(user, "$1")
return strings.Contains(message, u)
}
// RemoveName removes the friendly name from a given message
func removeName(user, message string) string {
n := nameRE.ReplaceAllString(user, "$1")
return strings.ReplaceAll(message, n+": ", "")
}
func syncMatrix() {
syncer := matrix.Syncer.(*gomatrix.DefaultSyncer)
syncer.OnEventType("m.room.message", func(ev *gomatrix.Event) {
if ev.Sender != matrix.UserID {
matrix.MarkRead(ev.RoomID, ev.ID)
if body, ok := ev.Body(); ok && toMe(config.MATRIXUSERNAME, body) {
cmd := strings.Fields(removeName(config.MATRIXUSERNAME, body))
if len(cmd) >= 2 {
switch cmd[0] {
case "blink":
switch cmd[1] {
case "on":
if status.FuzIsOpen {
matrix.SendFormattedText(config.MATRIXROOM, fmt.Sprintf("%s: let's make it **blink**! (%s max)", ev.Sender, espMaxBlinkDuration), fmt.Sprintf("%s: let's make it blink! (%s max)", ev.Sender, espMaxBlinkDuration))
espBlinkDeadline = time.Now().Add(espMaxBlinkDuration)
} else {
matrix.SendText(config.MATRIXROOM, fmt.Sprintf("%s: can't do that, space is closed", ev.Sender))
}
case "off":
if status.FuzIsOpen {
matrix.SendFormattedText(config.MATRIXROOM, fmt.Sprintf("%s: OK it's time to **STOP**!", ev.Sender), fmt.Sprintf("%s: OK it's time to STOP!", ev.Sender))
espBlinkDeadline = time.Now()
} else {
matrix.SendText(config.MATRIXROOM, fmt.Sprintf("%s: can't do that, space is closed", ev.Sender))
}
case "status":
if time.Now().Before(espBlinkDeadline) {
matrix.SendText(config.MATRIXROOM, fmt.Sprintf("%s: yes it's still blinking!", ev.Sender))
} else {
matrix.SendFormattedText(config.MATRIXROOM, fmt.Sprintf("%s: it's **not** blinking", ev.Sender), fmt.Sprintf("%s: it's not blinking", ev.Sender))
}
default:
matrix.SendFormattedText(config.MATRIXROOM, fmt.Sprintf("%s: command is `blink (status|on|off)`", ev.Sender), fmt.Sprintf("%s: command is blink (status|on|off)
", ev.Sender))
}
return
}
}
matrix.SendText(config.MATRIXROOM, fmt.Sprintf("%s: huh?", ev.Sender))
}
}
})
go func() { // set online status every 15 seconds
for {
if err := matrix.SetStatus("online", "up and running"); err != nil {
fmt.Println("error setting matrix status:", err)
}
time.Sleep(15 * time.Second)
}
}()
for {
if err := matrix.Sync(); err != nil {
fmt.Println("error syncing with matrix:", err)
}
}
}
func espBlinkLoop() {
client := &http.Client{}
for {
time.Sleep(time.Second)
if espIP == "" {
continue
}
if time.Now().Before(espBlinkDeadline) {
// http.get request to espIP to have it light up
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s/admin?enablerotatinglight", espIP), nil)
if err != nil {
fmt.Println("error creating request in espBlinkLoop:", err)
continue
}
req.SetBasicAuth(config.ESPUSERNAME, config.ESPPASSWORD)
fmt.Print("admin?enablerotatinglight ")
fmt.Println(client.Do(req))
time.Sleep(7 * time.Second)
// http.get request to espIP to shut down light
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s/admin?disablerotatinglight", espIP), nil)
if err != nil {
fmt.Println("error creating request in espBlinkLoop:", err)
continue
}
req.SetBasicAuth(config.ESPUSERNAME, config.ESPPASSWORD)
fmt.Print("admin?disablerotatinglight ")
fmt.Println(client.Do(req))
time.Sleep(3 * time.Second)
}
}
}
func ifacesIPs() []string { // from https://stackoverflow.com/a/23558495 linked playground
var ips []string
ifaces, err := net.Interfaces()
if err != nil {
return ips
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
/*if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}*/
addrs, err := iface.Addrs()
if err != nil {
return ips
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
ips = append(ips, ip.String())
}
}
return ips
}
func main() {
fmt.Println(ifacesIPs())
go updateUptime()
go checkClosure()
go syncMatrix()
go espBlinkLoop()
http.HandleFunc("/", rootHandler)
http.HandleFunc("/api", apiHandler)
http.HandleFunc("/img", imgHandler)
http.HandleFunc("/status", statusHandler)
log.Fatal(http.ListenAndServe(":"+config.PORT, nil))
}