-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
182 lines (165 loc) · 5.57 KB
/
web.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"html/template"
"io"
"net/http"
"reflect"
"slices"
"strings"
)
func runHTTPServer() {
router := mux.NewRouter()
router.HandleFunc("/", indexHandler).Methods("GET")
router.HandleFunc("/save_config", saveConfigHandler).Methods("POST")
router.HandleFunc("/otp", otpGetHandler).Methods("GET")
router.HandleFunc("/otp", otpPostHandler).Methods("POST")
http.ListenAndServe("0.0.0.0:5000", router)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
exportedConf, err := currentConf.Export()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl := template.New("index.html")
tmpl, err = tmpl.ParseFiles("template/index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, exportedConf)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func saveConfigHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// get conf meta
confExport, err := currentConf.Export()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// param check
for _, confSection := range confExport.ConfigSections {
for _, config := range confSection.Configs {
formValues := r.Form[confSection.Section+"__"+config.ConfigKey]
isRequired := (confSection.Section == GlobalConfigSectionName && config.Required) ||
(slices.Contains(r.Form[GlobalConfigSectionName+"__"+ConfKeyEnabledWebsites], confSection.Section) && config.Required)
if isRequired && len(formValues) == 0 {
json.NewEncoder(w).Encode(map[string]interface{}{"success": false,
"message": fmt.Sprintf(`Config "%s" is required`, config.ConfigName)})
return
}
if config.Type == FieldTypeString && len(formValues) > 0 {
value := formValues[0]
if isRequired && value == "" {
json.NewEncoder(w).Encode(map[string]interface{}{"success": false,
"message": fmt.Sprintf(`Config "%s" under section "%s" is required`, config.ConfigName, confSection.DisplayName)})
return
}
} else if config.Type == FieldTypeBool && len(formValues) > 0 {
value := formValues[0]
if value != TrueLiteral && value != FalseLiteral {
json.NewEncoder(w).Encode(map[string]interface{}{"success": false,
"message": fmt.Sprintf(`Invalid value for bool option "%s"`, config.ConfigName)})
return
}
}
}
}
// set values
for _, confSection := range confExport.ConfigSections {
for _, config := range confSection.Configs {
formValues := r.Form[confSection.Section+"__"+config.ConfigKey]
if len(formValues) == 0 {
continue
}
var structToChange reflect.Value
if confSection.Section == GlobalConfigSectionName {
// is global option
structToChange = reflect.ValueOf(currentConf)
} else {
// is agent option
if currentConf.AgentConfs[confSection.Section] == nil {
currentConf.AgentConfs[confSection.Section] = &AgentConf{}
}
structToChange = reflect.ValueOf(currentConf.AgentConfs[confSection.Section])
}
var err error
if config.Type == FieldTypeString {
value := formValues[0]
err = SetValueByJSONTag(structToChange, config.ConfigKey, value)
} else if config.Type == FieldTypeStringList {
value := strings.Split(formValues[0], ",")
if len(value) == 1 && value[0] == "" {
value = []string{}
}
err = SetValueByJSONTag(structToChange, config.ConfigKey, value)
} else if config.Type == FieldTypeCheckbox {
value := formValues
err = SetValueByJSONTag(structToChange, config.ConfigKey, value)
} else if config.Type == FieldTypeRadio {
value := formValues[0]
err = SetValueByJSONTag(structToChange, config.ConfigKey, value)
} else if config.Type == FieldTypeBool {
value := formValues[0]
if value == "True" {
err = SetValueByJSONTag(structToChange, config.ConfigKey, true)
} else {
err = SetValueByJSONTag(structToChange, config.ConfigKey, false)
}
} else {
json.NewEncoder(w).Encode(map[string]interface{}{"success": false,
"message": fmt.Sprintf("unknown field type %s", config.Type)})
return
}
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"success": false,
"message": fmt.Sprintf("set value for %s__%s failed: %s", confSection.Section, config.ConfigKey, err.Error())})
return
}
}
}
err = currentConf.WriteDisk()
if err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"success": false,
"message": fmt.Sprintf("write config to disk failed: %s", err.Error())})
return
}
json.NewEncoder(w).Encode(map[string]interface{}{"success": true})
}
func otpGetHandler(w http.ResponseWriter, r *http.Request) {
select {
case reqAgent := <-otpRequestChan:
json.NewEncoder(w).Encode(map[string]interface{}{"prompt": fmt.Sprintf("%s requires OTP. Please check your mailbox and input OTP here:", reqAgent)})
default:
json.NewEncoder(w).Encode(map[string]interface{}{"prompt": nil})
}
}
func otpPostHandler(w http.ResponseWriter, r *http.Request) {
type Payload struct {
Input string `json:"input"`
}
body, _ := io.ReadAll(r.Body)
defer r.Body.Close()
var p Payload
err := json.Unmarshal(body, &p)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
select {
case otpChan <- p.Input:
json.NewEncoder(w).Encode(map[string]interface{}{"success": true})
default:
json.NewEncoder(w).Encode(map[string]interface{}{"success": false, "message": "No website requires OTP"})
}
}