-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
153 lines (128 loc) · 3.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"flag"
"log"
"os"
"github.com/getlantern/systray"
"github.com/gobuffalo/packr/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/iimrudy/prismacontroller/structures"
"github.com/iimrudy/prismacontroller/utils"
"gopkg.in/yaml.v2"
)
var (
Config structures.Configuration
fiberAPP *fiber.App
StaicDataBox *packr.Box
Path string
)
func init() {
flag.StringVar(&Path, "path", "./", "Configuration & Icons location")
flag.Parse() // parse args
file, err := os.OpenFile(Path+"logs.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
log.SetOutput(file)
log.SetPrefix("[PrismaController] ")
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
content, err := utils.ReadFileToString(Path + "config.yml")
if err != nil {
log.Fatalln(err.Error())
}
err = yaml.Unmarshal([]byte(*content), &Config)
if err != nil {
log.Fatalln(err.Error())
}
log.Println("Using path" + Path)
}
func main() {
StaicDataBox = packr.New("Static Assets", "/static")
go systray.Run(onReady, onExit)
log.Println("PrismaController Started")
fiberAPP = fiber.New()
fiberAPP.Post("/commands/execute", func(c *fiber.Ctx) error {
r := new(structures.ClickButtonRequest) // new instance
c.BodyParser(r) // parse json
success := false
message := "command not found"
if r.Password == Config.PASSWORD {
for _, cmd := range Config.BUTTONS {
if cmd.Name == r.CommandName {
go func() {
err := utils.RunCommand(cmd)
if err != nil {
log.Println(err.Error())
}
}()
success = true
message = "executed"
break
}
}
} else {
success = false
message = "Invalid password."
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{"success": success, "message": message})
})
fiberAPP.Use("/", filesystem.New(filesystem.Config{
Root: StaicDataBox,
}))
fiberAPP.Static("/icons", Path+"icons")
fiberAPP.Post("/commands/get", func(c *fiber.Ctx) error {
r := new(structures.PasswordRequest) // new instance
c.BodyParser(r) // parse json
success := false
var message interface{}
message = "Invalid password."
if r.Password == Config.PASSWORD {
success = true
mcmds := []structures.MinifiedButton{}
for _, cmd := range Config.BUTTONS {
mcmds = append(mcmds, utils.ButtonToMinifiedButton(cmd))
}
message = mcmds
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{"success": success, "message": message})
})
fiberAPP.Post("/authorize", func(c *fiber.Ctx) error {
r := new(structures.PasswordRequest) // new instance
c.BodyParser(r) // parse json
success := false
var message interface{}
message = "Invalid password."
if r.Password == Config.PASSWORD {
success = true
message = "Valid password."
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{"success": success, "message": message})
})
fiberAPP.Listen(Config.HOST + ":" + Config.PORT)
}
func onReady() {
s, err := StaicDataBox.FindString("favicon.ico")
if err != nil {
log.Panicln(err.Error())
}
systray.SetIcon([]byte(s))
systray.SetTitle("PrismaController")
systray.SetTooltip("PrismaController Menu")
quitBtn := systray.AddMenuItem("Quit", "Quit PrismaController")
go func() {
for {
select {
case <-quitBtn.ClickedCh:
systray.Quit()
return
}
}
}()
// Sets the icon of a menu item. Only available on Mac and Windows.
}
func onExit() {
log.Println("Exiting now.")
fiberAPP.Shutdown()
os.Exit(0)
}