File size: 2,547 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package config

import (
	"encoding/base64"
	"encoding/binary"
	"fmt"
	"net/http"
	"regexp"
	"strconv"
	"strings"

	"github.com/google/uuid"
)

// ShortURLMiddleware will issue redirects for requests to generated short URLs.
//
// Unknown/unhandled paths will be left as-is.
func ShortURLMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		newPath := LongPath(req.URL.Path)
		if newPath == "" {
			next.ServeHTTP(w, req)
			return
		}

		cfg := FromContext(req.Context())
		u := *req.URL
		u.Path = newPath

		// use rawCallbackURL so we don't redirect to the same shortened URL
		http.Redirect(w, req, cfg.rawCallbackURL(u.String()).String(), http.StatusTemporaryRedirect)
	})
}

// must also be SMS-safe characters (cannot use _ for example)
const urlChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-."

var (
	alertURL         = regexp.MustCompile(`^/alerts/\d+$`)
	serviceAlertsURL = regexp.MustCompile(`^/services/[a-f0-9-]+/alerts$`)
	urlEnc           = base64.NewEncoding(urlChars).WithPadding(base64.NoPadding)
)

// ShortPath will attempt to convert a normal/long GoAlert URL into a shorter version.
//
// If unable/unknown it will return an empty string.
func ShortPath(longPath string) string {
	switch {
	case serviceAlertsURL.MatchString(longPath):
		idStr := strings.TrimPrefix(strings.TrimSuffix(longPath, "/alerts"), "/services/")
		id, err := uuid.Parse(idStr)
		if err != nil {
			return ""
		}
		return fmt.Sprintf("/s/%s", urlEnc.EncodeToString(id[:]))
	case alertURL.MatchString(longPath):
		i, err := strconv.Atoi(strings.TrimPrefix(longPath, "/alerts/"))
		if err != nil || i == 0 {
			return ""
		}
		buf := make([]byte, 8)
		n := binary.PutUvarint(buf, uint64(i))
		return fmt.Sprintf("/a/%s", urlEnc.EncodeToString(buf[:n]))
	}
	return ""
}

// LongPath will attempt to convert a shortened GoAlert URL into the original.
// If unable, it will return an empty string.
func LongPath(shortPath string) string {
	switch {
	case strings.HasPrefix(shortPath, "/a/"):
		dec, err := urlEnc.DecodeString(strings.TrimPrefix(shortPath, "/a/"))
		if err != nil {
			return ""
		}
		id, _ := binary.Uvarint(dec)
		return fmt.Sprintf("/alerts/%d", id)
	case strings.HasPrefix(shortPath, "/s/"):
		dec, err := urlEnc.DecodeString(strings.TrimPrefix(shortPath, "/s/"))
		if err != nil {
			return ""
		}
		id, err := uuid.FromBytes(dec)
		if err != nil {
			return ""
		}
		return fmt.Sprintf("/services/%s/alerts", id.String())
	}
	return ""
}