Alpha version be update

This commit is contained in:
Jesse Malotaux 2025-04-17 11:20:27 +02:00
parent f5f6f25a4a
commit e1b13b3930
12 changed files with 86 additions and 42 deletions

View file

@ -24,23 +24,46 @@ func ApiCORS(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http
} }
func ApiGet(w http.ResponseWriter, r *http.Request) { 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") { 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 != "/" { } 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 contentType := mime.TypeByExtension(filepath.Ext(file)) // get content type
if contentType != "" { if contentType == "" {
w.Header().Set("Content-Type", contentType) // set content type header file = filepath.Join(root, "index.html")
} }
if contentType == "" { // MCRMLog("ApiGet Path: ", r.URL.Path, "; File: ", file, "; Content-Type: ", contentType)
file = "../public/index.html" // default
}
http.ServeFile(w, r, file) http.ServeFile(w, r, file)
} }
@ -100,6 +123,9 @@ func ApiAuth(data string, w http.ResponseWriter, r *http.Request) {
PlayMacro(data, w, r) PlayMacro(data, w, r)
case "/device/link/remove": case "/device/link/remove":
RemoveLink(data, w, r) RemoveLink(data, w, r)
case "/panel/list":
MCRMLog("Authenticated Panellist")
PanelList(w, r)
case "/panel/get": case "/panel/get":
GetPanel(data, w, r) GetPanel(data, w, r)
} }

View file

