2016-06-14 16:54:08 -04:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
2016-06-14 16:54:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrUnsupportedStreamType = errors.New("Unsupported stream type.")
|
|
|
|
)
|
|
|
|
|
2016-09-30 10:53:40 -04:00
|
|
|
type DialerOptions struct {
|
2016-11-10 17:41:28 -05:00
|
|
|
Stream *StreamConfig
|
|
|
|
Proxy *ProxyConfig
|
2016-09-30 10:53:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Dialer func(src v2net.Address, dest v2net.Destination, options DialerOptions) (Connection, error)
|
2016-06-14 16:54:08 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
TCPDialer Dialer
|
|
|
|
KCPDialer Dialer
|
|
|
|
RawTCPDialer Dialer
|
|
|
|
UDPDialer Dialer
|
2016-08-13 09:42:56 -04:00
|
|
|
WSDialer Dialer
|
2016-11-08 18:17:09 -05:00
|
|
|
ProxyDialer Dialer
|
2016-06-14 16:54:08 -04:00
|
|
|
)
|
|
|
|
|
2016-11-08 18:17:09 -05:00
|
|
|
func Dial(src v2net.Address, dest v2net.Destination, options DialerOptions) (Connection, error) {
|
2016-11-10 17:41:28 -05:00
|
|
|
if options.Proxy.HasTag() && ProxyDialer != nil {
|
2016-11-08 18:17:09 -05:00
|
|
|
return ProxyDialer(src, dest, options)
|
|
|
|
}
|
2016-08-13 09:42:56 -04:00
|
|
|
|
2016-07-10 09:27:58 -04:00
|
|
|
var connection Connection
|
|
|
|
var err error
|
2016-09-20 05:53:05 -04:00
|
|
|
if dest.Network == v2net.Network_TCP {
|
2016-11-08 18:17:09 -05:00
|
|
|
switch options.Stream.Network {
|
2016-10-02 17:43:58 -04:00
|
|
|
case v2net.Network_TCP:
|
2016-11-08 18:17:09 -05:00
|
|
|
connection, err = TCPDialer(src, dest, options)
|
2016-10-02 17:43:58 -04:00
|
|
|
case v2net.Network_KCP:
|
2016-11-08 18:17:09 -05:00
|
|
|
connection, err = KCPDialer(src, dest, options)
|
2016-10-02 17:43:58 -04:00
|
|
|
case v2net.Network_WebSocket:
|
2016-11-08 18:17:09 -05:00
|
|
|
connection, err = WSDialer(src, dest, options)
|
2016-08-13 09:42:56 -04:00
|
|
|
|
2016-09-19 10:39:11 -04:00
|
|
|
// This check has to be the last one.
|
2016-10-02 17:43:58 -04:00
|
|
|
case v2net.Network_RawTCP:
|
2016-11-08 18:17:09 -05:00
|
|
|
connection, err = RawTCPDialer(src, dest, options)
|
2016-07-10 09:27:58 -04:00
|
|
|
default:
|
|
|
|
return nil, ErrUnsupportedStreamType
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
2016-07-10 09:27:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-13 09:42:56 -04:00
|
|
|
|
2016-09-30 10:53:40 -04:00
|
|
|
return connection, nil
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:17:09 -05:00
|
|
|
return UDPDialer(src, dest, options)
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func DialToDest(src v2net.Address, dest v2net.Destination) (net.Conn, error) {
|
2016-07-26 11:11:58 -04:00
|
|
|
return effectiveSystemDialer.Dial(src, dest)
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|