Logging update

This commit is contained in:
Jesse Malotaux 2025-04-11 17:49:28 +02:00
parent 541f016eaf
commit 2098aface9
13 changed files with 119 additions and 91 deletions

View file

@ -2,7 +2,7 @@ package helper
import (
"encoding/json"
"log"
"errors"
"net"
"net/http"
"strings"
@ -11,27 +11,27 @@ import (
. "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)
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)) ||
(isLanRemote(ip) && isEndpointAllowed("Remote", r.URL.Path)) {
log.Println(r.URL.Path, "endpoint access: accessible")
return true, ""
return true, "", nil
} 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, ""
return false, "", errors.New(r.URL.Path + ": not authorized or accessible")
}
func isLocal(ip string) bool {
@ -45,7 +45,7 @@ func isLanRemote(ip string) bool {
func isEndpointAllowed(source string, endpoint string) bool {
var endpoints, err = getAllowedEndpoints(source)
if err != "" {
log.Println(err)
return false
}
if (endpoints != nil) && (len(endpoints) > 0) {
@ -73,25 +73,23 @@ func getAllowedEndpoints(source string) (endpoints []string, err string) {
return []string{}, "No allowed endpoints"
}
func decryptAuth(r *http.Request) string {
func decryptAuth(r *http.Request) (string, error) {
var req structs.Authcall
err := json.NewDecoder(r.Body).Decode(&req)
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)
decryptData, errDec := DecryptAES(deviceKey, req.Data)
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) {

View file

@ -1,25 +1,22 @@
package helper
import (
"log"
"errors"
"os"
"time"
)
func TempPinFile(Uuid string, pin string) bool {
log.Println("temp pin file", Uuid, pin)
func TempPinFile(Uuid string, pin string) (bool, error) {
err := os.WriteFile("devices/"+Uuid+".tmp", []byte(pin), 0644)
if err != nil {
log.Println(err)
return false
return false, errors.New("TempPinFile Error: " + err.Error())
}
time.AfterFunc(1*time.Minute, func() {
log.Println("deleting", Uuid, pin)
os.Remove("devices/" + Uuid + ".tmp")
})
return true
return true, nil
}
func CheckPinFile(Uuid string) bool {

View file

@ -1,7 +1,6 @@
package helper
import (
"log"
"os"
"github.com/joho/godotenv"
@ -10,7 +9,7 @@ import (
func EnvGet(key string) string {
err := godotenv.Load("../.env")
if err != nil {
log.Fatal("Error loading .env file")
return ""
}
return os.Getenv("VITE_" + key)
}

View file

@ -2,7 +2,7 @@ package helper
import (
"encoding/json"
"log"
"errors"
"os"
"regexp"
"strings"
@ -34,12 +34,10 @@ func FormatMacroFileName(s string) string {
}
func ReadMacroFile(filename string) (steps []structs.Step, err error) {
log.Println(filename)
content, err := os.ReadFile(filename)
if err != nil {
log.Fatal("Error when opening file: ", err)
return nil, err
}
err = json.Unmarshal(content, &steps)
@ -47,18 +45,19 @@ func ReadMacroFile(filename string) (steps []structs.Step, err error) {
return steps, err
}
func RunMacroSteps(steps []structs.Step) {
func RunMacroSteps(steps []structs.Step) error {
for _, step := range steps {
// log.Println(step)
switch step.Type {
case "key":
robotgo.KeyToggle(step.Key, step.Direction)
// log.Println("Toggling", step.Key, "to", step.Direction)
err := robotgo.KeyToggle(step.Key, step.Direction)
if err != nil {
return errors.New("RunMacroSteps KeyToggle Error: " + err.Error())
}
case "delay":
time.Sleep(time.Duration(step.Location) * time.Millisecond)
// log.Println("Sleeping for", step.Value, "milliseconds")
default:
log.Println("Unknown step type:", step.Type)
return errors.New("RunMacroSteps Unknown step type:" + step.Type)
}
}
return nil
}