1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-27 04:27:20 -05:00
v2fly/transport/internet/kcp/dialer.go

96 lines
2.4 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
package kcp
import (
"context"
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
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/dice"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/transport/internet"
"github.com/v2fly/v2ray-core/v4/transport/internet/tls"
2016-06-14 16:54:08 -04:00
)
2021-05-19 17:28:52 -04:00
var globalConv = uint32(dice.RollUint16())
2016-07-12 12:54:16 -04:00
func fetchInput(_ 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
}
}
}
2019-02-22 18:01:23 -05:00
// DialKCP dials a new KCP connections to the specific destination.
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
2020-10-06 12:25:02 -04:00
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
iConn = tls.Client(iConn, config.GetTLSConfig(tls.WithDestination(dest)))
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(protocolName, DialKCP))
}