1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 10:37:09 -05:00
v2fly/transport/internet/dialer.go

72 lines
1.7 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
2016-08-13 09:42:56 -04:00
WSDialer Dialer
2016-06-14 16:54:08 -04:00
)
func Dial(src v2net.Address, dest v2net.Destination, settings *StreamSettings) (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-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-08-13 09:42:56 -04:00
case settings.IsCapableOf(StreamConnectionTypeWebSocket):
connection, err = WSDialer(src, dest)
/*Warning: Hours wasted: the following item must be last one
internet.StreamConnectionType have a default value of 1,
so the following attempt will catch all.
*/
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
}
2016-08-13 09:42:56 -04:00
2016-07-10 09:27:58 -04:00
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
}