1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-26 09:25:23 +00:00
v2fly/transport/internet/dialer.go

72 lines
1.7 KiB
Go
Raw Normal View History

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