mirror of
https://github.com/Macrame-App/Macrame
synced 2025-12-29 15:29:26 +00:00
Redundant commit: only to switch branches. Can be removed.
This commit is contained in:
commit
59dd711ab3
102 changed files with 34954 additions and 0 deletions
99
be/app/api.go
Normal file
99
be/app/api.go
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"be/app/helper"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ApiCORS(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request) {
|
||||
origin := r.Header.Get("Origin")
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173")
|
||||
|
||||
if strings.HasPrefix(r.Host, "192.168.") {
|
||||
log.Println("lan device")
|
||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||
}
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Accept, Accept-Language, Accept-Encoding")
|
||||
|
||||
return w, r
|
||||
}
|
||||
|
||||
func ApiGet(w http.ResponseWriter, r *http.Request) {
|
||||
file := "" // base directory
|
||||
|
||||
if r.URL.Path != "/" {
|
||||
file = "../public" + r.URL.Path // request
|
||||
}
|
||||
contentType := mime.TypeByExtension(filepath.Ext(file)) // get content type
|
||||
|
||||
if contentType != "" {
|
||||
w.Header().Set("Content-Type", contentType) // set content type header
|
||||
}
|
||||
|
||||
if contentType == "" {
|
||||
file = "../public/index.html" // default
|
||||
}
|
||||
|
||||
// log.Println("GET:", file)
|
||||
|
||||
http.ServeFile(w, r, file) // serve file
|
||||
}
|
||||
|
||||
func ApiPost(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
access, data := helper.EndpointAccess(w, r)
|
||||
|
||||
if !access {
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("api post", data == "")
|
||||
if data != "" {
|
||||
ApiAuth(data, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.URL.Path {
|
||||
case "/macro/record":
|
||||
SaveMacro(w, r)
|
||||
case "/macro/list":
|
||||
ListMacros(w, r)
|
||||
case "/macro/delete":
|
||||
DeleteMacro(w, r)
|
||||
case "/macro/play":
|
||||
PlayMacro("", w, r)
|
||||
case "/device/list":
|
||||
DeviceList(w, r)
|
||||
case "/device/access/check":
|
||||
DeviceAccessCheck(w, r)
|
||||
case "/device/access/request":
|
||||
DeviceAccessRequest(w, r)
|
||||
case "/device/link/ping":
|
||||
PingLink(w, r)
|
||||
case "/device/link/start":
|
||||
StartLink(w, r)
|
||||
case "/device/link/poll":
|
||||
PollLink(w, r)
|
||||
case "/device/link/remove":
|
||||
RemoveLink("", w, r)
|
||||
case "/device/handshake":
|
||||
Handshake(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func ApiAuth(data string, w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("apiauth", data != "")
|
||||
switch r.URL.Path {
|
||||
case "/macro/play":
|
||||
PlayMacro(data, w, r)
|
||||
case "/device/link/remove":
|
||||
RemoveLink(data, w, r)
|
||||
}
|
||||
}
|
||||
245
be/app/device.go
Normal file
245
be/app/device.go
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"be/app/helper"
|
||||
"be/app/structs"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func DeviceList(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("device list")
|
||||
dir := "devices"
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
devices := make(map[string]map[string]interface{})
|
||||
|
||||
for _, file := range files {
|
||||
filePath := dir + "/" + file.Name()
|
||||
ext := filepath.Ext(filePath)
|
||||
device := strings.TrimSuffix(file.Name(), ext)
|
||||
|
||||
log.Println(device, ext)
|
||||
|
||||
if _, ok := devices[device]; !ok {
|
||||
devices[device] = make(map[string]interface{})
|
||||
}
|
||||
|
||||
if ext == ".json" {
|
||||
devices[device]["settings"] = readDeviceSettings(filePath)
|
||||
}
|
||||
if ext == ".key" {
|
||||
devices[device]["key"] = true
|
||||
}
|
||||
}
|
||||
|
||||
result := map[string]interface{}{
|
||||
"devices": devices,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
|
||||
func readDeviceSettings(filepath string) (settings structs.Settings) {
|
||||
data, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &settings)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
log.Println(settings)
|
||||
return settings
|
||||
}
|
||||
|
||||
func DeviceAccessCheck(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("device access check")
|
||||
var req structs.Check
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, errSett := os.Stat("devices/" + req.Uuid + ".json")
|
||||
_, errKey := os.Stat("devices/" + req.Uuid + ".key")
|
||||
|
||||
if (errSett == nil) && (errKey == nil) {
|
||||
log.Println("authorized")
|
||||
json.NewEncoder(w).Encode("authorized")
|
||||
} else if (errSett == nil) && (errKey != nil) {
|
||||
log.Println("unauthorized")
|
||||
json.NewEncoder(w).Encode("unauthorized")
|
||||
} else {
|
||||
log.Println("unauthorized")
|
||||
json.NewEncoder(w).Encode("unlinked")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func DeviceAccessRequest(w http.ResponseWriter, r *http.Request) {
|
||||
var req structs.Request
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
deviceSettings := structs.Settings{Name: req.Name, Type: req.Type}
|
||||
|
||||
settingsJSON, err := json.Marshal(deviceSettings)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = os.WriteFile("devices/"+req.Uuid+".json", settingsJSON, 0644)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode("unauthorized")
|
||||
}
|
||||
|
||||
func PingLink(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("ping link")
|
||||
var req structs.Check
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(false)
|
||||
return
|
||||
}
|
||||
|
||||
key, keyErr := os.ReadFile("devices/" + req.Uuid + ".key")
|
||||
pin, pinErr := os.ReadFile("devices/" + req.Uuid + ".tmp")
|
||||
|
||||
encryptedKey, encErr := helper.EncryptAES(string(pin), string(key))
|
||||
|
||||
log.Println(encryptedKey, string(pin), string(key))
|
||||
|
||||
if keyErr == nil && pinErr == nil && encErr == nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(encryptedKey))
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(false)
|
||||
}
|
||||
|
||||
func StartLink(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("start link")
|
||||
var req structs.Check
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
pin := fmt.Sprintf("%04d", rand.Intn(10000))
|
||||
|
||||
deviceKey := helper.GenerateKey()
|
||||
|
||||
err = helper.SaveDeviceKey(req.Uuid, deviceKey)
|
||||
|
||||
if err == nil && helper.TempPinFile(req.Uuid, pin) {
|
||||
json.NewEncoder(w).Encode(pin)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func PollLink(w http.ResponseWriter, r *http.Request) {
|
||||
var req structs.Check
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(false)
|
||||
return
|
||||
}
|
||||
|
||||
if helper.CheckPinFile(req.Uuid) {
|
||||
json.NewEncoder(w).Encode(true)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(false)
|
||||
}
|
||||
|
||||
func RemoveLink(data string, w http.ResponseWriter, r *http.Request) {
|
||||
req := &structs.Check{}
|
||||
_, err := helper.ParseRequest(req, data, r)
|
||||
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(false)
|
||||
return
|
||||
}
|
||||
|
||||
err = os.Remove("devices/" + req.Uuid + ".key")
|
||||
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(false)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(true)
|
||||
}
|
||||
|
||||
func Handshake(w http.ResponseWriter, r *http.Request) {
|
||||
var req structs.Handshake
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
deviceKey, err := helper.GetKeyByUuid(req.Uuid)
|
||||
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(false)
|
||||
return
|
||||
}
|
||||
|
||||
decryptShake, _ := helper.DecryptAES(deviceKey, req.Shake)
|
||||
|
||||
if decryptShake == getDateStr() {
|
||||
os.Remove("devices/" + req.Uuid + ".tmp")
|
||||
|
||||
json.NewEncoder(w).Encode(true)
|
||||
return
|
||||
} else {
|
||||
os.Remove("devices/" + req.Uuid + ".key")
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(false)
|
||||
}
|
||||
|
||||
func getDateStr() string {
|
||||
date := time.Now()
|
||||
year, month, day := date.Date()
|
||||
formattedDate := fmt.Sprintf("%04d%02d%02d", year, month, day)
|
||||
return formattedDate
|
||||
}
|
||||
102
be/app/helper/api-helper.go
Normal file
102
be/app/helper/api-helper.go
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"be/app/structs"
|
||||
. "be/app/structs"
|
||||
)
|
||||
|
||||
func EndpointAccess(w http.ResponseWriter, r *http.Request) (bool, string) {
|
||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
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, ""
|
||||
} else if isLanRemote(ip) && isEndpointAllowed("Auth", r.URL.Path) {
|
||||
log.Println(r.URL.Path, "endpoint access: authorized")
|
||||
|
||||
data := decryptAuth(r)
|
||||
|
||||
return data != "", data
|
||||
}
|
||||
|
||||
log.Println(r.URL.Path, "endpoint access: not authorized or accessible")
|
||||
|
||||
return false, ""
|
||||
}
|
||||
|
||||
func isLocal(ip string) bool {
|
||||
return ip == "127.0.0.1" || ip == "::1"
|
||||
}
|
||||
|
||||
func isLanRemote(ip string) bool {
|
||||
return strings.HasPrefix(ip, "192.168.")
|
||||
}
|
||||
|
||||
func isEndpointAllowed(source string, endpoint string) bool {
|
||||
var endpoints, err = getAllowedEndpoints(source)
|
||||
if err != "" {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
if (endpoints != nil) && (len(endpoints) > 0) {
|
||||
for _, e := range endpoints {
|
||||
if e == endpoint {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func getAllowedEndpoints(source string) (endpoints []string, err string) {
|
||||
if source == "Local" {
|
||||
return Endpoints.Local, ""
|
||||
}
|
||||
if source == "Remote" {
|
||||
return Endpoints.Remote, ""
|
||||
}
|
||||
if source == "Auth" {
|
||||
return Endpoints.Auth, ""
|
||||
}
|
||||
|
||||
return []string{}, "No allowed endpoints"
|
||||
}
|
||||
|
||||
func decryptAuth(r *http.Request) string {
|
||||
var req structs.Authcall
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
|
||||
if err != nil || req.Uuid == "" || req.Data == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
deviceKey, errKey := GetKeyByUuid(req.Uuid)
|
||||
decryptData, errDec := DecryptAES(deviceKey, req.Data)
|
||||
|
||||
if errKey != nil && errDec != nil || decryptData == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
return decryptData
|
||||
}
|
||||
|
||||
func ParseRequest(req interface{}, data string, r *http.Request) (d interface{}, err error) {
|
||||
if data != "" {
|
||||
dataBytes := []byte(data)
|
||||
return req, json.Unmarshal(dataBytes, &req)
|
||||
} else {
|
||||
return req, json.NewDecoder(r.Body).Decode(&req)
|
||||
}
|
||||
}
|
||||
23
be/app/helper/browser-helper.go
Normal file
23
be/app/helper/browser-helper.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func OpenBrowser(url string) bool {
|
||||
var args []string
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
args = []string{"open"}
|
||||
case "windows":
|
||||
args = []string{"cmd", "/c", "start"}
|
||||
default:
|
||||
args = []string{"xdg-open"}
|
||||
}
|
||||
|
||||
cmd := exec.Command(args[0], append(args[1:], url)...)
|
||||
|
||||
return cmd.Start() == nil
|
||||
}
|
||||
46
be/app/helper/device-helper.go
Normal file
46
be/app/helper/device-helper.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TempPinFile(Uuid string, pin string) bool {
|
||||
log.Println("temp pin file", Uuid, pin)
|
||||
err := os.WriteFile("devices/"+Uuid+".tmp", []byte(pin), 0644)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return false
|
||||
}
|
||||
|
||||
time.AfterFunc(1*time.Minute, func() {
|
||||
log.Println("deleting", Uuid, pin)
|
||||
os.Remove("devices/" + Uuid + ".tmp")
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func CheckPinFile(Uuid string) bool {
|
||||
_, err := os.Stat("devices/" + Uuid + ".tmp")
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func SaveDeviceKey(Uuid string, key string) error {
|
||||
err := os.WriteFile("devices/"+Uuid+".key", []byte(key), 0644)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetKeyByUuid(Uuid string) (string, error) {
|
||||
data, err := os.ReadFile("devices/" + Uuid + ".key")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
105
be/app/helper/encrypt-helper.go
Normal file
105
be/app/helper/encrypt-helper.go
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func EncryptAES(key string, plaintext string) (string, error) {
|
||||
origData := []byte(plaintext)
|
||||
|
||||
// Create AES cipher
|
||||
block, err := aes.NewCipher(keyToBytes(key))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
blockSize := block.BlockSize()
|
||||
|
||||
origData = PKCS5Padding(origData, blockSize)
|
||||
|
||||
iv := []byte(EnvGet("MCRM__IV"))
|
||||
blockMode := cipher.NewCBCEncrypter(block, iv)
|
||||
|
||||
crypted := make([]byte, len(origData))
|
||||
blockMode.CryptBlocks(crypted, origData)
|
||||
|
||||
cryptedString := base64.StdEncoding.EncodeToString(crypted)
|
||||
|
||||
return cryptedString, nil
|
||||
}
|
||||
|
||||
func DecryptAES(key string, cryptedText string) (string, error) {
|
||||
crypted, err := base64.StdEncoding.DecodeString(cryptedText)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(keyToBytes(key))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
iv := []byte(EnvGet("MCRM__IV"))
|
||||
blockMode := cipher.NewCBCDecrypter(block, iv)
|
||||
|
||||
origData := make([]byte, len(crypted))
|
||||
|
||||
blockMode.CryptBlocks(origData, crypted)
|
||||
origData, err = PKCS5UnPadding(origData)
|
||||
|
||||
if err != nil || len(origData) <= 3 {
|
||||
return "", errors.New("invalid key")
|
||||
}
|
||||
|
||||
origDataString := string(origData)
|
||||
|
||||
return origDataString, nil
|
||||
}
|
||||
|
||||
func generateRandomString(length int) string {
|
||||
b := make([]byte, length)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
func GenerateKey() string {
|
||||
return strings.Replace(generateRandomString(24), "=", "", -1)
|
||||
}
|
||||
|
||||
func keyToBytes(key string) []byte {
|
||||
// Convert key to bytes
|
||||
keyBytes := []byte(key)
|
||||
|
||||
// If key is 4 characters, append salt
|
||||
if len(key) == 4 {
|
||||
keyBytes = []byte(key + EnvGet("MCRM__SALT"))
|
||||
}
|
||||
|
||||
return keyBytes
|
||||
}
|
||||
|
||||
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(ciphertext)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(ciphertext, padtext...)
|
||||
}
|
||||
|
||||
func PKCS5UnPadding(origData []byte) ([]byte, error) {
|
||||
length := len(origData)
|
||||
unpadding := int(origData[length-1])
|
||||
|
||||
if (unpadding >= length) || (unpadding == 0) {
|
||||
return nil, errors.New("unpadding error")
|
||||
}
|
||||
|
||||
return origData[:(length - unpadding)], nil
|
||||
}
|
||||
16
be/app/helper/env-helper.go
Normal file
16
be/app/helper/env-helper.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func EnvGet(key string) string {
|
||||
err := godotenv.Load("../.env")
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
return os.Getenv("VITE_" + key)
|
||||
}
|
||||
64
be/app/helper/macro-helper.go
Normal file
64
be/app/helper/macro-helper.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be/app/structs"
|
||||
|
||||
"github.com/go-vgo/robotgo"
|
||||
)
|
||||
|
||||
func FormatMacroFileName(s string) string {
|
||||
// Remove invalid characters
|
||||
re := regexp.MustCompile(`[\/\?\*\>\<\:\\"\|\n]`)
|
||||
s = re.ReplaceAllString(s, "")
|
||||
|
||||
// Replace spaces with underscores
|
||||
s = strings.ReplaceAll(s, " ", "_")
|
||||
|
||||
// Remove special characters
|
||||
re = regexp.MustCompile(`[!@#$%^&\(\)\[\]\{\}\~]`)
|
||||
s = re.ReplaceAllString(s, "")
|
||||
|
||||
// Truncate the string
|
||||
if len(s) > 255 {
|
||||
s = s[:255]
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(content, &steps)
|
||||
|
||||
return steps, err
|
||||
}
|
||||
|
||||
func RunMacroSteps(steps []structs.Step) {
|
||||
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)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
88
be/app/macro.go
Normal file
88
be/app/macro.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"be/app/helper"
|
||||
"be/app/structs"
|
||||
)
|
||||
|
||||
func SaveMacro(w http.ResponseWriter, r *http.Request) {
|
||||
var newMacro structs.NewMacro
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Println(string(body))
|
||||
|
||||
err = json.Unmarshal(body, &newMacro)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
stepsJSON, err := json.Marshal(newMacro.Steps)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile("../macros/"+helper.FormatMacroFileName(newMacro.Name)+".json", stepsJSON, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func ListMacros(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("listing macros")
|
||||
dir := "../macros"
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
var fileNames []string
|
||||
|
||||
for _, file := range files {
|
||||
filename := filepath.Base(file.Name())
|
||||
filename = strings.TrimSuffix(filename, filepath.Ext(filename))
|
||||
filename = strings.Replace(filename, "_", " ", -1)
|
||||
|
||||
fileNames = append(fileNames, filename)
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(fileNames)
|
||||
}
|
||||
|
||||
func DeleteMacro(w http.ResponseWriter, r *http.Request) {}
|
||||
|
||||
func PlayMacro(data string, w http.ResponseWriter, r *http.Request) {
|
||||
req := &structs.MacroRequest{}
|
||||
_, err := helper.ParseRequest(req, data, r)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
macro := req.Macro
|
||||
|
||||
var filename = helper.FormatMacroFileName(macro)
|
||||
var filepath = fmt.Sprintf("../macros/%s.json", filename)
|
||||
|
||||
macroFile, err := helper.ReadMacroFile(filepath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
helper.RunMacroSteps(macroFile)
|
||||
}
|
||||
37
be/app/structs/api-struct.go
Normal file
37
be/app/structs/api-struct.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package structs
|
||||
|
||||
type Allowed struct {
|
||||
Local []string
|
||||
Remote []string
|
||||
Auth []string
|
||||
}
|
||||
|
||||
var Endpoints = Allowed{
|
||||
Local: []string{
|
||||
"/macro/record",
|
||||
"/macro/list",
|
||||
"/macro/delete",
|
||||
"/macro/play",
|
||||
"/device/list",
|
||||
"/device/access/check",
|
||||
"/device/access/request",
|
||||
"/device/link/ping",
|
||||
"/device/link/start",
|
||||
"/device/link/poll",
|
||||
"/device/link/remove",
|
||||
"/device/handshake",
|
||||
},
|
||||
Remote: []string{
|
||||
"/macro/list",
|
||||
"/device/access/check",
|
||||
"/device/access/request",
|
||||
"/device/link/ping",
|
||||
"/device/link/end",
|
||||
"/device/handshake",
|
||||
"/device/auth",
|
||||
},
|
||||
Auth: []string{
|
||||
"/macro/play",
|
||||
"/device/link/remove",
|
||||
},
|
||||
}
|
||||
31
be/app/structs/device-struct.go
Normal file
31
be/app/structs/device-struct.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package structs
|
||||
|
||||
type Settings struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type RemoteWebhook struct {
|
||||
Event string `json:"event"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
type Check struct {
|
||||
Uuid string `json:"uuid"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type Handshake struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Shake string `json:"shake"`
|
||||
}
|
||||
|
||||
type Authcall struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Data string `json:"d"`
|
||||
}
|
||||
19
be/app/structs/macro-struct.go
Normal file
19
be/app/structs/macro-struct.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package structs
|
||||
|
||||
type MacroRequest struct {
|
||||
Macro string `json:"macro"`
|
||||
}
|
||||
|
||||
type Step struct {
|
||||
Type string `json:"type"`
|
||||
Key string `json:"key"`
|
||||
Code string `json:"code"`
|
||||
Location int `json:"location"`
|
||||
Direction string `json:"direction"`
|
||||
Value int `json:"value"`
|
||||
}
|
||||
|
||||
type NewMacro struct {
|
||||
Name string `json:"name"`
|
||||
Steps []Step `json:"steps"`
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue