65 lines
1.4 KiB
Go
Raw Normal View History

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