-
Notifications
You must be signed in to change notification settings - Fork 1
/
webserver.go
171 lines (132 loc) · 4.39 KB
/
webserver.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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/jangxx/go-poclient"
"github.com/GeertJohan/go.rice"
"github.com/gorilla/mux"
)
func initWebserver(config WebserverConfig) *http.Server {
r := mux.NewRouter()
staticBox := rice.MustFindBox("./webinterface/static/")
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(staticBox.HTTPBox())))
buildBox := rice.MustFindBox("./webinterface/build/")
r.PathPrefix("/build/").Handler(http.StripPrefix("/build/", http.FileServer(buildBox.HTTPBox())))
r.HandleFunc("/", IndexRoute)
r.HandleFunc("/login", serveSingleFile(buildBox, "html/login.html")).Methods("GET")
r.HandleFunc("/registerdevice", serveSingleFile(buildBox, "html/registerdevice.html")).Methods("GET")
r.HandleFunc("/done", serveSingleFile(buildBox, "html/done.html")).Methods("GET")
apiRouter := r.PathPrefix("/api").Subrouter()
apiRouter.HandleFunc("/login", LoginRoute).Methods("POST")
apiRouter.HandleFunc("/register", RegisterRoute).Methods("POST")
apiRouter.HandleFunc("/logout", LogoutRoute).Methods("POST")
apiRouter.HandleFunc("/userinfo", UserinfoRoute).Methods("GET")
srv := &http.Server{
Handler: r,
Addr: config.Addr + ":" + config.Port,
}
log.Printf("Server is listening on %s:%s\n", config.Addr, config.Port)
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Printf("Server exited with error: %s\n", err)
}
}()
return srv
}
func IndexRoute(resp http.ResponseWriter, req *http.Request) {
if loggedin, registered := pushover.GetStatus(); !loggedin {
http.Redirect(resp, req, "/login", http.StatusFound)
} else if loggedin && !registered {
http.Redirect(resp, req, "/registerdevice", http.StatusFound)
} else {
http.Redirect(resp, req, "/done", http.StatusFound)
}
}
func LoginRoute(resp http.ResponseWriter, req *http.Request) {
requestData := struct {
Email string `json:"email"`
Password string `json:"password"`
Code2FA string `json:"twofacode"`
}{}
err := json.NewDecoder(req.Body).Decode(&requestData)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
if requestData.Code2FA == "" {
err = pushover.Login(requestData.Email, requestData.Password)
} else {
err = pushover.Login2FA(requestData.Email, requestData.Password, requestData.Code2FA)
}
if _, is2faerror := err.(*poclient.Missing2FAError); is2faerror {
resp.Write([]byte("2FA_MISSING"))
return
}
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
//store user info in config and return successfully
config.Userid, config.Usersecret = pushover.User()
config.Display_Username = requestData.Email
err = storeConfig(config, "settings.json")
if err != nil {
resp.Write([]byte(err.Error()))
}
resp.Write([]byte("SUCCESS"))
}
func RegisterRoute(resp http.ResponseWriter, req *http.Request) {
requestData := struct {
Devicename string `json:"devicename"`
}{}
err := json.NewDecoder(req.Body).Decode(&requestData)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
err = pushover.RegisterDevice(requestData.Devicename)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
pushover_retry <- true // allow the notification thread to try again
// store user info in config and return successfully
config.Deviceid = pushover.Device()
config.Display_Devicename = requestData.Devicename
err = storeConfig(config, "settings.json")
if err != nil {
resp.Write([]byte(err.Error()))
}
}
func LogoutRoute(resp http.ResponseWriter, req *http.Request) {
resetPOClient()
// reset user config
config.Userid = ""
config.Usersecret = ""
config.Deviceid = ""
config.Display_Devicename = ""
config.Display_Username = ""
err := storeConfig(config, "settings.json")
if err != nil {
resp.Write([]byte(err.Error()))
}
}
func UserinfoRoute(resp http.ResponseWriter, req *http.Request) {
display_username := config.Display_Username
display_devicename := config.Display_Devicename
loggedin, registered := pushover.GetStatus()
response := struct {
Username string `json:"username"`
Devicename string `json:"devicename"`
LoggedIn bool `json:"loggedin"`
Registered bool `json:"registered"`
}{
display_username,
display_devicename,
loggedin,
registered,
}
resp.Header().Set("Content-Type", "application/json")
json.NewEncoder(resp).Encode(response)
}