2016-06-14 22:54:08 +02:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
2017-01-26 20:46:44 +01:00
|
|
|
"context"
|
2017-01-01 21:19:12 +01:00
|
|
|
|
2017-01-03 15:16:48 +01:00
|
|
|
"v2ray.com/core/common"
|
2017-08-29 12:56:57 +02:00
|
|
|
"v2ray.com/core/common/net"
|
2018-06-25 01:09:02 +02:00
|
|
|
"v2ray.com/core/common/session"
|
2016-08-20 20:55:45 +02:00
|
|
|
"v2ray.com/core/transport/internet"
|
2017-04-20 11:00:15 +02:00
|
|
|
"v2ray.com/core/transport/internet/tls"
|
2016-06-14 22:54:08 +02:00
|
|
|
)
|
|
|
|
|
2017-04-18 12:55:09 +02:00
|
|
|
func getTCPSettingsFromContext(ctx context.Context) *Config {
|
2018-09-07 14:50:25 +02:00
|
|
|
rawTCPSettings := internet.StreamSettingsFromContext(ctx)
|
2017-04-18 12:55:09 +02:00
|
|
|
if rawTCPSettings == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-07 14:50:25 +02:00
|
|
|
return rawTCPSettings.ProtocolSettings.(*Config)
|
2017-04-18 12:55:09 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:57:22 +01:00
|
|
|
// Dial dials a new TCP connection to the given destination.
|
2017-08-29 12:56:57 +02:00
|
|
|
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
|
2018-06-25 01:09:02 +02:00
|
|
|
newError("dialing TCP to ", dest).WriteToLog(session.ExportIDToError(ctx))
|
2018-09-18 23:09:54 +02:00
|
|
|
conn, err := internet.DialSystem(ctx, dest)
|
2017-04-07 21:54:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-06-14 22:54:08 +02:00
|
|
|
}
|
2017-12-17 00:53:17 +01:00
|
|
|
|
2018-02-28 15:15:22 +01:00
|
|
|
if config := tls.ConfigFromContext(ctx); config != nil {
|
|
|
|
conn = tls.Client(conn, config.GetTLSConfig(tls.WithDestination(dest), tls.WithNextProto("h2")))
|
2017-04-07 21:54:40 +02:00
|
|
|
}
|
2017-04-18 12:55:09 +02:00
|
|
|
|
|
|
|
tcpSettings := getTCPSettingsFromContext(ctx)
|
|
|
|
if tcpSettings != nil && tcpSettings.HeaderSettings != nil {
|
2017-04-07 21:54:40 +02:00
|
|
|
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
|
|
|
|
if err != nil {
|
2017-04-09 13:30:46 +02:00
|
|
|
return nil, newError("failed to get header settings").Base(err).AtError()
|
2017-04-07 21:54:40 +02:00
|
|
|
}
|
|
|
|
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
|
|
|
|
if err != nil {
|
2017-04-09 13:30:46 +02:00
|
|
|
return nil, newError("failed to create header authenticator").Base(err).AtError()
|
2016-11-02 22:26:21 +01:00
|
|
|
}
|
2017-04-07 21:54:40 +02:00
|
|
|
conn = auth.Client(conn)
|
2016-09-30 16:53:40 +02:00
|
|
|
}
|
2017-04-07 21:54:40 +02:00
|
|
|
return internet.Connection(conn), nil
|
2016-06-14 22:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-08-06 13:48:35 +02:00
|
|
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
2016-06-14 22:54:08 +02:00
|
|
|
}
|