1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-16 00:48:14 -04:00
v2fly/transport/internet/dialer.go

63 lines
1.3 KiB
Go
Raw Normal View History

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-10-02 17:43:58 -04:00
Stream *StreamConfig
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-06-14 16:54:08 -04:00
)
2016-10-02 17:43:58 -04:00
func Dial(src v2net.Address, dest v2net.Destination, settings *StreamConfig) (Connection, error) {
2016-08-13 09:42:56 -04:00
2016-07-10 09:27:58 -04:00
var connection Connection
var err error
2016-09-30 10:53:40 -04:00
dialerOptions := DialerOptions{
Stream: settings,
}
2016-09-20 05:53:05 -04:00
if dest.Network == v2net.Network_TCP {
2016-10-02 17:43:58 -04:00
switch settings.Network {
case v2net.Network_TCP:
2016-09-30 10:53:40 -04:00
connection, err = TCPDialer(src, dest, dialerOptions)
2016-10-02 17:43:58 -04:00
case v2net.Network_KCP:
2016-09-30 10:53:40 -04:00
connection, err = KCPDialer(src, dest, dialerOptions)
2016-10-02 17:43:58 -04:00
case v2net.Network_WebSocket:
2016-09-30 10:53:40 -04:00
connection, err = WSDialer(src, dest, dialerOptions)
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-09-30 10:53:40 -04:00
connection, err = RawTCPDialer(src, dest, dialerOptions)
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-09-30 10:53:40 -04:00
return UDPDialer(src, dest, dialerOptions)
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
}