Skip to content

Commit

Permalink
update performance
Browse files Browse the repository at this point in the history
  • Loading branch information
vinhjaxt committed Nov 25, 2019
1 parent 11db6ba commit 7338bf3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
40 changes: 27 additions & 13 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"net"
"strconv"

"github.com/tidwall/gjson"
"github.com/valyala/fasthttp"
Expand All @@ -13,43 +14,56 @@ var errNoSuitableIP = errors.New("No suitable IP")
var errNoAnswer = errors.New("No IP")
var dnsEndpointQs string

func getUsableIP(hostname, port string) (string, error) {
if ip := net.ParseIP(hostname); ip != nil {
if isPrivateIP(ip) {
return "", errPrivateIP
func getUsableIP(hostname, portStr string) (*net.TCPAddr, error) {
port, err := strconv.Atoi(portStr)
if err != nil {
return nil, err
}
ipp := net.ParseIP(hostname)
if ipp != nil {
if isPrivateIP(ipp) {
return nil, errPrivateIP
}
return hostname, nil
return &net.TCPAddr{
IP: ipp,
Port: port,
}, nil
}
req := fasthttp.AcquireRequest()
req.Header.Set("Accept-Encoding", "gzip, deflate")
req.SetRequestURI(dnsEndpointQs)
req.URI().QueryArgs().Set("name", hostname)
resp := fasthttp.AcquireResponse()
err := httpClient.DoTimeout(req, resp, httpClientTimeout)
err = httpClient.DoTimeout(req, resp, httpClientTimeout)
fasthttp.ReleaseRequest(req)
if err != nil {
fasthttp.ReleaseResponse(resp)
return "", err
return nil, err
}
body, err := getResponseBody(resp)
fasthttp.ReleaseResponse(resp)
if err != nil {
return "", err
return nil, err
}
answers := gjson.GetBytes(body, "Answer")
if answers.Exists() == false || answers.IsArray() == false {
return "", errNoAnswer
return nil, errNoAnswer
}
for _, answer := range answers.Array() {
ip := answer.Get("data").String()
if ipp := net.ParseIP(ip); ipp == nil || isPrivateIP(ipp) {
ipp = net.ParseIP(ip)
if ipp == nil || isPrivateIP(ipp) {
continue
}
conn, err := fasthttp.DialDualStackTimeout("["+ip+"]:"+port, dialTimeout)
tcpAddr := &net.TCPAddr{
IP: ipp,
Port: port,
}
conn, err := net.DialTCP("tcp4", nil, tcpAddr)
if err == nil {
conn.Close()
return ip, nil
return tcpAddr, nil
}
}
return "", errNoSuitableIP
return nil, errNoSuitableIP
}
2 changes: 2 additions & 0 deletions domains-regex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
^discordapp.com$
^gateway\.discord\.gg$
\.gateway\.discord\.gg$
\.discordapp\.net$
^discordapp\.net$

# MS Office
\.office\.com$
Expand Down
27 changes: 14 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ var uncatchRecover = func() {
}
}

func httpsHandler(ctx *fasthttp.RequestCtx, hostname, dialStr string) error {
func httpsHandler(ctx *fasthttp.RequestCtx, hostname string, tcpAddr *net.TCPAddr) error {
var ioFrom net.Conn
destConn, err := fasthttp.DialDualStackTimeout(dialStr, dialTimeout)

destConn, err := net.DialTCP("tcp4", nil, tcpAddr)
if err != nil {
return err
}
Expand All @@ -49,7 +50,7 @@ func httpsHandler(ctx *fasthttp.RequestCtx, hostname, dialStr string) error {
// return err
log.Println("Remote handshake:", hostname, err)
// fallback
ioFrom, err = fasthttp.DialDualStackTimeout(dialStr, dialTimeout)
ioFrom, err = net.DialTCP("tcp4", nil, tcpAddr)
if err != nil {
return err
}
Expand Down Expand Up @@ -101,8 +102,8 @@ func ioTransfer(destination io.WriteCloser, source io.ReadCloser) {
}
}

var cacheIPMapLock sync.RWMutex
var cacheIPMap = map[string]string{}
var cacheAddrMapLock sync.RWMutex
var cacheTCPAddrMap = map[string]*net.TCPAddr{}

func requestHandler(ctx *fasthttp.RequestCtx) {
defer uncatchRecover()
Expand Down Expand Up @@ -131,24 +132,24 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
}
}

cacheIPMapLock.RLock()
ip, ok := cacheIPMap[host]
cacheIPMapLock.RUnlock()
cacheAddrMapLock.RLock()
tcpAddr, ok := cacheTCPAddrMap[host]
cacheAddrMapLock.RUnlock()
if ok == false {
ip, err = getUsableIP(hostname, port)
tcpAddr, err = getUsableIP(hostname, port)
if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
log.Println("No usable IP:", host, err)
return
}
cacheIPMapLock.Lock()
cacheIPMap[host] = ip
cacheIPMapLock.Unlock()
cacheAddrMapLock.Lock()
cacheTCPAddrMap[host] = tcpAddr
cacheAddrMapLock.Unlock()
}

// https connecttion
if bytes.Equal(ctx.Method(), []byte("CONNECT")) {
err = httpsHandler(ctx, hostname, "["+ip+"]:"+port)
err = httpsHandler(ctx, hostname, tcpAddr)
if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
log.Println("httpsHandler:", host, err)
Expand Down

0 comments on commit 7338bf3

Please sign in to comment.