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

View file

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

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.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=

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

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

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
}