diff --git a/go.mod b/go.mod index 6017e80..485a6dd 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module rproxy4 go 1.24.2 require gopkg.in/yaml.v2 v2.4.0 + +require github.com/viki-org/dnscache v0.0.0-20130720023526-c70c1f23c5d8 // indirect diff --git a/go.sum b/go.sum index dd0bc19..5d8f9f4 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/viki-org/dnscache v0.0.0-20130720023526-c70c1f23c5d8 h1:EVObHAr8DqpoJCVv6KYTle8FEImKhtkfcZetNqxDoJQ= +github.com/viki-org/dnscache v0.0.0-20130720023526-c70c1f23c5d8/go.mod h1:dniwbG03GafCjFohMDmz6Zc6oCuiqgH6tGNyXTkHzXE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/main.go b/main.go index 346d30b..607c5ab 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,18 @@ package main import ( + "context" "crypto/tls" "fmt" "log" + "net" "net/http" "net/http/httputil" "net/url" "strings" + "time" + + "github.com/viki-org/dnscache" ) func main() { @@ -32,6 +37,8 @@ func main() { } } +var resolver = dnscache.New(time.Minute * 500) + func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) { endpoint := c.endpoint(r) if endpoint.To == "" { @@ -71,8 +78,18 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err != nil { log.Printf("[%s] %v", c.key(r), err) } + var transport http.Transport transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + if ip, _ := resolver.FetchOneString(strings.Split(u.Host, ":")[0]); ip != "" { + transport.DialContext = func(ctx context.Context, network string, address string) (net.Conn, error) { + if idx := strings.LastIndex(u.Host, ":"); idx > 0 { + ip = ip + u.Host[idx:] + } + return (&net.Dialer{}).DialContext(ctx, network, ip) + } + } + proxy := httputil.NewSingleHostReverseProxy(u) proxy.Transport = redirPurge{ proxyHost: r.Host, diff --git a/rproxy4 b/rproxy4 new file mode 100755 index 0000000..25f1ff9 Binary files /dev/null and b/rproxy4 differ