feat: use internal resolver for wg network
This commit is contained in:
94
internal/proxy/proxy.go
Normal file
94
internal/proxy/proxy.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/zhsj/wghttp/internal/resolver"
|
||||
"github.com/zhsj/wghttp/internal/third_party/tailscale/httpproxy"
|
||||
"github.com/zhsj/wghttp/internal/third_party/tailscale/proxymux"
|
||||
"github.com/zhsj/wghttp/internal/third_party/tailscale/socks5"
|
||||
)
|
||||
|
||||
type dialer func(ctx context.Context, network, address string) (net.Conn, error)
|
||||
|
||||
type Proxy struct {
|
||||
Dial dialer
|
||||
DNS string
|
||||
Stats func() (any, error)
|
||||
}
|
||||
|
||||
func statsHandler(next http.Handler, stats func() (any, error)) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Host != "" || r.URL.Path != "/stats" {
|
||||
next.ServeHTTP(rw, r)
|
||||
return
|
||||
}
|
||||
s, err := stats()
|
||||
if err != nil {
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
} else {
|
||||
resp, _ := json.MarshalIndent(s, "", " ")
|
||||
rw.Header().Set("Content-Type", "application/json")
|
||||
_, _ = rw.Write(append(resp, '\n'))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func dialWithDNS(dial dialer, dns string) dialer {
|
||||
resolv := resolver.New(dns, dial)
|
||||
|
||||
return func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
host, port, err := net.SplitHostPort(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err == nil {
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
return dial(ctx, network, address)
|
||||
}
|
||||
}
|
||||
|
||||
ips, err := resolv.LookupHost(ctx, host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
lastErr error
|
||||
conn net.Conn
|
||||
)
|
||||
for _, ip := range ips {
|
||||
addr := net.JoinHostPort(ip, port)
|
||||
conn, lastErr = dial(ctx, network, addr)
|
||||
if lastErr == nil {
|
||||
return conn, nil
|
||||
}
|
||||
}
|
||||
return nil, lastErr
|
||||
}
|
||||
}
|
||||
|
||||
func (p Proxy) Serve(ln net.Listener) {
|
||||
d := dialWithDNS(p.Dial, p.DNS)
|
||||
|
||||
socksListener, httpListener := proxymux.SplitSOCKSAndHTTP(ln)
|
||||
|
||||
httpProxy := &http.Server{Handler: statsHandler(httpproxy.Handler(d), p.Stats)}
|
||||
socksProxy := &socks5.Server{Dialer: d}
|
||||
|
||||
errc := make(chan error, 2)
|
||||
go func() {
|
||||
if err := httpProxy.Serve(httpListener); err != nil {
|
||||
errc <- err
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
if err := socksProxy.Serve(socksListener); err != nil {
|
||||
errc <- err
|
||||
}
|
||||
}()
|
||||
<-errc
|
||||
}
|
||||
45
internal/proxy/proxy_test.go
Normal file
45
internal/proxy/proxy_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDialWithDNS(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
stdDiar := net.Dialer{
|
||||
Resolver: &net.Resolver{
|
||||
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return nil, fmt.Errorf("dial to %s", address)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// d := dialWithDNS(stdDiar.DialContext, "https://223.5.5.5")
|
||||
d := dialWithDNS(func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
t.Logf("dial to %s:%s", network, address)
|
||||
return stdDiar.DialContext(ctx, network, address)
|
||||
}, "tls://223.5.5.5")
|
||||
|
||||
for _, addr := range []string{
|
||||
"example.com:80",
|
||||
"223.6.6.6:80",
|
||||
"localhost:22",
|
||||
"127.0.0.1:22",
|
||||
} {
|
||||
t.Run(addr, func(t *testing.T) {
|
||||
conn, err := d(context.Background(), "tcp", addr)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Logf("remote %s", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user