-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastproxy.go
63 lines (54 loc) · 1.64 KB
/
fastproxy.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
package main
import (
"crypto/tls"
"log"
"time"
"github.com/valyala/fasthttp"
)
var client0 *fasthttp.Client
var encoding = []byte("gzip, br")
func init() {
dial0 := &fasthttp.TCPDialer{
Concurrency: 512,
DNSCacheDuration: time.Hour,
}
client0 = &fasthttp.Client{
Name: "Go-http-client/1.1",
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxIdleConnDuration: 15 * time.Minute,
NoDefaultUserAgentHeader: true,
DisableHeaderNamesNormalizing: true, // If you set the case on your headers correctly you can enable this
DisablePathNormalizing: true,
// increase DNS cache time to an hour instead of default minute
Dial: dial0.Dial,
TLSConfig: &tls.Config{InsecureSkipVerify: true},
}
}
func fastProxy(ctx *fasthttp.RequestCtx) {
url_ := ctx.FormValue("_url")
if url_ == nil {
log.Printf("bad url!")
return
}
log.Printf("[%s] -> [%s]", ctx.Host(), url_)
req := fasthttp.AcquireRequest()
req.SetRequestURIBytes(url_)
req.Header.SetBytesV(fasthttp.HeaderAcceptEncoding, encoding)
req.Header.SetMethod(fasthttp.MethodGet)
resp := fasthttp.AcquireResponse()
err := client0.Do(req, resp)
fasthttp.ReleaseRequest(req)
if err != nil {
fasthttp.ReleaseResponse(resp)
log.Printf("couldn't read, %v", err)
return
}
ctx.SetContentTypeBytes(resp.Header.ContentType())
ctx.SetStatusCode(resp.StatusCode())
ctx.Response.Header.SetBytesV(fasthttp.HeaderContentEncoding, resp.Header.ContentEncoding())
body := resp.Body()
ctx.Response.SetBodyRaw(body)
log.Printf("<- %vb", len(body))
fasthttp.ReleaseResponse(resp)
}