Compare commits
2 Commits
96052b786d
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e4acfff5a2 | |||
| f1b4b88091 |
@@ -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
|
||||
|
||||
@@ -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=
|
||||
|
||||
@@ -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 == "" {
|
||||
@@ -40,6 +47,9 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if endpoint.Redir {
|
||||
u, _ := url.Parse(endpoint.To)
|
||||
r.URL = u
|
||||
r.Host = u.Host
|
||||
http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
@@ -68,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,
|
||||
|
||||
Reference in New Issue
Block a user