1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-29 23:37:54 -04:00
v2fly/transport/internet/kcp/dialer.go

91 lines
2.1 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
package kcp
import (
"context"
2016-09-30 10:53:40 -04:00
"crypto/tls"
2017-12-03 15:29:27 -05:00
"io"
2016-07-12 12:54:16 -04:00
"sync/atomic"
2016-12-08 10:27:41 -05:00
2017-02-10 10:42:24 -05:00
"v2ray.com/core/app/log"
2017-01-03 09:16:48 -05:00
"v2ray.com/core/common"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/dice"
"v2ray.com/core/common/net"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/transport/internet"
2016-09-30 10:53:40 -04:00
v2tls "v2ray.com/core/transport/internet/tls"
2016-06-14 16:54:08 -04:00
)
2016-07-12 12:54:16 -04:00
var (
2017-04-27 05:54:15 -04:00
globalConv = uint32(dice.RollUint16())
2016-07-12 12:54:16 -04:00
)
2017-12-03 15:29:27 -05:00
func fetchInput(ctx context.Context, input io.Reader, reader PacketReader, conn *Connection) {
2017-04-15 15:19:21 -04:00
payload := buf.New()
2016-11-27 02:58:31 -05:00
defer payload.Release()
for {
2017-12-03 15:29:27 -05:00
err := payload.Reset(buf.ReadFrom(input))
2016-11-27 02:58:31 -05:00
if err != nil {
payload.Release()
return
}
2017-12-03 15:29:27 -05:00
segments := reader.Read(payload.Bytes())
if len(segments) > 0 {
conn.Input(segments)
2016-11-27 02:58:31 -05:00
}
}
}
func DialKCP(ctx context.Context, dest net.Destination) (internet.Connection, error) {
dest.Network = net.Network_UDP
2017-04-09 07:30:46 -04:00
log.Trace(newError("dialing mKCP to ", dest))
2016-11-27 02:58:31 -05:00
src := internet.DialerSourceFromContext(ctx)
rawConn, err := internet.DialSystem(ctx, src, dest)
if err != nil {
2017-04-14 08:58:32 -04:00
return nil, newError("failed to dial to dest: ", err).AtWarning().Base(err)
}
2016-06-14 18:30:11 -04:00
kcpSettings := internet.TransportSettingsFromContext(ctx).(*Config)
2016-10-02 17:43:58 -04:00
2016-12-08 10:27:41 -05:00
header, err := kcpSettings.GetPackerHeader()
2016-08-06 15:59:22 -04:00
if err != nil {
2017-04-09 07:30:46 -04:00
return nil, newError("failed to create packet header").Base(err)
2016-12-08 10:27:41 -05:00
}
security, err := kcpSettings.GetSecurity()
if err != nil {
2017-04-09 07:30:46 -04:00
return nil, newError("failed to create security").Base(err)
2016-08-06 15:59:22 -04:00
}
2017-12-03 15:29:27 -05:00
reader := &KCPPacketReader{
Header: header,
Security: security,
}
writer := &KCPPacketWriter{
Header: header,
Security: security,
Writer: rawConn,
}
2016-07-12 12:54:16 -04:00
conv := uint16(atomic.AddUint32(&globalConv, 1))
2017-12-14 17:24:40 -05:00
session := NewConnection(ConnMetadata{
LocalAddr: rawConn.LocalAddr(),
RemoteAddr: rawConn.RemoteAddr(),
Conversation: conv,
2017-12-03 15:29:27 -05:00
}, writer, rawConn, kcpSettings)
go fetchInput(ctx, rawConn, reader, session)
2016-06-17 10:51:41 -04:00
2017-11-23 17:46:46 -05:00
var iConn internet.Connection = session
2016-09-30 10:53:40 -04:00
2017-12-16 18:53:17 -05:00
if config := v2tls.ConfigFromContext(ctx, v2tls.WithDestination(dest)); config != nil {
tlsConn := tls.Client(iConn, config.GetTLSConfig())
iConn = tlsConn
2016-09-30 10:53:40 -04:00
}
return iConn, nil
2016-06-14 16:54:08 -04:00
}
func init() {
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_MKCP, DialKCP))
2016-06-14 16:54:08 -04:00
}