Chore: use custom dialer

This commit is contained in:
Dreamacro
2020-02-09 17:02:48 +08:00
parent a55be58c01
commit afc9f3f59a
10 changed files with 68 additions and 5 deletions

View 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
View 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
)