Compare commits
2 Commits
96052b786d
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e4acfff5a2 | |||
| f1b4b88091 |
@@ -3,3 +3,5 @@ module rproxy4
|
|||||||
go 1.24.2
|
go 1.24.2
|
||||||
|
|
||||||
require gopkg.in/yaml.v2 v2.4.0
|
require gopkg.in/yaml.v2 v2.4.0
|
||||||
|
|
||||||
|
require github.com/viki-org/dnscache v0.0.0-20130720023526-c70c1f23c5d8 // indirect
|
||||||
|
|||||||
@@ -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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/viki-org/dnscache"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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) {
|
func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
endpoint := c.endpoint(r)
|
endpoint := c.endpoint(r)
|
||||||
if endpoint.To == "" {
|
if endpoint.To == "" {
|
||||||
@@ -40,6 +47,9 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if endpoint.Redir {
|
if endpoint.Redir {
|
||||||
|
u, _ := url.Parse(endpoint.To)
|
||||||
|
r.URL = u
|
||||||
|
r.Host = u.Host
|
||||||
http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
|
http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -68,8 +78,18 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[%s] %v", c.key(r), err)
|
log.Printf("[%s] %v", c.key(r), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var transport http.Transport
|
var transport http.Transport
|
||||||
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
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 := httputil.NewSingleHostReverseProxy(u)
|
||||||
proxy.Transport = redirPurge{
|
proxy.Transport = redirPurge{
|
||||||
proxyHost: r.Host,
|
proxyHost: r.Host,
|
||||||
|
|||||||
Reference in New Issue
Block a user