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