Chore: use custom dialer
This commit is contained in:
parent
a55be58c01
commit
afc9f3f59a
10 changed files with 68 additions and 5 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/Dreamacro/clash/component/dialer"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
|
@ -26,7 +27,7 @@ func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn,
|
|||
}
|
||||
|
||||
func (d *Direct) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
||||
pc, err := net.ListenPacket("udp", "")
|
||||
pc, err := dialer.ListenPacket("udp", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/Dreamacro/clash/common/structure"
|
||||
"github.com/Dreamacro/clash/component/dialer"
|
||||
obfs "github.com/Dreamacro/clash/component/simple-obfs"
|
||||
"github.com/Dreamacro/clash/component/socks5"
|
||||
v2rayObfs "github.com/Dreamacro/clash/component/v2ray-plugin"
|
||||
|
@ -83,7 +84,7 @@ func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata) (C
|
|||
}
|
||||
|
||||
func (ss *ShadowSocks) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
||||
pc, err := net.ListenPacket("udp", "")
|
||||
pc, err := dialer.ListenPacket("udp", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/Dreamacro/clash/component/dialer"
|
||||
"github.com/Dreamacro/clash/component/socks5"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
@ -96,7 +97,7 @@ func (ss *Socks5) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
pc, err := net.ListenPacket("udp", "")
|
||||
pc, err := dialer.ListenPacket("udp", "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Dreamacro/clash/component/dialer"
|
||||
"github.com/Dreamacro/clash/component/socks5"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/dns"
|
||||
|
@ -107,7 +108,7 @@ func dialContext(ctx context.Context, network, address string) (net.Conn, error)
|
|||
var primary, fallback dialResult
|
||||
|
||||
startRacer := func(ctx context.Context, host string, ipv6 bool) {
|
||||
dialer := net.Dialer{}
|
||||
dialer := dialer.Dialer()
|
||||
result := dialResult{ipv6: ipv6, done: true}
|
||||
defer func() {
|
||||
select {
|
||||
|
|
|
@ -5,6 +5,8 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Dreamacro/clash/component/dialer"
|
||||
)
|
||||
|
||||
// Vehicle Type
|
||||
|
@ -85,6 +87,7 @@ func (h *HTTPVehicle) Read() ([]byte, error) {
|
|||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
DialContext: dialer.DialContext,
|
||||
}
|
||||
|
||||
client := http.Client{Transport: transport}
|
||||
|
|
2
common/cache/lrucache.go
vendored
2
common/cache/lrucache.go
vendored
|
@ -12,7 +12,7 @@ import (
|
|||
type Option func(*LruCache)
|
||||
|
||||
// EvictCallback is used to get a callback when a cache entry is evicted
|
||||
type EvictCallback func(key interface{}, value interface{})
|
||||
type EvictCallback = func(key interface{}, value interface{})
|
||||
|
||||
// WithEvict set the evict callback
|
||||
func WithEvict(cb EvictCallback) Option {
|
||||
|
|
38
component/dialer/dialer.go
Normal file
38
component/dialer/dialer.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package dialer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
)
|
||||
|
||||
func Dialer() *net.Dialer {
|
||||
dialer := &net.Dialer{}
|
||||
if DialerHook != nil {
|
||||
DialerHook(dialer)
|
||||
}
|
||||
|
||||
return dialer
|
||||
}
|
||||
|
||||
func ListenConfig() *net.ListenConfig {
|
||||
cfg := &net.ListenConfig{}
|
||||
if ListenConfigHook != nil {
|
||||
ListenConfigHook(cfg)
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func Dial(network, address string) (net.Conn, error) {
|
||||
return DialContext(context.Background(), network, address)
|
||||
}
|
||||
|
||||
func DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
dailer := Dialer()
|
||||
return dailer.DialContext(ctx, network, address)
|
||||
}
|
||||
|
||||
func ListenPacket(network, address string) (net.PacketConn, error) {
|
||||
lc := ListenConfig()
|
||||
return lc.ListenPacket(context.Background(), network, address)
|
||||
}
|
11
component/dialer/hook.go
Normal file
11
component/dialer/hook.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package dialer
|
||||
|
||||
import "net"
|
||||
|
||||
type DialerHookFunc = func(*net.Dialer)
|
||||
type ListenConfigHookFunc = func(*net.ListenConfig)
|
||||
|
||||
var (
|
||||
DialerHook DialerHookFunc = nil
|
||||
ListenConfigHook ListenConfigHookFunc = nil
|
||||
)
|
|
@ -3,6 +3,8 @@ package dns
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Dreamacro/clash/component/dialer"
|
||||
|
||||
D "github.com/miekg/dns"
|
||||
)
|
||||
|
||||
|
@ -16,6 +18,8 @@ func (c *client) Exchange(m *D.Msg) (msg *D.Msg, err error) {
|
|||
}
|
||||
|
||||
func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
||||
c.Client.Dialer = dialer.Dialer()
|
||||
|
||||
// Please note that miekg/dns ExchangeContext doesn't respond to context cancel.
|
||||
msg, _, err = c.Client.ExchangeContext(ctx, m, c.Address)
|
||||
return
|
||||
|
|
|
@ -7,6 +7,8 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/Dreamacro/clash/component/dialer"
|
||||
|
||||
D "github.com/miekg/dns"
|
||||
)
|
||||
|
||||
|
@ -17,6 +19,7 @@ const (
|
|||
|
||||
var dohTransport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{ClientSessionCache: globalSessionCache},
|
||||
DialContext: dialer.DialContext,
|
||||
}
|
||||
|
||||
type dohClient struct {
|
||||
|
|
Loading…
Reference in a new issue