-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroutes.go
356 lines (335 loc) · 7.29 KB
/
routes.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"fmt"
"github.com/gokyle/webshell"
"github.com/gokyle/webshell/auth"
"log"
"net/http"
"regexp"
"strings"
)
var (
admin_user string
page_title string
server_host string
server_secure bool
server_dev = true
strip_views = regexp.MustCompile("^/views/(.+)$")
valid_link = regexp.MustCompile("^\\w+://")
valid_sid = regexp.MustCompile("^[\\w-_]+$")
)
var NotFound webshell.TemplateErrorRoute
type Page struct {
Title string
Count string
Views string
AllViews string
Host string
ShortCode string
Posted bool
ShowErr bool
ShowMsg bool
Scheme string
Msg string
CheckAuth bool
File string
}
func (page *Page) getAllViews() {
count, err := getAllViews()
if err != nil {
page.AllViews = "no views"
} else {
switch count {
case 0:
page.AllViews = "no views"
case 1:
page.AllViews = "one view"
default:
page.AllViews = fmt.Sprintf("%d views", count)
}
}
}
func (page *Page) getPageCount() {
count, err := countShortened()
if err != nil {
page.Count = "No links"
} else {
if count == 0 {
page.Count = "No links"
} else {
var verb string
if count == 1 {
page.Count = "link"
} else {
page.Count = "links"
}
page.Count = fmt.Sprintf("%s %d %s", verb, count,
page.Count)
}
}
return
}
func NewPage() (page *Page) {
page = new(Page)
page.Title = page_title
page.Host = server_host
page.CheckAuth = check_auth
page.File = "templates/index.html"
if server_secure {
page.Scheme = "https"
} else {
page.Scheme = "http"
}
return
}
func home(w http.ResponseWriter, r *http.Request) {
page := NewPage()
servePage(page, w, r)
return
}
func servePage(page *Page, w http.ResponseWriter, r *http.Request) {
page.getPageCount()
page.getAllViews()
out, err := webshell.BuildTemplateFile(page.File, page)
if err != nil {
webshell.Error404(err.Error(), "text/plain", w, r)
} else {
w.Write(out)
}
LogRequest(page, r)
}
func serveErr(page *Page, err error, w http.ResponseWriter, r *http.Request) {
page.ShowErr = true
page.Msg = err.Error()
LogError(page, r)
servePage(page, w, r)
}
func topRoute(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
newShortened(w, r)
return
}
sid := r.URL.Path
if len(sid) > 1 {
sid = sid[1:len(sid)]
}
url, err := lookupShortCode(sid)
if err != nil {
home(w, r)
return
} else if url != "" {
err = updateSidViews(sid)
if err != nil {
log.Printf("[+] error updating views: %s\n",
err.Error())
}
LogRequest(nil, r)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
return
}
if sid != "/" && sid != "/index.html" {
page := NewPage()
page.getPageCount()
page.getAllViews()
page.Msg = "404 - page not found"
page.ShowErr = true
LogError(page, r)
NotFound(page, w, r)
} else {
home(w, r)
}
}
func newShortened(w http.ResponseWriter, r *http.Request) {
page := NewPage()
err := r.ParseForm()
if err != nil {
serveErr(page, err, w, r)
return
}
user := r.Form.Get("user")
pass := r.Form.Get("pass")
if check_auth && !auth.Authenticate(user, pass) {
err = fmt.Errorf("Authenticated failed!")
serveErr(page, err, w, r)
return
}
sid := r.Form.Get("sid")
url := r.Form.Get("url")
if len(url) > 0 && !valid_link.MatchString(url) {
url = "http://" + url
}
if len(url) == 0 {
err := fmt.Errorf("Invalid URL")
serveErr(page, err, w, r)
return
} else if len(sid) > 0 {
if surl, err := lookupShortCode(sid); err != nil {
serveErr(page, err, w, r)
return
} else if surl != "" && surl != url {
err = fmt.Errorf("URL already present.")
if err != nil {
serveErr(page, err, w, r)
return
}
sid, db_err := urlToSid(url)
if db_err == nil {
page.ShortCode = sid
page.Posted = true
} else {
err = db_err
}
serveErr(page, err, w, r)
return
} else if err = insertShortened(sid, url); err != nil {
serveErr(page, err, w, r)
return
} else if valid_sid.MatchString(sid) {
page.ShortCode = sid
page.Posted = true
} else {
err = fmt.Errorf("Invalid short code.")
serveErr(page, err, w, r)
return
}
} else {
sid, err := ShortenUrl(ValidateShortenedUrl)
if err != nil {
serveErr(page, err, w, r)
return
}
if err = insertShortened(sid, url); err != nil {
serveErr(page, err, w, r)
return
} else {
page.Posted = true
page.ShortCode = sid
}
}
servePage(page, w, r)
return
}
func getViews(w http.ResponseWriter, r *http.Request) {
sid := strip_views.ReplaceAllString(r.URL.Path, "$1")
sid = strings.TrimRight(sid, "/")
page := NewPage()
page.File = "templates/views.html"
if !valid_sid.MatchString(sid) {
NotFound(page, w, r)
return
}
count, err := getSidViews(sid)
var views string
if err != nil {
err = fmt.Errorf("invalid short code")
page.File = "templates/index.html"
serveErr(page, err, w, r)
return
} else {
if count == 0 {
views = "no views"
} else if count == 1 {
views = "one view"
} else {
views = fmt.Sprintf("%d views", count)
}
}
page.ShortCode = sid
url, err := lookupShortCode(sid)
if err != nil {
serveErr(page, err, w, r)
} else {
page.Views = views
page.ShowMsg = true
page.Msg = url
servePage(page, w, r)
}
}
func changePass(w http.ResponseWriter, r *http.Request) {
page := NewPage()
page.File = "templates/change.html"
if r.Method != "POST" {
servePage(page, w, r)
return
}
err := r.ParseForm()
if err != nil {
serveErr(page, err, w, r)
return
}
user := r.Form.Get("user")
pass := r.Form.Get("pass")
new_pass := r.Form.Get("newpass")
confirm := r.Form.Get("confirm")
if new_pass != confirm {
err = fmt.Errorf("New passwords do not match.")
serveErr(page, err, w, r)
return
}
if !check_auth || !auth.Authenticate(user, pass) {
page.Msg = "Authentication failed."
page.ShowErr = true
servePage(page, w, r)
return
}
salt, hash := auth.HashPass(new_pass)
if len(salt) == 0 || len(hash) == 0 {
page.Msg = "Invalid password."
page.ShowErr = true
servePage(page, w, r)
return
}
err = dbChangePass(user, salt, hash)
if err != nil {
serveErr(page, err, w, r)
return
}
page.ShowMsg = true
page.Msg = "Password changed."
servePage(page, w, r)
}
func addUser(w http.ResponseWriter, r *http.Request) {
page := NewPage()
page.File = "templates/add.html"
if admin_user == "" {
err := fmt.Errorf("No administrative user specified.")
serveErr(page, err, w, r)
return
}
if r.Method != "POST" {
servePage(page, w, r)
return
}
err := r.ParseForm()
if err != nil {
serveErr(page, err, w, r)
return
}
user := r.Form.Get("user")
pass := r.Form.Get("pass")
if user != admin_user && !auth.Authenticate(user, pass) {
err = fmt.Errorf("Authentication failed.")
serveErr(page, err, w, r)
return
}
new_user := r.Form.Get("newuser")
new_pass := r.Form.Get("newpass")
salt, hash := auth.HashPass(new_pass)
if len(salt) == 0 || len(hash) == 0 {
page.Msg = "Invalid password."
page.ShowErr = true
servePage(page, w, r)
return
}
err = addUserToDb(new_user, salt, hash)
if err != nil {
serveErr(page, err, w, r)
} else {
page.Msg = "User added."
page.ShowMsg = true
servePage(page, w, r)
}
}
func init_auth() {
auth.LookupCredentials = getPassHash
}