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

99 lines
2.3 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-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) {
2018-02-24 17:23:10 -05:00
cache := make(chan *buf.Buffer, 1024)
go func() {
for {
payload := buf.New()
2018-11-02 10:01:33 -04:00
if _, err := payload.ReadFrom(input); err != nil {
2018-02-24 17:23:10 -05:00
payload.Release()
close(cache)
return
}
select {
case cache <- payload:
default:
payload.Release()
}
2016-11-27 02:58:31 -05:00
}
2018-02-24 17:23:10 -05:00
}()
for payload := range cache {
2017-12-03 15:29:27 -05:00
segments := reader.Read(payload.Bytes())
2018-02-24 17:23:10 -05:00
payload.Release()
2017-12-03 15:29:27 -05:00
if len(segments) > 0 {
conn.Input(segments)
2016-11-27 02:58:31 -05:00
}
}
}
func DialKCP(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
dest.Network = net.Network_UDP
2017-12-19 15:28:12 -05:00
newError("dialing mKCP to ", dest).WriteToLog()
2016-11-27 02:58:31 -05:00
rawConn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
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 := streamSettings.ProtocolSettings.(*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
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
2018-02-28 09:15:22 -05:00
tlsConn := tls.Client(iConn, config.GetTLSConfig(v2tls.WithDestination(dest)))
2017-12-16 18:53:17 -05:00
iConn = tlsConn
2016-09-30 10:53:40 -04:00
}
return iConn, nil
2016-06-14 16:54:08 -04:00
}
func init() {
2018-08-06 07:48:35 -04:00
common.Must(internet.RegisterTransportDialer(protocolName, DialKCP))
2016-06-14 16:54:08 -04:00
}