1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-31 00:08:08 -04:00
v2fly/transport/internet/dialer.go

60 lines
1.4 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
package internet
import (
2016-07-10 09:27:58 -04:00
"crypto/tls"
2016-06-14 16:54:08 -04:00
"errors"
"net"
v2net "github.com/v2ray/v2ray-core/common/net"
2016-07-10 09:27:58 -04:00
v2tls "github.com/v2ray/v2ray-core/transport/internet/tls"
2016-06-14 16:54:08 -04:00
)
var (
ErrUnsupportedStreamType = errors.New("Unsupported stream type.")
)
type Dialer func(src v2net.Address, dest v2net.Destination) (Connection, error)
var (
TCPDialer Dialer
KCPDialer Dialer
RawTCPDialer Dialer
UDPDialer Dialer
)
func Dial(src v2net.Address, dest v2net.Destination, settings *StreamSettings) (Connection, error) {
2016-07-10 09:27:58 -04:00
var connection Connection
var err error
2016-06-14 16:54:08 -04:00
if dest.IsTCP() {
switch {
case settings.IsCapableOf(StreamConnectionTypeTCP):
2016-07-10 09:27:58 -04:00
connection, err = TCPDialer(src, dest)
2016-06-14 19:08:03 -04:00
case settings.IsCapableOf(StreamConnectionTypeKCP):
2016-07-10 09:27:58 -04:00
connection, err = KCPDialer(src, dest)
2016-06-14 16:54:08 -04:00
case settings.IsCapableOf(StreamConnectionTypeRawTCP):
2016-07-10 09:27:58 -04:00
connection, err = RawTCPDialer(src, dest)
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
}
if settings.Security == StreamSecurityTypeNone {
return connection, nil
}
config := settings.TLSSettings.GetTLSConfig()
if dest.Address().IsDomain() {
config.ServerName = dest.Address().Domain()
}
tlsConn := tls.Client(connection, config)
return v2tls.NewConnection(tlsConn), nil
2016-06-14 16:54:08 -04:00
}
return UDPDialer(src, dest)
}
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
}