1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

Merge pull request #219 from xiaokangwang/substituteDialer

Solve #218: Supporting substitute of the default Dialer.
This commit is contained in:
Darien Raymond 2016-07-26 16:33:14 +02:00 committed by GitHub
commit 27a29da7bf
2 changed files with 41 additions and 19 deletions

View File

@ -56,26 +56,30 @@ func Dial(src v2net.Address, dest v2net.Destination, settings *StreamSettings) (
} }
func DialToDest(src v2net.Address, dest v2net.Destination) (net.Conn, error) { func DialToDest(src v2net.Address, dest v2net.Destination) (net.Conn, error) {
dialer := &net.Dialer{
Timeout: time.Second * 60,
DualStack: true,
}
if src != nil && src != v2net.AnyIP { if isDefaultDialerSubstituted() {
var addr net.Addr dialer := v2AlternativeDialer
if dest.IsTCP() { return (*dialer).Dial(dest.Network().String(), dest.NetAddr())
addr = &net.TCPAddr{ } else {
IP: src.IP(), dialer := &net.Dialer{
Port: 0, Timeout: time.Second * 60,
} DualStack: true,
} else {
addr = &net.UDPAddr{
IP: src.IP(),
Port: 0,
}
} }
dialer.LocalAddr = addr if src != nil && src != v2net.AnyIP {
var addr net.Addr
if dest.IsTCP() {
addr = &net.TCPAddr{
IP: src.IP(),
Port: 0,
}
} else {
addr = &net.UDPAddr{
IP: src.IP(),
Port: 0,
}
}
dialer.LocalAddr = addr
}
return dialer.Dial(dest.Network().String(), dest.NetAddr())
} }
return dialer.Dial(dest.Network().String(), dest.NetAddr())
} }

View File

@ -0,0 +1,18 @@
package internet
import "net"
var v2AlternativeDialer *V2AlternativeDialerT
type V2AlternativeDialerT interface {
Dial(nw string, ad string) (net.Conn, error)
}
func SubstituteDialer(substituteWith V2AlternativeDialerT) error {
v2AlternativeDialer = &substituteWith
return nil
}
func isDefaultDialerSubstituted() bool {
return (v2AlternativeDialer != nil)
}