/
main.go
164 lines (145 loc) · 4.07 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
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"flag"
"html/template"
"log"
"net/http"
"strconv"
"strings"
"github.com/vblazhnov/go-http-digest-auth"
"github.com/vblazhnov/RemoteControl/interfaces"
"github.com/vblazhnov/RemoteControl/plugins/keyboard"
"github.com/vblazhnov/RemoteControl/plugins/mouse"
"github.com/vblazhnov/RemoteControl/plugins/shutdown"
"github.com/vblazhnov/RemoteControl/utils"
)
var (
serverAddress string
serverPort uint
user string
password string
certTLSFilePath string
keyTLSFilePath string
wrapper auth.Wrapper
)
func init() {
flag.StringVar(&serverAddress, "host", "0.0.0.0", "the host that server binds to")
flag.UintVar(&serverPort, "port", 1234, "the port that server binds to")
flag.StringVar(&user, "user", "user", "username for auth while using the service")
flag.StringVar(&password, "password", "iospassword", "password for auth while using the service")
flag.StringVar(&certTLSFilePath, "TLSCertPath", "", "path to cert file for use TLS")
flag.StringVar(&keyTLSFilePath, "TLSKeyPath", "", "path to key file for use TLS")
}
func main() {
flag.Parse()
wrapper = auth.NewBaseAuth(auth.Info{user, password, serverAddress})
initPlugins()
initResources()
startServer()
}
func startServer() {
var err error
addr := serverAddress + ":" + strconv.FormatUint(uint64(serverPort), 10)
if certTLSFilePath == "" || keyTLSFilePath == "" {
err = http.ListenAndServe(addr, nil)
} else {
err = http.ListenAndServeTLS(addr, certTLSFilePath, keyTLSFilePath, nil)
}
if err != nil {
log.Fatal(err)
}
}
var resources map[string][]byte
func initResources() {
resources = make(map[string][]byte, 3)
appendResources(utils.ReadFilesInDir("static\\css"))
appendResources(utils.ReadFilesInDir("static\\js"))
handleFilesMap(resources)
}
func appendResources(new map[string][]byte) {
for k, v := range new {
k = "/" + strings.Replace(k, "\\", "/", -1)
resources[k] = v
}
}
func handleFilesMap(files map[string][]byte) {
for k := range files {
handleRequest(k, func(w http.ResponseWriter, r *http.Request) {
path := r.RequestURI
res, ok := resources[path]
if !ok {
http.NotFound(w, r)
} else {
if strings.Contains(path, ".css") {
w.Header().Add("Content-Type", "text/css")
}
w.Write(res)
}
})
}
}
func initPlugins() {
plugins := []interfaces.Plugin{
shutdown.Control{},
mouse.Control{},
keyboard.Control{},
}
registerPlugins(plugins)
}
func registerPlugins(plugins []interfaces.Plugin) {
var err error
menus = make(map[template.URL]string, len(plugins))
allPluginsData = make([]func() template.HTML, 0)
mainTemplate, err = template.New("mainTemplate").Parse(string(utils.ReadHTML("\\static\\template.html")))
if err != nil {
log.Fatal(err)
}
for _, p := range plugins {
for _, ep := range p.GetHandlers() {
switch ep.Type() {
case interfaces.EndPointAPI:
handleRequest("/api/v1"+ep.Path(), ep.Handler())
case interfaces.EndPointContent:
handleRequest(ep.Path(), ep.Handler())
default:
log.Println("Incorrect plugin endpoint type: ", ep)
}
}
for _, static := range p.GetMainContent() {
menus[static.Path()] = static.Title()
allPluginsData = append(allPluginsData, static.Data)
handleStatic(static.Path(), static.Data, static.Title)
}
}
mainPageData := func() template.HTML {
html := template.HTML("")
for _, v := range allPluginsData {
html += v()
}
return html
}
handleStatic("/", mainPageData, func() string { return "Remote Control" })
}
var (
mainTemplate *template.Template
menus map[template.URL]string
allPluginsData []func() template.HTML
)
func handleStatic(path template.URL, data func() template.HTML, title func() string) {
templateInner := struct {
Title string
MenuItems map[template.URL]string
Content template.HTML
}{
title(),
menus,
data(),
}
handleFunc := func(w http.ResponseWriter, r *http.Request) {
mainTemplate.Execute(w, templateInner)
}
http.HandleFunc(string(path), wrapper.Wrap(handleFunc))
}
func handleRequest(path string, fun http.HandlerFunc) {
http.HandleFunc(path, wrapper.Wrap(fun))
}