@ -44,8 +44,11 @@ func GetServerIP(w http.ResponseWriter, r *http.Request) {
func DeviceList(w http.ResponseWriter, r *http.Request) { func DeviceList(w http.ResponseWriter, r *http.Request) {
dir := "devices" dir := "devices"
files, err := os.ReadDir(dir) files, err := os.ReadDir(dir)
if err != nil { if err != nil {
os.MkdirAll(dir, 0600)
files = nil
MCRMLog("DeviceList Error: ", err) MCRMLog("DeviceList Error: ", err)
} }
@ -112,8 +115,6 @@ func DeviceAccessCheck(w http.ResponseWriter, r *http.Request) {
MCRMLog("DeviceAccessCheck: UUID: ", req.Uuid, "; Access: Unlinked") MCRMLog("DeviceAccessCheck: UUID: ", req.Uuid, "; Access: Unlinked")
json.NewEncoder(w).Encode("unlinked") json.NewEncoder(w).Encode("unlinked")
} }
return
} }
func DeviceAccessRequest(w http.ResponseWriter, r *http.Request) { 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") key, keyErr := os.ReadFile("devices/" + req.Uuid + ".key")
pin, pinErr := os.ReadFile("devices/" + req.Uuid + ".tmp") 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)) encryptedKey, encErr := helper.EncryptAES(string(pin), string(key))
if keyErr == nil && pinErr == nil && encErr == nil { 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) errKey := helper.SaveDeviceKey(req.Uuid, deviceKey)
savedPin, errPin := helper.TempPinFile(req.Uuid, pin) 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) json.NewEncoder(w).Encode(pin)
} else { } else {
MCRMLog("StartLink Error: errKey:", err, "; errPin:", err) MCRMLog("StartLink Error: errKey:", errKey, "; errPin:", errPin)
} }
return
} }
func PollLink(w http.ResponseWriter, r *http.Request) { func PollLink(w http.ResponseWriter, r *http.Request) {

View file

@ -7,9 +7,7 @@ import (
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"errors" "errors"
"math"
mathRand "math/rand" mathRand "math/rand"
"strconv"
"strings" "strings"
) )
@ -75,10 +73,11 @@ func GenerateRandomString(length int) string {
} }
func GenerateRandomIntegerString(length int) string { func GenerateRandomIntegerString(length int) string {
min := int64(0) var sb strings.Builder
max := int64(math.Pow10(length)) for i := 0; i < length; i++ {
randInt := min + mathRand.Int63()%(max-min+1) sb.WriteByte('0' + byte(mathRand.Intn(10)))
return strconv.FormatInt(randInt, 10) }
return sb.String()
} }
func GenerateKey() string { func GenerateKey() string {

View file

@ -13,8 +13,8 @@ var configPath = "../public/config.js"
func EnvGet(key string) string { func EnvGet(key string) string {
if !configFileExists() { if !configFileExists() {
createConfigFile(configPath) CreateConfigFile(configPath)
checkFeDevDir() CheckFeDevDir()
} }
data, err := os.ReadFile(configPath) data, err := os.ReadFile(configPath)
@ -41,7 +41,8 @@ func configFileExists() bool {
return err == nil return err == nil
} }
func checkFeDevDir() { func CheckFeDevDir() {
log.Println("Checking FE dev directory...")
_, err := os.Stat("../fe") _, err := os.Stat("../fe")
if err != nil { if err != nil {
@ -64,7 +65,7 @@ func copyConfigToFe() {
} }
} }
func createConfigFile(filename string) { func CreateConfigFile(filename string) {
port, _ := findOpenPort() port, _ := findOpenPort()
saltKey := GenerateKey() saltKey := GenerateKey()
salt := saltKey[:28] salt := saltKey[:28]

View file

@ -26,19 +26,18 @@ var Endpoints = Allowed{
"/panel/save/json", "/panel/save/json",
}, },
Remote: []string{ Remote: []string{
"/macro/list",
"/device/access/check", "/device/access/check",
"/device/access/request", "/device/access/request",
"/device/server/ip",
"/device/link/ping", "/device/link/ping",
"/device/link/end", "/device/link/end",
"/device/handshake", "/device/handshake",
"/device/auth", "/device/auth",
"/panel/list",
// "/panel/get",
}, },
Auth: []string{ Auth: []string{
"/macro/play", "/macro/play",
"/device/link/remove", "/device/link/remove",
"/panel/get", "/panel/get",
"/panel/list",
}, },
} }

View file

@ -2,10 +2,7 @@ module be
go 1.24.0 go 1.24.0
require ( require github.com/go-vgo/robotgo v0.110.7
github.com/go-vgo/robotgo v0.110.6
github.com/joho/godotenv v1.5.1
)
require ( require (
github.com/dblohm7/wingoes v0.0.0-20240820181039-f2b84150679e // indirect github.com/dblohm7/wingoes v0.0.0-20240820181039-f2b84150679e // indirect

View file

@ -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.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 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= 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.7 h1:4scqQrJOBHoFCfcMROYEVFBxHvB3nF/UN6DWoRIFzBE=
github.com/go-vgo/robotgo v0.110.6/go.mod h1:eBUjTHY1HYjzdi1+UWJUbxB+b9gE+l4Ei7vQU/9SnLw= 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 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= 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 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 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 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= 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 h1:1IlzDla/ZATV/FsRn1ETf7ir91PHS2mrd4VMunEtd9k=
github.com/kbinani/screenshot v0.0.0-20250118074034-a3924b7bbc8c/go.mod h1:Pmpz2BLf55auQZ67u3rvyI2vAQvNetkK/4zYUmpauZQ= 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= github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 h1:7UMa6KCCMjZEMDtTVdcGu0B1GmmC7QJKiCCjyTAWQy0=

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -3,6 +3,8 @@ package main
import ( import (
"log" "log"
"net/http" "net/http"
"os"
"strings"
"be/app" "be/app"
"be/app/helper" "be/app/helper"
@ -11,6 +13,8 @@ import (
func main() { func main() {
app.MCRMLogInit() app.MCRMLogInit()
switchToBeDir()
if helper.EnvGet("MCRM__PORT") == "" { if helper.EnvGet("MCRM__PORT") == "" {
app.MCRMLog("Error: MCRM__PORT is not set") app.MCRMLog("Error: MCRM__PORT is not set")
} }
@ -19,19 +23,29 @@ func main() {
apiInit(w, r) 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)) 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) { func apiInit(w http.ResponseWriter, r *http.Request) {
app.ApiCORS(w, r) app.ApiCORS(w, r)
app.MCRMLog("Remote IP: " + r.RemoteAddr)
if r.Method == "GET" { if r.Method == "GET" {
app.ApiGet(w, r) app.ApiGet(w, r)
} else if r.Method == "POST" { } else if r.Method == "POST" {

14
be/setup/setup.go Normal file
View file

@ -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
}