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:
commit
27a29da7bf
@ -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) {
|
||||
dialer := &net.Dialer{
|
||||
Timeout: time.Second * 60,
|
||||
DualStack: true,
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
if isDefaultDialerSubstituted() {
|
||||
dialer := v2AlternativeDialer
|
||||
return (*dialer).Dial(dest.Network().String(), dest.NetAddr())
|
||||
} else {
|
||||
dialer := &net.Dialer{
|
||||
Timeout: time.Second * 60,
|
||||
DualStack: true,
|
||||
}
|
||||
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())
|
||||
}
|
||||
|
18
transport/internet/dialerSubstitute.go
Normal file
18
transport/internet/dialerSubstitute.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user