2016-06-14 16:54:08 -04:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
2017-01-26 14:46:44 -05:00
|
|
|
"context"
|
2017-01-01 15:19:12 -05:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/serial"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/session"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet"
|
2022-12-16 13:48:25 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet/security"
|
2016-06-14 16:54:08 -04:00
|
|
|
)
|
|
|
|
|
2018-01-02 12:57:22 -05:00
|
|
|
// Dial dials a new TCP connection to the given destination.
|
2018-11-21 08:54:40 -05:00
|
|
|
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
|
2018-06-24 19:09:02 -04:00
|
|
|
newError("dialing TCP to ", dest).WriteToLog(session.ExportIDToError(ctx))
|
2018-11-21 08:54:40 -05:00
|
|
|
conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
|
2017-04-07 15:54:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
2017-12-16 18:53:17 -05:00
|
|
|
|
2022-12-16 13:48:25 -05:00
|
|
|
securityEngine, err := security.CreateSecurityEngineFromSettings(ctx, streamSettings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("unable to create security engine").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if securityEngine != nil {
|
2023-01-14 07:04:44 -05:00
|
|
|
conn, err = securityEngine.Client(conn, security.OptionWithDestination{Dest: dest})
|
2022-12-16 13:48:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("unable to create security protocol client from security engine").Base(err)
|
|
|
|
}
|
2017-04-07 15:54:40 -04:00
|
|
|
}
|
2017-04-18 06:55:09 -04:00
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
tcpSettings := streamSettings.ProtocolSettings.(*Config)
|
|
|
|
if tcpSettings.HeaderSettings != nil {
|
2021-06-19 09:36:54 -04:00
|
|
|
headerConfig, err := serial.GetInstanceOf(tcpSettings.HeaderSettings)
|
2017-04-07 15:54:40 -04:00
|
|
|
if err != nil {
|
2017-04-09 07:30:46 -04:00
|
|
|
return nil, newError("failed to get header settings").Base(err).AtError()
|
2017-04-07 15:54:40 -04:00
|
|
|
}
|
|
|
|
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
|
|
|
|
if err != nil {
|
2017-04-09 07:30:46 -04:00
|
|
|
return nil, newError("failed to create header authenticator").Base(err).AtError()
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
2017-04-07 15:54:40 -04:00
|
|
|
conn = auth.Client(conn)
|
2016-09-30 10:53:40 -04:00
|
|
|
}
|
2017-04-07 15:54:40 -04:00
|
|
|
return internet.Connection(conn), nil
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-08-06 07:48:35 -04:00
|
|
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|