diff --git a/be/app/api.go b/be/app/api.go index d2d79c6..0f169ef 100644 --- a/be/app/api.go +++ b/be/app/api.go @@ -24,23 +24,46 @@ func ApiCORS(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http } func ApiGet(w http.ResponseWriter, r *http.Request) { - file := "" // base directory + // file := "" // base directory + + // if strings.Contains(r.URL.Path, "/config.js") { + // file = "../public/config.js" + // } else if r.URL.Path != "/" { + // file = "../public" + r.URL.Path // request + // } + // MCRMLog("ApiGet Path: ", r.URL.Path, "; File: ", file) + + // contentType := mime.TypeByExtension(filepath.Ext(file)) // get content type + + // if contentType == "" { + // file = "../public/index.html" // default + // } + + // MCRMLog("ApiGet File: ", file) + + // http.ServeFile(w, r, file) + root, err := filepath.Abs("../public") + if err != nil { + MCRMLog("ApiGet Abs Error: ", err) + return + } + + var file string if strings.Contains(r.URL.Path, "/config.js") { - file = "../public/config.js" + file = filepath.Join(root, "config.js") + w.Header().Set("Content-Type", "text/javascript") // set content type header } else if r.URL.Path != "/" { - file = "../public" + r.URL.Path // request + file = filepath.Join(root, r.URL.Path) } contentType := mime.TypeByExtension(filepath.Ext(file)) // get content type - if contentType != "" { - w.Header().Set("Content-Type", contentType) // set content type header + if contentType == "" { + file = filepath.Join(root, "index.html") } - if contentType == "" { - file = "../public/index.html" // default - } + // MCRMLog("ApiGet Path: ", r.URL.Path, "; File: ", file, "; Content-Type: ", contentType) http.ServeFile(w, r, file) } @@ -100,6 +123,9 @@ func ApiAuth(data string, w http.ResponseWriter, r *http.Request) { PlayMacro(data, w, r) case "/device/link/remove": RemoveLink(data, w, r) + case "/panel/list": + MCRMLog("Authenticated Panellist") + PanelList(w, r) case "/panel/get": GetPanel(data, w, r) } diff --git a/be/app/device.go b/be/app/device.go index 88e05c3..4ad7ed3 100644 --- a/be/app/device.go +++ b/be/app/device.go @@ -44,8 +44,11 @@ func GetServerIP(w http.ResponseWriter, r *http.Request) { func DeviceList(w http.ResponseWriter, r *http.Request) { dir := "devices" + files, err := os.ReadDir(dir) if err != nil { + os.MkdirAll(dir, 0600) + files = nil MCRMLog("DeviceList Error: ", err) } @@ -112,8 +115,6 @@ func DeviceAccessCheck(w http.ResponseWriter, r *http.Request) { MCRMLog("DeviceAccessCheck: UUID: ", req.Uuid, "; Access: Unlinked") json.NewEncoder(w).Encode("unlinked") } - - return } func DeviceAccessRequest(w http.ResponseWriter, r *http.Request) { @@ -157,9 +158,6 @@ func PingLink(w http.ResponseWriter, r *http.Request) { key, keyErr := os.ReadFile("devices/" + req.Uuid + ".key") pin, pinErr := os.ReadFile("devices/" + req.Uuid + ".tmp") - // MCRMLog("PingLink UUID: ", req.Uuid) - // MCRMLog("PingLink Key: ", string(key), "; Pin: ", string(pin)) - encryptedKey, encErr := helper.EncryptAES(string(pin), string(key)) if keyErr == nil && pinErr == nil && encErr == nil { @@ -192,13 +190,11 @@ func StartLink(w http.ResponseWriter, r *http.Request) { errKey := helper.SaveDeviceKey(req.Uuid, deviceKey) savedPin, errPin := helper.TempPinFile(req.Uuid, pin) - if errKey == nil && errPin == nil && savedPin == true { + if errKey == nil && errPin == nil && savedPin { json.NewEncoder(w).Encode(pin) } else { - MCRMLog("StartLink Error: errKey:", err, "; errPin:", err) + MCRMLog("StartLink Error: errKey:", errKey, "; errPin:", errPin) } - - return } func PollLink(w http.ResponseWriter, r *http.Request) { diff --git a/be/app/helper/encrypt-helper.go b/be/app/helper/encrypt-helper.go index d99707c..551ac3f 100644 --- a/be/app/helper/encrypt-helper.go +++ b/be/app/helper/encrypt-helper.go @@ -7,9 +7,7 @@ import ( "crypto/rand" "encoding/base64" "errors" - "math" mathRand "math/rand" - "strconv" "strings" ) @@ -75,10 +73,11 @@ func GenerateRandomString(length int) string { } func GenerateRandomIntegerString(length int) string { - min := int64(0) - max := int64(math.Pow10(length)) - randInt := min + mathRand.Int63()%(max-min+1) - return strconv.FormatInt(randInt, 10) + var sb strings.Builder + for i := 0; i < length; i++ { + sb.WriteByte('0' + byte(mathRand.Intn(10))) + } + return sb.String() } func GenerateKey() string { diff --git a/be/app/helper/env-helper.go b/be/app/helper/env-helper.go index 2e895b6..9fc3c9b 100644 --- a/be/app/helper/env-helper.go +++ b/be/app/helper/env-helper.go @@ -13,8 +13,8 @@ var configPath = "../public/config.js" func EnvGet(key string) string { if !configFileExists() { - createConfigFile(configPath) - checkFeDevDir() + CreateConfigFile(configPath) + CheckFeDevDir() } data, err := os.ReadFile(configPath) @@ -41,7 +41,8 @@ func configFileExists() bool { return err == nil } -func checkFeDevDir() { +func CheckFeDevDir() { + log.Println("Checking FE dev directory...") _, err := os.Stat("../fe") if err != nil { @@ -64,7 +65,7 @@ func copyConfigToFe() { } } -func createConfigFile(filename string) { +func CreateConfigFile(filename string) { port, _ := findOpenPort() saltKey := GenerateKey() salt := saltKey[:28] diff --git a/be/app/structs/api-struct.go b/be/app/structs/api-struct.go index c8cfbe5..f3be4d1 100644 --- a/be/app/structs/api-struct.go +++ b/be/app/structs/api-struct.go @@ -26,19 +26,18 @@ var Endpoints = Allowed{ "/panel/save/json", }, Remote: []string{ - "/macro/list", "/device/access/check", "/device/access/request", + "/device/server/ip", "/device/link/ping", "/device/link/end", "/device/handshake", "/device/auth", - "/panel/list", - // "/panel/get", }, Auth: []string{ "/macro/play", "/device/link/remove", "/panel/get", + "/panel/list", }, } diff --git a/be/go.mod b/be/go.mod index ae82a5b..c22028b 100644 --- a/be/go.mod +++ b/be/go.mod @@ -2,10 +2,7 @@ module be go 1.24.0 -require ( - github.com/go-vgo/robotgo v0.110.6 - github.com/joho/godotenv v1.5.1 -) +require github.com/go-vgo/robotgo v0.110.7 require ( github.com/dblohm7/wingoes v0.0.0-20240820181039-f2b84150679e // indirect diff --git a/be/go.sum b/be/go.sum index 8bbd9cb..9c09ecb 100644 --- a/be/go.sum +++ b/be/go.sum @@ -11,16 +11,14 @@ github.com/gen2brain/shm v0.1.1/go.mod h1:UgIcVtvmOu+aCJpqJX7GOtiN7X2ct+TKLg4RTx github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= -github.com/go-vgo/robotgo v0.110.6 h1:1tOxlmTXYg6F3Xs8IT++331MxY2nZ+Q3B6eW312llbo= -github.com/go-vgo/robotgo v0.110.6/go.mod h1:eBUjTHY1HYjzdi1+UWJUbxB+b9gE+l4Ei7vQU/9SnLw= +github.com/go-vgo/robotgo v0.110.7 h1:4scqQrJOBHoFCfcMROYEVFBxHvB3nF/UN6DWoRIFzBE= +github.com/go-vgo/robotgo v0.110.7/go.mod h1:eBUjTHY1HYjzdi1+UWJUbxB+b9gE+l4Ei7vQU/9SnLw= github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4= github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kbinani/screenshot v0.0.0-20250118074034-a3924b7bbc8c h1:1IlzDla/ZATV/FsRn1ETf7ir91PHS2mrd4VMunEtd9k= github.com/kbinani/screenshot v0.0.0-20250118074034-a3924b7bbc8c/go.mod h1:Pmpz2BLf55auQZ67u3rvyI2vAQvNetkK/4zYUmpauZQ= github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 h1:7UMa6KCCMjZEMDtTVdcGu0B1GmmC7QJKiCCjyTAWQy0= diff --git a/be/macrame b/be/macrame deleted file mode 100644 index 54cd577..0000000 Binary files a/be/macrame and /dev/null differ diff --git a/be/macrame.exe b/be/macrame.exe deleted file mode 100644 index 92c1313..0000000 Binary files a/be/macrame.exe and /dev/null differ diff --git a/be/main.exe b/be/main.exe deleted file mode 100644 index 54cd577..0000000 Binary files a/be/main.exe and /dev/null differ diff --git a/be/main.go b/be/main.go index 9dacc72..11cdf4d 100644 --- a/be/main.go +++ b/be/main.go @@ -3,6 +3,8 @@ package main import ( "log" "net/http" + "os" + "strings" "be/app" "be/app/helper" @@ -11,6 +13,8 @@ import ( func main() { app.MCRMLogInit() + switchToBeDir() + if helper.EnvGet("MCRM__PORT") == "" { app.MCRMLog("Error: MCRM__PORT is not set") } @@ -19,19 +23,29 @@ func main() { apiInit(w, r) }) - log.Println("Listening on http://localhost:" + helper.EnvGet("MCRM__PORT")) + // helper.OpenBrowser("http://localhost:" + helper.EnvGet("MCRM__PORT")) - helper.OpenBrowser("http://localhost:" + helper.EnvGet("MCRM__PORT")) + app.MCRMLog("Listening on http://localhost:" + helper.EnvGet("MCRM__PORT")) app.MCRMLog(http.ListenAndServe(":"+helper.EnvGet("MCRM__PORT"), nil)) +} +func switchToBeDir() { + cwd, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + if !strings.HasSuffix(cwd, "be") { + err := os.Chdir("be") + if err != nil { + log.Fatal(err) + } + } } func apiInit(w http.ResponseWriter, r *http.Request) { app.ApiCORS(w, r) - app.MCRMLog("Remote IP: " + r.RemoteAddr) - if r.Method == "GET" { app.ApiGet(w, r) } else if r.Method == "POST" { diff --git a/be/setup/setup.go b/be/setup/setup.go new file mode 100644 index 0000000..192ea35 --- /dev/null +++ b/be/setup/setup.go @@ -0,0 +1,14 @@ +package main + +import "be/app/helper" + +func main() { + helper.CreateConfigFile("../public/config.js") + helper.CheckFeDevDir() + + port := helper.EnvGet("MCRM__PORT") + + helper.MakeCaddyFile("CaddyFile", port) + + return +}