-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshort.go
624 lines (562 loc) · 15 KB
/
short.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
// Copyright 2021 Changkun Ou. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"log"
"net/http"
"net/url"
"path/filepath"
"strconv"
"strings"
"time"
"changkun.de/x/redir/internal/config"
"changkun.de/x/redir/internal/models"
"changkun.de/x/redir/internal/short"
"changkun.de/x/redir/internal/utils"
)
// sHandler redirects the current request to a known link if the alias is
// found in the redir store.
func (s *server) sHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// for development.
if config.Conf.CORS {
log.Println("CORS is activated.")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
}
switch r.Method {
case http.MethodOptions:
// nothing, really.
case http.MethodPost:
s.sHandlerPost(w, r)
case http.MethodGet:
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Cache-Control", "max-age=0")
s.sHandlerGet(w, r)
default:
err := fmt.Errorf("%s is not supported", r.Method)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(err.Error()))
}
})
}
type shortInput struct {
Op short.Op `json:"op"`
Alias string `json:"alias"`
Data interface{} `json:"data"`
}
type shortOutput struct {
Message string `json:"message"`
}
// sHandlerPost handles all kinds of operations.
// This is not a RESTful style, because we don't have that much router space
// to use. We are currently limited the single index router, which is the /s.
func (s *server) sHandlerPost(w http.ResponseWriter, r *http.Request) {
var err error
defer func() {
if err != nil {
b, _ := json.Marshal(shortOutput{
Message: err.Error(),
})
_, _ = w.Write(b)
w.WriteHeader(http.StatusBadRequest)
}
}()
// All post request must be authenticated.
user, err := s.handleAuth(w, r)
if err != nil {
return
}
w.Header().Add("Content-Type", "application/json")
// Decode request body and determine what is the operator
d := json.NewDecoder(r.Body)
var red shortInput
err = d.Decode(&red)
if err != nil {
return
}
// Validating the operator and decode the redir data
if !short.Op(red.Op).Valid() {
err = errors.New("unsupported operator")
return
}
b, err := json.Marshal(red.Data)
if err != nil {
return
}
var redir models.Redir
err = json.Unmarshal(b, &redir)
if err != nil {
return
}
redir.UpdatedBy = user
// Edit redirect data.
err = short.Edit(r.Context(), s.db, short.Op(red.Op), red.Alias, &redir)
if err == nil {
// Flush the cache so that the changes can be effected immediately.
s.cache.Flush()
}
}
// sHandlerGet is the core of redir service. It redirects a given
// alias to the actual destination.
func (s *server) sHandlerGet(w http.ResponseWriter, r *http.Request) {
var err error
defer func() {
if err != nil && !errors.Is(err, errUnauthorized) {
// Just redirect the user we could not find the record rather than
// throw 50x. The server logs should be able to identify the issue.
log.Printf("request err: %v\n", err)
http.Redirect(w, r, "/404.html", http.StatusTemporaryRedirect)
}
}()
ctx := r.Context()
// statistic page
prefix := config.Conf.S.Prefix
// URLs with /s/.* is reserved for internal usage.
if strings.HasPrefix(r.URL.Path, prefix+".") {
err = s.serveStatic(ctx, w, r, prefix)
return
}
// Identify the alias of the short link.
alias := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, prefix), "/")
// If alias is empty, then process index page request.
if alias == "" {
err = s.sIndex(ctx, w, r)
return
}
// Only allow valid aliases.
if !short.Validity.MatchString(alias) {
err = short.ErrInvalidAlias
return
}
// Process visitor information, wait maximum 5 seconds.
if config.Conf.Stats.Enable {
recordCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
s.recognizeVisitor(recordCtx, w, r, alias)
}
// Figure out redirect location
red, ok := s.cache.Get(alias)
if !ok {
red, err = s.checkdb(ctx, alias)
if err != nil {
red, err = s.checkvcs(ctx, alias)
if err != nil {
return
}
}
s.cache.Put(alias, red)
}
// Send a wait page if time does not permitting
if time.Now().UTC().Sub(red.ValidFrom.UTC()) < 0 {
err = waitTmpl.Execute(w, &pageInfo{
ValidFrom: red.ValidFrom.UTC().Format("2006-01-02T15:04:05"),
ShowImpressum: config.Conf.GDPR.Impressum.Enable,
ShowPrivacy: config.Conf.GDPR.Privacy.Enable,
ShowContact: config.Conf.GDPR.Contact.Enable,
})
return
}
// Send a warn page if the redirected link is an external link
//
// If the link configuring person thinks the redirected link is trustable,
// do the redirects always.
if !red.Trust {
// Figure out if the user allow redirects always
allowRedir := false
cookie, _ := r.Cookie(redirAllowCookie)
if cookie != nil {
n, _ := strconv.Atoi(cookie.Value)
if n == 1 {
allowRedir = true
}
}
// If a redirect is accidentally configured as non-trustable,
// but still an internal website, then we don't show the warn page.
if !allowRedir && !strings.Contains(red.URL, r.Host) {
err = warnTmpl.Execute(w, &pageInfo{
OwnerName: config.Conf.GDPR.Owner.Name,
OwnerDomain: config.Conf.GDPR.Owner.Domain,
URL: red.URL,
ShowImpressum: config.Conf.GDPR.Impressum.Enable,
ShowPrivacy: config.Conf.GDPR.Privacy.Enable,
ShowContact: config.Conf.GDPR.Contact.Enable,
})
return
}
}
// If this is a page that refers to a PDF, we prefer serve it as a PDF
// content directly rather than redirect.
if strings.HasSuffix(red.URL, ".pdf") {
var resp *http.Response
resp, err = http.Get(red.URL)
if err != nil {
return
}
defer resp.Body.Close()
_, err = io.Copy(w, resp.Body)
return
}
// Finally, let's redirect!
http.Redirect(w, r, red.URL, http.StatusTemporaryRedirect)
}
type pageInfo struct {
OwnerName string
OwnerDomain string
URL string
ValidFrom string
Body template.HTML
Email string
ShowImpressum bool
ShowPrivacy bool
ShowContact bool
}
func (s *server) serveStatic(
ctx context.Context,
w http.ResponseWriter,
r *http.Request,
prefix string,
) error {
var (
t *template.Template
d *pageInfo
)
switch {
case strings.HasPrefix(r.URL.Path, prefix+".static"):
// Serve static files under ./.static/*. This should not conflict
// with all existing aliases, meaning that alias should not start
// with a dot.
ext := filepath.Ext(r.URL.Path)
switch ext {
case ".css":
w.Header().Add("Content-Type", "text/css")
case ".js":
w.Header().Add("Content-Type", "text/javascript")
}
f, err := statics.Open(strings.TrimPrefix(r.URL.Path, prefix+".static/"))
if err != nil {
return err
}
b, err := io.ReadAll(f)
if err != nil {
return err
}
_, err = w.Write(b)
return err
case strings.HasPrefix(r.URL.Path, prefix+".impressum"):
if config.Conf.GDPR.Impressum.Enable {
t = impressumTmpl
}
d = &pageInfo{
Body: template.HTML(config.Conf.GDPR.Impressum.Content),
ShowImpressum: config.Conf.GDPR.Impressum.Enable,
ShowPrivacy: config.Conf.GDPR.Privacy.Enable,
ShowContact: config.Conf.GDPR.Contact.Enable,
}
case strings.HasPrefix(r.URL.Path, prefix+".privacy"):
if config.Conf.GDPR.Privacy.Enable {
t = privacyTmpl
}
d = &pageInfo{
Body: template.HTML(config.Conf.GDPR.Privacy.Content),
ShowImpressum: config.Conf.GDPR.Impressum.Enable,
ShowPrivacy: config.Conf.GDPR.Privacy.Enable,
ShowContact: config.Conf.GDPR.Contact.Enable,
}
case strings.HasPrefix(r.URL.Path, prefix+".contact"):
if config.Conf.GDPR.Contact.Enable {
t = contactTmpl
}
d = &pageInfo{
Email: config.Conf.GDPR.Contact.Email,
ShowImpressum: config.Conf.GDPR.Impressum.Enable,
ShowPrivacy: config.Conf.GDPR.Privacy.Enable,
ShowContact: config.Conf.GDPR.Contact.Enable,
}
}
if t != nil {
return t.Execute(w, d)
}
return nil
}
const (
redirVidCookie = "redir_vid"
redirAllowCookie = "redir_allow"
)
// recognizeVisitor implements a best effort visitor recording.
//
// If the redir's cookie is presented, then we use cookie id.
// If the cookie does not present any data, we read the IP address, and
// allocates a new visitor id for the visitor.
//
// We don't care if any error happens inside.
func (s *server) recognizeVisitor(
ctx context.Context,
w http.ResponseWriter,
r *http.Request,
alias string,
) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var cookieVid string
c, err := r.Cookie(redirVidCookie)
if err != nil {
cookieVid = ""
} else {
cookieVid = c.Value
}
// count visit and set cookie.
vid, err := s.db.RecordVisit(ctx, &models.Visit{
VisitorID: cookieVid,
Alias: alias,
IP: utils.ReadIP(r),
UA: r.UserAgent(),
Referer: r.Referer(),
Time: time.Now().UTC(),
})
if err != nil {
log.Printf("cannot record alias %s's visit: %v", alias, err)
} else {
w.Header().Set("Set-Cookie", redirVidCookie+"="+vid)
}
}
// checkdb checks whether the given alias is exsited in the redir database
func (s *server) checkdb(ctx context.Context, alias string) (*models.Redir, error) {
a, err := s.db.FetchAlias(ctx, alias)
if err != nil {
return nil, err
}
return a, nil
}
// checkvcs checks whether the given alias is an repository on VCS, if so,
// then creates a new alias and returns url of the vcs repository.
func (s *server) checkvcs(ctx context.Context, alias string) (*models.Redir, error) {
// construct the try path and make the request to vcs
repoPath := config.Conf.X.RepoPath
repoPath = strings.TrimSuffix(repoPath, "/*")
tryPath := fmt.Sprintf("%s/%s", repoPath, alias)
resp, err := http.Get(tryPath)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK &&
resp.StatusCode != http.StatusMovedPermanently {
return nil, fmt.Errorf("%s is not a repository", tryPath)
}
// figure out the new location
if resp.StatusCode == http.StatusMovedPermanently {
tryPath = resp.Header.Get("Location")
}
// store such a try path
r := &models.Redir{
Alias: alias,
URL: tryPath,
Private: false,
Trust: false,
ValidFrom: time.Now().UTC(),
}
err = s.db.StoreAlias(ctx, r)
if err != nil {
return s.checkdb(ctx, alias)
}
return r, nil
}
var (
errInvalidStatParam = errors.New("invalid stat parameter")
errMissingStatParam = errors.New("missing stat parameter")
)
// sIndex serves two types of index page, and serves statistics data.
//
// If there are no supplied value of a `mode` query parameter, the method
// returns a public visible index page that contains all publicly visible
// short urls.
//
// If the query parameter contains mode=admin, then it requires basic
// auth to access the admin dashboard where one can manage all short urls.
//
// If the query parameter contaisn mode=stat, then it returns application/json
// data, which contains data for data visualizations in the index page.
func (s *server) sIndex(
ctx context.Context,
w http.ResponseWriter,
r *http.Request,
) error {
e := struct {
AdminView bool
StatsMode bool
DevMode bool
ShowImpressum bool
ShowPrivacy bool
ShowContact bool
}{
AdminView: false,
StatsMode: config.Conf.Stats.Enable,
DevMode: config.Conf.Development,
ShowImpressum: config.Conf.GDPR.Impressum.Enable,
ShowPrivacy: config.Conf.GDPR.Privacy.Enable,
ShowContact: config.Conf.GDPR.Contact.Enable,
}
mode := r.URL.Query().Get("mode")
switch mode {
case "stats": // stats data is public to everyone
if config.Conf.Stats.Enable {
err := s.statData(ctx, w, r)
if !errors.Is(err, errInvalidStatParam) {
return err
}
log.Println(err)
}
case "index": // public visible index data
return s.indexData(ctx, w, r, true)
case "index-pro": // data with statistics
return s.indexData(ctx, w, r, false)
case "admin":
_, err := s.handleAuth(w, r)
if err != nil {
return err
}
e.AdminView = true
default:
// Process visitor information for public index, wait maximum 5 seconds.
recordCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
s.recognizeVisitor(recordCtx, w, r, "")
}
// Serve the index page.
w.Header().Add("Content-Type", "text/html")
return dTmpl.Execute(w, e)
}
type indexOutput struct {
Data []models.RedirIndex `json:"data"`
Page int64 `json:"page"`
Total int64 `json:"total"`
}
// index on all aliases, require admin access.
func (s *server) indexData(
ctx context.Context,
w http.ResponseWriter,
r *http.Request,
public bool,
) error {
if !public {
_, err := s.handleAuth(w, r)
if err != nil {
return err
}
}
w.Header().Add("Content-Type", "application/json")
// get page size and number
ps := r.URL.Query().Get("ps")
pageSize, err := strconv.ParseUint(ps, 10, 0)
if err != nil {
pageSize = 5
}
pn := r.URL.Query().Get("pn")
pageNum, err := strconv.ParseUint(pn, 10, 0)
if err != nil || pageNum <= 0 {
pageNum = 1
}
rs, total, err := s.db.FetchAliasAll(ctx, public, int64(pageSize), int64(pageNum))
if err != nil {
return err
}
b, err := json.Marshal(indexOutput{
Data: rs,
Page: int64(pageNum),
Total: total,
})
if err != nil {
return err
}
_, _ = w.Write(b)
return nil
}
func (s *server) statData(
ctx context.Context,
w http.ResponseWriter,
r *http.Request,
) (retErr error) {
defer func() {
if retErr != nil {
retErr = fmt.Errorf("%w: %v", errInvalidStatParam, retErr)
}
}()
params := r.URL.Query()
a := params.Get("a")
if a == "" {
retErr = fmt.Errorf("%s: alias (a)", errMissingStatParam)
return
}
stat := params.Get("stat")
if stat == "" {
retErr = fmt.Errorf("%s: stat mode (stat)", errMissingStatParam)
return
}
start, end, err := parseDuration(params)
if err != nil {
retErr = fmt.Errorf("%s: %v", errInvalidStatParam, err)
return
}
w.Header().Add("Content-Type", "application/json")
var results interface{}
switch stat {
case "referer":
results, err = s.db.StatReferer(ctx, a, start, end)
if err != nil {
retErr = err
return
}
case "ua":
results, err = s.db.StatUA(ctx, a, start, end)
if err != nil {
retErr = err
return
}
case "time":
results, err = s.db.StatVisitHist(ctx, a, start, end)
if err != nil {
retErr = err
return
}
default:
retErr = fmt.Errorf("%s stat mode is not supported", stat)
return
}
b, err := json.Marshal(results)
if err != nil {
retErr = err
return
}
_, _ = w.Write(b)
return
}
func parseDuration(p url.Values) (start, end time.Time, err error) {
t0 := p.Get("t0")
if t0 != "" {
start, err = time.Parse("2006-01-02", t0)
if err != nil {
return
}
} else {
start = time.Now().UTC().Add(-time.Hour * 24 * 7) // last week
}
t1 := p.Get("t1")
if t1 != "" {
end, err = time.Parse("2006-01-02", t1)
if err != nil {
return
}
} else {
end = time.Now().UTC()
}
return
}