-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
handle.go
47 lines (44 loc) · 1.21 KB
/
handle.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
package main
import (
"net/http"
"net/url"
"net/http/httputil"
"log"
"net"
"time"
"context"
"github.com/bogdanovich/dns_resolver"
"strings"
)
type handle struct {
reverseProxy string
}
func (this *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr + " " + r.Method + " " + r.URL.String() + " " + r.Proto + " " + r.UserAgent())
remote, err := url.Parse(this.reverseProxy)
if err != nil {
log.Fatalln(err)
}
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}
http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
remote := strings.Split(addr, ":")
if cmd.ip == "" {
resolver := dns_resolver.New([]string{"114.114.114.114", "114.114.115.115", "119.29.29.29", "223.5.5.5", "8.8.8.8", "208.67.222.222", "208.67.220.220"})
resolver.RetryTimes = 5
ip, err := resolver.LookupHost(remote[0])
if err != nil {
log.Println(err)
}
cmd.ip = ip[0].String()
}
addr = cmd.ip + ":" + remote[1]
return dialer.DialContext(ctx, network, addr)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
r.Host = remote.Host
proxy.ServeHTTP(w, r)
}