mirror of
https://github.com/Macrame-App/Macrame
synced 2025-12-29 07:19:26 +00:00
Logging update
This commit is contained in:
parent
541f016eaf
commit
2098aface9
13 changed files with 119 additions and 91 deletions
|
|
@ -2,7 +2,6 @@ package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"be/app/helper"
|
"be/app/helper"
|
||||||
"log"
|
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -40,19 +39,19 @@ func ApiGet(w http.ResponseWriter, r *http.Request) {
|
||||||
file = "../public/index.html" // default
|
file = "../public/index.html" // default
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.Println("GET:", file)
|
// app.MCRMLog("GET:", file)
|
||||||
|
|
||||||
http.ServeFile(w, r, file) // serve file
|
http.ServeFile(w, r, file) // serve file
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApiPost(w http.ResponseWriter, r *http.Request) {
|
func ApiPost(w http.ResponseWriter, r *http.Request) {
|
||||||
access, data := helper.EndpointAccess(w, r)
|
access, data, err := helper.EndpointAccess(w, r)
|
||||||
|
|
||||||
if !access {
|
if !access || err != nil {
|
||||||
|
MCRMLog("ApiPost EndPointAccess Error: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("api post", data == "")
|
|
||||||
if data != "" {
|
if data != "" {
|
||||||
ApiAuth(data, w, r)
|
ApiAuth(data, w, r)
|
||||||
return
|
return
|
||||||
|
|
@ -95,7 +94,6 @@ func ApiPost(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApiAuth(data string, w http.ResponseWriter, r *http.Request) {
|
func ApiAuth(data string, w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("apiauth", data != "")
|
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/macro/play":
|
case "/macro/play":
|
||||||
PlayMacro(data, w, r)
|
PlayMacro(data, w, r)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"be/app/structs"
|
"be/app/structs"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -18,12 +17,12 @@ import (
|
||||||
func GetServerIP(w http.ResponseWriter, r *http.Request) {
|
func GetServerIP(w http.ResponseWriter, r *http.Request) {
|
||||||
ifs, err := net.Interfaces()
|
ifs, err := net.Interfaces()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
MCRMLog(err)
|
||||||
}
|
}
|
||||||
for _, ifi := range ifs {
|
for _, ifi := range ifs {
|
||||||
addrs, err := ifi.Addrs()
|
addrs, err := ifi.Addrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
MCRMLog(err)
|
||||||
}
|
}
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
var ip net.IP
|
var ip net.IP
|
||||||
|
|
@ -44,11 +43,10 @@ func GetServerIP(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeviceList(w http.ResponseWriter, r *http.Request) {
|
func DeviceList(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("device list")
|
|
||||||
dir := "devices"
|
dir := "devices"
|
||||||
files, err := os.ReadDir(dir)
|
files, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
MCRMLog("DeviceList Error: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
devices := make(map[string]map[string]interface{})
|
devices := make(map[string]map[string]interface{})
|
||||||
|
|
@ -58,8 +56,6 @@ func DeviceList(w http.ResponseWriter, r *http.Request) {
|
||||||
ext := filepath.Ext(filePath)
|
ext := filepath.Ext(filePath)
|
||||||
device := strings.TrimSuffix(file.Name(), ext)
|
device := strings.TrimSuffix(file.Name(), ext)
|
||||||
|
|
||||||
// log.Println(device, ext)
|
|
||||||
|
|
||||||
if _, ok := devices[device]; !ok {
|
if _, ok := devices[device]; !ok {
|
||||||
devices[device] = make(map[string]interface{})
|
devices[device] = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
@ -76,32 +72,30 @@ func DeviceList(w http.ResponseWriter, r *http.Request) {
|
||||||
"devices": devices,
|
"devices": devices,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(result)
|
|
||||||
|
|
||||||
json.NewEncoder(w).Encode(result)
|
json.NewEncoder(w).Encode(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readDeviceSettings(filepath string) (settings structs.Settings) {
|
func readDeviceSettings(filepath string) (settings structs.Settings) {
|
||||||
data, err := os.ReadFile(filepath)
|
data, err := os.ReadFile(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
MCRMLog("readDeviceSettings Error: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(data, &settings)
|
err = json.Unmarshal(data, &settings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
MCRMLog("readDeviceSettings JSON Error: ", err)
|
||||||
}
|
}
|
||||||
log.Println(settings)
|
|
||||||
return settings
|
return settings
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeviceAccessCheck(w http.ResponseWriter, r *http.Request) {
|
func DeviceAccessCheck(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("device access check")
|
|
||||||
var req structs.Check
|
var req structs.Check
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
err := json.NewDecoder(r.Body).Decode(&req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("DeviceAccessCheck Error: ", err)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -110,13 +104,12 @@ func DeviceAccessCheck(w http.ResponseWriter, r *http.Request) {
|
||||||
_, errKey := os.Stat("devices/" + req.Uuid + ".key")
|
_, errKey := os.Stat("devices/" + req.Uuid + ".key")
|
||||||
|
|
||||||
if (errSett == nil) && (errKey == nil) {
|
if (errSett == nil) && (errKey == nil) {
|
||||||
log.Println("authorized")
|
|
||||||
json.NewEncoder(w).Encode("authorized")
|
json.NewEncoder(w).Encode("authorized")
|
||||||
} else if (errSett == nil) && (errKey != nil) {
|
} else if (errSett == nil) && (errKey != nil) {
|
||||||
log.Println("unauthorized")
|
MCRMLog("DeviceAccessCheck: UUID: ", req.Uuid, "; Access: Unauthorized")
|
||||||
json.NewEncoder(w).Encode("unauthorized")
|
json.NewEncoder(w).Encode("unauthorized")
|
||||||
} else {
|
} else {
|
||||||
log.Println("unauthorized")
|
MCRMLog("DeviceAccessCheck: UUID: ", req.Uuid, "; Access: Unlinked")
|
||||||
json.NewEncoder(w).Encode("unlinked")
|
json.NewEncoder(w).Encode("unlinked")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,14 +130,14 @@ func DeviceAccessRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
settingsJSON, err := json.Marshal(deviceSettings)
|
settingsJSON, err := json.Marshal(deviceSettings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
MCRMLog("DeviceAccessRequest JSON Error: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile("devices/"+req.Uuid+".json", settingsJSON, 0644)
|
err = os.WriteFile("devices/"+req.Uuid+".json", settingsJSON, 0644)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
MCRMLog("DeviceAccessRequest Error: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,11 +145,11 @@ func DeviceAccessRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func PingLink(w http.ResponseWriter, r *http.Request) {
|
func PingLink(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("ping link")
|
|
||||||
var req structs.Check
|
var req structs.Check
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
err := json.NewDecoder(r.Body).Decode(&req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("PingLink Error: ", err)
|
||||||
json.NewEncoder(w).Encode(false)
|
json.NewEncoder(w).Encode(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -166,24 +159,24 @@ func PingLink(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
encryptedKey, encErr := helper.EncryptAES(string(pin), string(key))
|
encryptedKey, encErr := helper.EncryptAES(string(pin), string(key))
|
||||||
|
|
||||||
log.Println(encryptedKey, string(pin), string(key))
|
|
||||||
|
|
||||||
if keyErr == nil && pinErr == nil && encErr == nil {
|
if keyErr == nil && pinErr == nil && encErr == nil {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write([]byte(encryptedKey))
|
w.Write([]byte(encryptedKey))
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
MCRMLog("PingLink Error: keyErr:", keyErr, "; pinErr:", pinErr, "; encErr:", encErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
json.NewEncoder(w).Encode(false)
|
json.NewEncoder(w).Encode(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartLink(w http.ResponseWriter, r *http.Request) {
|
func StartLink(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("start link")
|
|
||||||
var req structs.Check
|
var req structs.Check
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
err := json.NewDecoder(r.Body).Decode(&req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("StartLink Error: ", err)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -192,10 +185,13 @@ func StartLink(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
deviceKey := helper.GenerateKey()
|
deviceKey := helper.GenerateKey()
|
||||||
|
|
||||||
err = helper.SaveDeviceKey(req.Uuid, deviceKey)
|
errKey := helper.SaveDeviceKey(req.Uuid, deviceKey)
|
||||||
|
savedPin, errPin := helper.TempPinFile(req.Uuid, pin)
|
||||||
|
|
||||||
if err == nil && helper.TempPinFile(req.Uuid, pin) {
|
if errKey == nil && errPin == nil && savedPin == true {
|
||||||
json.NewEncoder(w).Encode(pin)
|
json.NewEncoder(w).Encode(pin)
|
||||||
|
} else {
|
||||||
|
MCRMLog("StartLink Error: errKey:", err, "; errPin:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
@ -224,6 +220,7 @@ func RemoveLink(data string, w http.ResponseWriter, r *http.Request) {
|
||||||
_, err := helper.ParseRequest(req, data, r)
|
_, err := helper.ParseRequest(req, data, r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("RemoveLink ParseRequest Error: ", err)
|
||||||
json.NewEncoder(w).Encode(false)
|
json.NewEncoder(w).Encode(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -231,6 +228,7 @@ func RemoveLink(data string, w http.ResponseWriter, r *http.Request) {
|
||||||
err = os.Remove("devices/" + req.Uuid + ".key")
|
err = os.Remove("devices/" + req.Uuid + ".key")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("RemoveLink Remove Error: ", err)
|
||||||
json.NewEncoder(w).Encode(false)
|
json.NewEncoder(w).Encode(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -250,6 +248,7 @@ func Handshake(w http.ResponseWriter, r *http.Request) {
|
||||||
deviceKey, err := helper.GetKeyByUuid(req.Uuid)
|
deviceKey, err := helper.GetKeyByUuid(req.Uuid)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("Handshake GetKeyByUuid Error: ", err)
|
||||||
json.NewEncoder(w).Encode(false)
|
json.NewEncoder(w).Encode(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -11,27 +11,27 @@ import (
|
||||||
. "be/app/structs"
|
. "be/app/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func EndpointAccess(w http.ResponseWriter, r *http.Request) (bool, string) {
|
func EndpointAccess(w http.ResponseWriter, r *http.Request) (bool, string, error) {
|
||||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return false, "", errors.New(r.URL.Path + ": SplitHostPort error: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isLocal(ip) && isEndpointAllowed("Local", r.URL.Path)) ||
|
if (isLocal(ip) && isEndpointAllowed("Local", r.URL.Path)) ||
|
||||||
(isLanRemote(ip) && isEndpointAllowed("Remote", r.URL.Path)) {
|
(isLanRemote(ip) && isEndpointAllowed("Remote", r.URL.Path)) {
|
||||||
log.Println(r.URL.Path, "endpoint access: accessible")
|
return true, "", nil
|
||||||
return true, ""
|
|
||||||
} else if isLanRemote(ip) && isEndpointAllowed("Auth", r.URL.Path) {
|
} else if isLanRemote(ip) && isEndpointAllowed("Auth", r.URL.Path) {
|
||||||
log.Println(r.URL.Path, "endpoint access: authorized")
|
data, err := decryptAuth(r)
|
||||||
|
|
||||||
data := decryptAuth(r)
|
if err != nil {
|
||||||
|
return false, "", err
|
||||||
|
}
|
||||||
|
|
||||||
return data != "", data
|
return data != "", data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(r.URL.Path, "endpoint access: not authorized or accessible")
|
return false, "", errors.New(r.URL.Path + ": not authorized or accessible")
|
||||||
|
|
||||||
return false, ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isLocal(ip string) bool {
|
func isLocal(ip string) bool {
|
||||||
|
|
@ -45,7 +45,7 @@ func isLanRemote(ip string) bool {
|
||||||
func isEndpointAllowed(source string, endpoint string) bool {
|
func isEndpointAllowed(source string, endpoint string) bool {
|
||||||
var endpoints, err = getAllowedEndpoints(source)
|
var endpoints, err = getAllowedEndpoints(source)
|
||||||
if err != "" {
|
if err != "" {
|
||||||
log.Println(err)
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (endpoints != nil) && (len(endpoints) > 0) {
|
if (endpoints != nil) && (len(endpoints) > 0) {
|
||||||
|
|
@ -73,25 +73,23 @@ func getAllowedEndpoints(source string) (endpoints []string, err string) {
|
||||||
return []string{}, "No allowed endpoints"
|
return []string{}, "No allowed endpoints"
|
||||||
}
|
}
|
||||||
|
|
||||||
func decryptAuth(r *http.Request) string {
|
func decryptAuth(r *http.Request) (string, error) {
|
||||||
var req structs.Authcall
|
var req structs.Authcall
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
err := json.NewDecoder(r.Body).Decode(&req)
|
||||||
|
|
||||||
if err != nil || req.Uuid == "" || req.Data == "" {
|
if err != nil || req.Uuid == "" || req.Data == "" {
|
||||||
return ""
|
return "", errors.New("DecryptAuth Error: " + err.Error() + "; UUID: " + req.Uuid + "; Data: " + req.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("data", req.Data)
|
|
||||||
|
|
||||||
deviceKey, errKey := GetKeyByUuid(req.Uuid)
|
deviceKey, errKey := GetKeyByUuid(req.Uuid)
|
||||||
decryptData, errDec := DecryptAES(deviceKey, req.Data)
|
decryptData, errDec := DecryptAES(deviceKey, req.Data)
|
||||||
|
|
||||||
if errKey != nil && errDec != nil || decryptData == "" {
|
if errKey != nil && errDec != nil || decryptData == "" {
|
||||||
return ""
|
return "", errors.New("DecryptAuth Error: " + errKey.Error() + "; " + errDec.Error() + "; UUID: " + req.Uuid + "; Data: " + req.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return decryptData
|
return decryptData, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseRequest(req interface{}, data string, r *http.Request) (d interface{}, err error) {
|
func ParseRequest(req interface{}, data string, r *http.Request) (d interface{}, err error) {
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,22 @@
|
||||||
package helper
|
package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TempPinFile(Uuid string, pin string) bool {
|
func TempPinFile(Uuid string, pin string) (bool, error) {
|
||||||
log.Println("temp pin file", Uuid, pin)
|
|
||||||
err := os.WriteFile("devices/"+Uuid+".tmp", []byte(pin), 0644)
|
err := os.WriteFile("devices/"+Uuid+".tmp", []byte(pin), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
return false, errors.New("TempPinFile Error: " + err.Error())
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
time.AfterFunc(1*time.Minute, func() {
|
time.AfterFunc(1*time.Minute, func() {
|
||||||
log.Println("deleting", Uuid, pin)
|
|
||||||
os.Remove("devices/" + Uuid + ".tmp")
|
os.Remove("devices/" + Uuid + ".tmp")
|
||||||
})
|
})
|
||||||
|
|
||||||
return true
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckPinFile(Uuid string) bool {
|
func CheckPinFile(Uuid string) bool {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package helper
|
package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
|
@ -10,7 +9,7 @@ import (
|
||||||
func EnvGet(key string) string {
|
func EnvGet(key string) string {
|
||||||
err := godotenv.Load("../.env")
|
err := godotenv.Load("../.env")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error loading .env file")
|
return ""
|
||||||
}
|
}
|
||||||
return os.Getenv("VITE_" + key)
|
return os.Getenv("VITE_" + key)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -34,12 +34,10 @@ func FormatMacroFileName(s string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadMacroFile(filename string) (steps []structs.Step, err error) {
|
func ReadMacroFile(filename string) (steps []structs.Step, err error) {
|
||||||
log.Println(filename)
|
|
||||||
|
|
||||||
content, err := os.ReadFile(filename)
|
content, err := os.ReadFile(filename)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error when opening file: ", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(content, &steps)
|
err = json.Unmarshal(content, &steps)
|
||||||
|
|
@ -47,18 +45,19 @@ func ReadMacroFile(filename string) (steps []structs.Step, err error) {
|
||||||
return steps, err
|
return steps, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunMacroSteps(steps []structs.Step) {
|
func RunMacroSteps(steps []structs.Step) error {
|
||||||
for _, step := range steps {
|
for _, step := range steps {
|
||||||
// log.Println(step)
|
|
||||||
switch step.Type {
|
switch step.Type {
|
||||||
case "key":
|
case "key":
|
||||||
robotgo.KeyToggle(step.Key, step.Direction)
|
err := robotgo.KeyToggle(step.Key, step.Direction)
|
||||||
// log.Println("Toggling", step.Key, "to", step.Direction)
|
if err != nil {
|
||||||
|
return errors.New("RunMacroSteps KeyToggle Error: " + err.Error())
|
||||||
|
}
|
||||||
case "delay":
|
case "delay":
|
||||||
time.Sleep(time.Duration(step.Location) * time.Millisecond)
|
time.Sleep(time.Duration(step.Location) * time.Millisecond)
|
||||||
// log.Println("Sleeping for", step.Value, "milliseconds")
|
|
||||||
default:
|
default:
|
||||||
log.Println("Unknown step type:", step.Type)
|
return errors.New("RunMacroSteps Unknown step type:" + step.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
22
be/app/log.go
Normal file
22
be/app/log.go
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var logFile *os.File
|
||||||
|
|
||||||
|
func MCRMLogInit() {
|
||||||
|
var err error
|
||||||
|
logFile, err = os.OpenFile("log.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func MCRMLog(v ...interface{}) {
|
||||||
|
log.Println(v...)
|
||||||
|
fmt.Fprintln(logFile, v...)
|
||||||
|
}
|
||||||
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -19,33 +18,35 @@ func SaveMacro(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
MCRMLog("SaveMacro ReadAll Error: ", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(string(body))
|
|
||||||
|
|
||||||
err = json.Unmarshal(body, &newMacro)
|
err = json.Unmarshal(body, &newMacro)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
MCRMLog("SaveMacro Unmarshal Error: ", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
stepsJSON, err := json.Marshal(newMacro.Steps)
|
stepsJSON, err := json.Marshal(newMacro.Steps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
MCRMLog("SaveMacro Marshal Error: ", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile("../macros/"+helper.FormatMacroFileName(newMacro.Name)+".json", stepsJSON, 0644)
|
err = os.WriteFile("../macros/"+helper.FormatMacroFileName(newMacro.Name)+".json", stepsJSON, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
MCRMLog("SaveMacro WriteFile Error: ", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListMacros(w http.ResponseWriter, r *http.Request) {
|
func ListMacros(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("listing macros")
|
|
||||||
dir := "../macros"
|
dir := "../macros"
|
||||||
files, err := os.ReadDir(dir)
|
files, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
MCRMLog("ListMacros ReadDir Error: ", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var macroList []structs.MacroInfo
|
var macroList []structs.MacroInfo
|
||||||
|
|
@ -55,7 +56,6 @@ func ListMacros(w http.ResponseWriter, r *http.Request) {
|
||||||
macroname := strings.TrimSuffix(filename, filepath.Ext(filename))
|
macroname := strings.TrimSuffix(filename, filepath.Ext(filename))
|
||||||
nicename := strings.Replace(macroname, "_", " ", -1)
|
nicename := strings.Replace(macroname, "_", " ", -1)
|
||||||
|
|
||||||
log.Println(macroname, nicename)
|
|
||||||
macroList = append(macroList, structs.MacroInfo{
|
macroList = append(macroList, structs.MacroInfo{
|
||||||
Name: nicename,
|
Name: nicename,
|
||||||
Macroname: macroname,
|
Macroname: macroname,
|
||||||
|
|
@ -72,6 +72,7 @@ func PlayMacro(data string, w http.ResponseWriter, r *http.Request) {
|
||||||
_, err := helper.ParseRequest(req, data, r)
|
_, err := helper.ParseRequest(req, data, r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("PlayMacro ParseRequest Error: ", err)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -83,10 +84,15 @@ func PlayMacro(data string, w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
macroFile, err := helper.ReadMacroFile(filepath)
|
macroFile, err := helper.ReadMacroFile(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
MCRMLog("PlayMacro ReadMacroFile Error: ", err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.RunMacroSteps(macroFile)
|
err = helper.RunMacroSteps(macroFile)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
MCRMLog("PlayMacro RunMacroSteps Error: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"be/app/structs"
|
"be/app/structs"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -14,6 +13,7 @@ import (
|
||||||
func PanelList(w http.ResponseWriter, r *http.Request) {
|
func PanelList(w http.ResponseWriter, r *http.Request) {
|
||||||
panelDirs, err := os.ReadDir("../panels")
|
panelDirs, err := os.ReadDir("../panels")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("PanelList ReadDir Error: ", err)
|
||||||
json.NewEncoder(w).Encode(false)
|
json.NewEncoder(w).Encode(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -22,12 +22,12 @@ func PanelList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
for _, panelDir := range panelDirs {
|
for _, panelDir := range panelDirs {
|
||||||
if panelDir.IsDir() {
|
if panelDir.IsDir() {
|
||||||
// log.Println(getPanelInfo(panelDir.Name()))
|
|
||||||
panelList = append(panelList, getPanelInfo(panelDir.Name()))
|
panelList = append(panelList, getPanelInfo(panelDir.Name()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(panelList) == 0 {
|
if len(panelList) == 0 {
|
||||||
|
MCRMLog("PanelList: No panels found")
|
||||||
json.NewEncoder(w).Encode(false)
|
json.NewEncoder(w).Encode(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -48,7 +48,7 @@ func getPanelInfo(dirname string) structs.PanelInfo {
|
||||||
} else {
|
} else {
|
||||||
err = json.Unmarshal(jsonFile, &panelInfo)
|
err = json.Unmarshal(jsonFile, &panelInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
MCRMLog("getPanelInfo Unmarshal Error: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -67,6 +67,7 @@ func getPanelThumb(dirname string) string {
|
||||||
filename := "thumbnail" + ext
|
filename := "thumbnail" + ext
|
||||||
file, err := os.Open("../panels/" + dirname + "/" + filename)
|
file, err := os.Open("../panels/" + dirname + "/" + filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("getPanelThumb Open Error: ", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
@ -87,6 +88,7 @@ func getPanelCode(dirname string) (html string, css string) {
|
||||||
func encodeImg(file *os.File) string {
|
func encodeImg(file *os.File) string {
|
||||||
contents, err := os.ReadFile(file.Name())
|
contents, err := os.ReadFile(file.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("encodeImg ReadFile Error: ", err)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -99,12 +101,13 @@ func GetPanel(data string, w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
_, err := helper.ParseRequest(req, data, r)
|
_, err := helper.ParseRequest(req, data, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("GetPanel ParseRequest Error: ", err)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = structs.PanelResponse{}
|
var response = structs.PanelResponse{}
|
||||||
// dirname := req.Dir
|
|
||||||
panelInfo := getPanelInfo(req.Dir)
|
panelInfo := getPanelInfo(req.Dir)
|
||||||
panelHtml, panelCss := getPanelCode(req.Dir)
|
panelHtml, panelCss := getPanelCode(req.Dir)
|
||||||
|
|
||||||
|
|
@ -124,6 +127,7 @@ func SavePanelJSON(w http.ResponseWriter, r *http.Request) {
|
||||||
var req structs.PanelSaveJSON
|
var req structs.PanelSaveJSON
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
err := json.NewDecoder(r.Body).Decode(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("SavePanelJSON Decode Error: ", err)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -135,12 +139,14 @@ func SavePanelJSON(w http.ResponseWriter, r *http.Request) {
|
||||||
// Marshal the data to JSON without the dir field
|
// Marshal the data to JSON without the dir field
|
||||||
jsonData, err := json.Marshal(req)
|
jsonData, err := json.Marshal(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("SavePanelJSON Marshal Error: ", err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile(filePath, jsonData, 0644)
|
err = os.WriteFile(filePath, jsonData, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
MCRMLog("SavePanelJSON WriteFile Error: ", err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,6 @@ require (
|
||||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||||
golang.org/x/exp v0.0.0-20250215185904-eff6e970281f // indirect
|
golang.org/x/exp v0.0.0-20250215185904-eff6e970281f // indirect
|
||||||
golang.org/x/image v0.24.0 // indirect
|
golang.org/x/image v0.24.0 // indirect
|
||||||
golang.org/x/net v0.38.0 // indirect
|
golang.org/x/net v0.39.0 // indirect
|
||||||
golang.org/x/sys v0.31.0 // indirect
|
golang.org/x/sys v0.32.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -68,13 +68,13 @@ golang.org/x/exp v0.0.0-20250215185904-eff6e970281f h1:oFMYAjX0867ZD2jcNiLBrI9Bd
|
||||||
golang.org/x/exp v0.0.0-20250215185904-eff6e970281f/go.mod h1:BHOTPb3L19zxehTsLoJXVaTktb06DFgmdW6Wb9s8jqk=
|
golang.org/x/exp v0.0.0-20250215185904-eff6e970281f/go.mod h1:BHOTPb3L19zxehTsLoJXVaTktb06DFgmdW6Wb9s8jqk=
|
||||||
golang.org/x/image v0.24.0 h1:AN7zRgVsbvmTfNyqIbbOraYL8mSwcKncEj8ofjgzcMQ=
|
golang.org/x/image v0.24.0 h1:AN7zRgVsbvmTfNyqIbbOraYL8mSwcKncEj8ofjgzcMQ=
|
||||||
golang.org/x/image v0.24.0/go.mod h1:4b/ITuLfqYq1hqZcjofwctIhi7sZh2WaCjvsBNjjya8=
|
golang.org/x/image v0.24.0/go.mod h1:4b/ITuLfqYq1hqZcjofwctIhi7sZh2WaCjvsBNjjya8=
|
||||||
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
|
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
|
||||||
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
|
||||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
|
||||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
|
||||||
3
be/log.txt
Normal file
3
be/log.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
ApiPost EndPointAccess Error: /macro/list: endpoint access: accessible
|
||||||
|
ApiPost EndPointAccess Error: /macro/list: endpoint access: accessible
|
||||||
|
ApiPost EndPointAccess Error: /panel/list: endpoint access: accessible
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"be/app"
|
"be/app"
|
||||||
|
|
@ -9,11 +8,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
app.MCRMLogInit()
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
apiInit(w, r)
|
apiInit(w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Println(http.ListenAndServe(":"+helper.EnvGet("MCRM__PORT"), nil))
|
app.MCRMLog(http.ListenAndServe(":"+helper.EnvGet("MCRM__PORT"), nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiInit(w http.ResponseWriter, r *http.Request) {
|
func apiInit(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue