Skip to content

Commit

Permalink
Merge pull request v2ray#219 from xiaokangwang/substituteDialer
Browse files Browse the repository at this point in the history
Solve v2ray#218: Supporting substitute of the default Dialer.
  • Loading branch information
DarienRaymond authored Jul 26, 2016
2 parents b72afc7 + d1b4461 commit 27a29da
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
40 changes: 22 additions & 18 deletions transport/internet/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
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
}
dialer.LocalAddr = addr
return dialer.Dial(dest.Network().String(), dest.NetAddr())
}

return dialer.Dial(dest.Network().String(), dest.NetAddr())
}
18 changes: 18 additions & 0 deletions transport/internet/dialerSubstitute.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 27a29da

Please sign in to comment.