-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshrl_enhancers.go
153 lines (131 loc) · 3.27 KB
/
shrl_enhancers.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
package service
import (
"net/http"
"net/url"
"regexp"
"strings"
"github.com/mat/besticon/v3/besticon"
log "github.com/sirupsen/logrus"
pb "github.com/demophoon/shrls/server/gen"
)
func (s Server) enhanceShortUrl(u *pb.ShortURL) {
// Fetch Favicon
s.resolveFavicon(u)
// Resolve Redirects
s.resolveRedirects(u)
// Strip Url Parameters
s.removeQueryParameters(u)
}
func (s Server) resolveRedirects(u *pb.ShortURL) {
switch u.Content.Content.(type) {
case *pb.ExpandedURL_Url:
if s.config.ResolveURLMatchingHosts == nil {
return
}
loc := u.Content.GetUrl().Url
parsed, err := url.Parse(loc)
if err != nil {
log.Warnf("Couldn't parse url when resolving redirects from %s: %s", loc, err)
return
}
s := *s.config.ResolveURLMatchingHosts
regex_str := strings.Join(s, "|")
log.Debugf("Regexp string: %s", regex_str)
matcher, err := regexp.Compile(regex_str)
if err != nil {
log.Errorf("Unable to determine resolve_urls_matching_hosts option: %s", err)
return
}
if !matcher.Match([]byte(parsed.Host)) {
return
}
nextLocation := parsed.String()
i := 0
for i < 25 {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Head(nextLocation)
if err != nil {
log.Errorf("Unable to resolve url: %s", err)
return
}
if resp.StatusCode >= 300 && resp.StatusCode < 400 {
nextLocation = resp.Header.Get("Location")
i += 1
} else {
break
}
}
u.Content.Content = &pb.ExpandedURL_Url{
Url: &pb.Redirect{
Url: nextLocation,
Favicon: u.Content.GetUrl().Favicon,
},
}
}
}
func (s Server) removeQueryParameters(u *pb.ShortURL) {
switch u.Content.Content.(type) {
case *pb.ExpandedURL_Url:
if s.config.RemoveQueryParametersMatchingHosts == nil {
return
}
loc := u.Content.GetUrl().Url
parsed, err := url.Parse(loc)
if err != nil {
log.Warnf("Couldn't parse url when removing query parameters from %s: %s", loc, err)
return
}
s := *s.config.RemoveQueryParametersMatchingHosts
regex_str := strings.Join(s, "|")
log.Debugf("Regexp string: %s", regex_str)
matcher, err := regexp.Compile(regex_str)
if err != nil {
log.Errorf("Unable to determine remove_query_parameters_matching_hosts option: %s", err)
return
}
if !matcher.Match([]byte(parsed.Host)) {
return
}
stripped := url.URL{
Scheme: parsed.Scheme,
User: parsed.User,
Host: parsed.Host,
Path: parsed.Path,
}
u.Content.Content = &pb.ExpandedURL_Url{
Url: &pb.Redirect{
Url: stripped.String(),
Favicon: u.Content.GetUrl().Favicon,
},
}
}
}
func (s Server) resolveFavicon(u *pb.ShortURL) {
switch u.Content.Content.(type) {
case *pb.ExpandedURL_Url:
loc := u.Content.GetUrl().Url
bi := besticon.New()
icf := bi.NewIconFinder()
icons, err := icf.FetchIcons(loc)
if err == nil {
for _, icon := range icons {
if icon.Width >= 16 && icon.Height >= 16 {
u.Content.Content = &pb.ExpandedURL_Url{
Url: &pb.Redirect{
Url: loc,
Favicon: icon.ImageData,
},
}
log.Tracef("favicon found for %s at %s", loc, icon.URL)
break
}
}
} else {
log.Warnf("Unable to fetch favicon from %s: %s", loc, err)
}
}
}