-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlx.go
executable file
·280 lines (242 loc) · 6.85 KB
/
urlx.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
package urlx
import (
"bytes"
"errors"
"net"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/purell"
"github.com/valyala/fasthttp"
"golang.org/x/net/idna"
)
// Parse parses raw URL string into the net/url URL struct.
// It uses the url.Parse() internally, but it slightly changes
// its behavior:
// 1. It forces the default scheme and port.
// 2. It favors absolute paths over relative ones, thus "example.com"
// is parsed into url.Host instead of url.Path.
// 4. It lowercases the Host (not only the Scheme).
func ParseUnsafe(rawURL string) (u *url.URL) {
u, _ = url.Parse(rawURL)
return
}
func Parse(rawURL string) (*url.URL, error) {
rawURL = strings.ReplaceAll(rawURL, "www.", "")
// Force default http scheme, so net/url.Parse() doesn't
// put both host and path into the (relative) path.
if strings.Index(rawURL, "//") == 0 {
// Leading double slashes (any scheme). Force http.
rawURL = "http:" + rawURL
}
if strings.Index(rawURL, "://") == -1 {
// Missing scheme. Force http.
rawURL = "http://" + rawURL
}
// Use net/url.Parse() now.
u, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
host, _, err := SplitHostPort(u)
if err != nil {
return nil, err
}
if err := checkHost(host); err != nil {
return nil, err
}
u.Host = strings.ToLower(u.Host)
u.Scheme = strings.ToLower(u.Scheme)
return u, nil
}
var (
// RFC 1035.
domainRegexp = regexp.MustCompile(`^([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$`)
ipv4Regexp = regexp.MustCompile(`^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$`)
ipv6Regexp = regexp.MustCompile(`^\[[a-fA-F0-9:]+\]$`)
)
func checkHost(host string) error {
if host == "" {
return &url.Error{Op: "host", URL: host, Err: errors.New("empty host")}
}
host = strings.ToLower(host)
if domainRegexp.MatchString(host) || host == "localhost" {
return nil
}
if punycode, err := idna.ToASCII(host); err != nil {
return err
} else if domainRegexp.MatchString(punycode) {
return nil
}
// IPv4 and IPv6.
if ipv4Regexp.MatchString(host) || ipv6Regexp.MatchString(host) {
return nil
}
return &url.Error{Op: "host", URL: host, Err: errors.New("invalid host")}
}
func RemoveURLParams(u *url.URL) {
u.RawQuery = ""
}
// SplitHostPort splits network address of the form "host:port" into
// host and port. Unlike net.SplitHostPort(), it doesn't remove brackets
// from [IPv6] host and it accepts net/url.URL struct instead of a string.
func SplitHostPort(u *url.URL) (host, port string, err error) {
if u == nil {
return "", "", &url.Error{Op: "host", URL: host, Err: errors.New("empty url")}
}
host = u.Host
// Find last colon.
if i := strings.LastIndex(host, ":"); i != -1 {
// If we're not inside [IPv6] brackets, split host:port.
if len(host) > i && strings.Index(host[i:], "]") == -1 {
port = host[i+1:]
host = host[:i]
}
}
// Port is optional. But if it's set, is it a number?
if port != "" {
if _, err := strconv.Atoi(port); err != nil {
return "", "", &url.Error{Op: "port", URL: host, Err: err}
}
}
return host, port, nil
}
const normalizeFlags purell.NormalizationFlags = purell.FlagRemoveDefaultPort |
purell.FlagDecodeDWORDHost | purell.FlagDecodeOctalHost | purell.FlagDecodeHexHost |
purell.FlagRemoveUnnecessaryHostDots | purell.FlagRemoveDotSegments | purell.FlagRemoveDuplicateSlashes |
purell.FlagUppercaseEscapes | purell.FlagDecodeUnnecessaryEscapes | purell.FlagEncodeNecessaryEscapes |
purell.FlagSortQuery
// Normalize returns normalized URL string.
// Behavior:
// 1. Remove unnecessary host dots.
// 2. Remove default port (http://localhost:80 becomes http://localhost).
// 3. Remove duplicate slashes.
// 4. Remove unnecessary dots from path.
// 5. Sort query parameters.
// 6. Decode host IP into decimal numbers.
// 7. Handle escape values.
// 8. Decode Punycode domains into UTF8 representation.
func Normalize(u *url.URL) (string, error) {
host, port, err := SplitHostPort(u)
if err != nil {
return "", err
}
if err := checkHost(host); err != nil {
return "", err
}
// Decode Punycode.
host, err = idna.ToUnicode(host)
if err != nil {
return "", err
}
u.Host = strings.ToLower(host)
if port != "" {
u.Host += ":" + port
}
if u.Scheme == "" {
u.Scheme = "http"
}
u.Scheme = strings.ToLower(u.Scheme)
return purell.NormalizeURL(u, normalizeFlags), nil
}
//делает запрос и смотрит если прокси
func RealLinkFasthttp(link string) (real string) {
real = link
r := fasthttp.AcquireRequest()
r.SetRequestURI(link)
resp := fasthttp.AcquireResponse()
if fasthttp.Do(r, resp) != nil {
return
}
if resp.Header.Peek("Location") != nil {
real = string(resp.Header.Peek("Location"))
}
return
}
var transport = &http.Transport{Dial: (&net.Dialer{Timeout: 5 * time.Second}).Dial, TLSHandshakeTimeout: 5 * time.Second}
var c = &http.Client{Timeout: time.Second * 10, Transport: transport}
//делает запрос и смотрит если прокси
func RealLink(link string) (real string) {
real = link
r, err := c.Get(link)
if err != nil {
return
}
u, err := NormalizeString(r.Request.URL.String())
if err != nil {
return
}
real = u
return
}
//делает запрос и смотрит если прокси
func RealLinkClear(link string) (real string, err error) {
real = link
transport := &http.Transport{Dial: (&net.Dialer{Timeout: 10 * time.Second}).Dial, TLSHandshakeTimeout: 10 * time.Second}
c := &http.Client{Timeout: time.Second * 10, Transport: transport}
r, err := c.Get(link)
if err != nil {
return
}
r.Request.URL.RawQuery = ""
link = r.Request.URL.String()
link = strings.TrimSpace(link)
link = strings.Replace(link, "\n", "", -1)
link = strings.Replace(link, "\t", "", -1)
link = strings.Replace(link, "\r", "", -1)
link = strings.Replace(link, "www.", "", -1)
var b bytes.Buffer
for _, l := range link {
if l == '#' {
break
}
b.WriteRune(l)
}
link = b.String()
u, err := NormalizeString(link)
if err != nil {
return
}
real = u
return
}
// NormalizeString returns normalized URL string.
// It's a shortcut for Parse() and Normalize() funcs.
func NormalizeString(rawURL string) (string, error) {
u, err := Parse(rawURL)
if err != nil {
return "", err
}
return Normalize(u)
}
// Resolve resolves the URL host to its IP address.
func Resolve(u *url.URL) (*net.IPAddr, error) {
host, _, err := SplitHostPort(u)
if err != nil {
return nil, err
}
addr, err := net.ResolveIPAddr("ip", host)
if err != nil {
return nil, err
}
return addr, nil
}
// Resolve resolves the URL host to its IP address.
// It's a shortcut for Parse() and Resolve() funcs.
func ResolveString(rawURL string) (*net.IPAddr, error) {
u, err := Parse(rawURL)
if err != nil {
return nil, err
}
return Resolve(u)
}
func URIEncode(uri string) (string, error) {
u, err := url.Parse(uri)
if err != nil {
return "", err
}
return u.String(), nil
}