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) {
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)
}

View file

@ -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) {

View file

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

View file

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

View file

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