Chore: mix the proxy adapter and interface to dns client

This commit is contained in:
yaling888 2022-06-03 11:27:41 +08:00
parent a4d135ed21
commit 3dbba5d8d2
4 changed files with 26 additions and 11 deletions

View File

@ -76,7 +76,7 @@ mitm:
``` ```
### DNS configuration ### DNS configuration
Support resolve ip with a proxy tunnel. Support resolve ip with a proxy tunnel or interface.
Support `geosite` with `fallback-filter`. Support `geosite` with `fallback-filter`.
@ -96,8 +96,8 @@ Use `curl -X POST controllerip:port/cache/fakeip/flush` to flush persistence fak
- https://doh.pub/dns-query - https://doh.pub/dns-query
- tls://223.5.5.5:853 - tls://223.5.5.5:853
fallback: fallback:
- 'tls://8.8.4.4:853#proxy or interface'
- 'https://1.0.0.1/dns-query#Proxy' # append the proxy adapter name to the end of DNS URL with '#' prefix. - 'https://1.0.0.1/dns-query#Proxy' # append the proxy adapter name to the end of DNS URL with '#' prefix.
- 'tls://8.8.4.4:853#Proxy'
fallback-filter: fallback-filter:
geoip: false geoip: false
geosite: geosite:

View File

@ -54,10 +54,14 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error)
} }
var conn net.Conn var conn net.Conn
if c.proxyAdapter == "" { if c.proxyAdapter != "" {
conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port), options...)
} else {
conn, err = dialContextWithProxyAdapter(ctx, c.proxyAdapter, network, ip, c.port, options...) conn, err = dialContextWithProxyAdapter(ctx, c.proxyAdapter, network, ip, c.port, options...)
if err == errProxyNotFound {
options = append(options[:0], dialer.WithInterface(c.proxyAdapter), dialer.WithRoutingMark(0))
conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port), options...)
}
} else {
conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port), options...)
} }
if err != nil { if err != nil {

View File

@ -69,7 +69,9 @@ func (dc *dohClient) doRequest(req *http.Request) (msg *D.Msg, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close() defer func() {
_ = resp.Body.Close()
}()
buf, err := io.ReadAll(resp.Body) buf, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
@ -97,11 +99,17 @@ func newDoHClient(url string, r *Resolver, proxyAdapter string) *dohClient {
return nil, err return nil, err
} }
if proxyAdapter == "" { if proxyAdapter != "" {
return dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port)) var conn net.Conn
} else { conn, err = dialContextWithProxyAdapter(ctx, proxyAdapter, "tcp", ip, port)
return dialContextWithProxyAdapter(ctx, proxyAdapter, "tcp", ip, port) if err == errProxyNotFound {
options := []dialer.Option{dialer.WithInterface(proxyAdapter), dialer.WithRoutingMark(0)}
conn, err = dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port), options...)
}
return conn, err
} }
return dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port))
}, },
}, },
} }

View File

@ -3,6 +3,7 @@ package dns
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"net" "net"
"net/netip" "net/netip"
@ -19,6 +20,8 @@ import (
D "github.com/miekg/dns" D "github.com/miekg/dns"
) )
var errProxyNotFound = errors.New("proxy adapter not found")
func putMsgToCache(c *cache.LruCache[string, *D.Msg], key string, msg *D.Msg) { func putMsgToCache(c *cache.LruCache[string, *D.Msg], key string, msg *D.Msg) {
var ttl uint32 var ttl uint32
switch { switch {
@ -135,7 +138,7 @@ func (wpc *wrapPacketConn) RemoteAddr() net.Addr {
func dialContextWithProxyAdapter(ctx context.Context, adapterName string, network string, dstIP netip.Addr, port string, opts ...dialer.Option) (net.Conn, error) { func dialContextWithProxyAdapter(ctx context.Context, adapterName string, network string, dstIP netip.Addr, port string, opts ...dialer.Option) (net.Conn, error) {
proxy, ok := tunnel.Proxies()[adapterName] proxy, ok := tunnel.Proxies()[adapterName]
if !ok { if !ok {
return nil, fmt.Errorf("proxy adapter [%s] not found", adapterName) return nil, errProxyNotFound
} }
networkType := C.TCP networkType := C.TCP