v2fly/transport/internet/kcp/dialer.go

98 lines
2.4 KiB
Go
Raw Normal View History

2019-02-01 19:08:21 +00:00
// +build !confonly
2016-06-14 20:54:08 +00:00
package kcp
import (
"context"
2017-12-03 20:29:27 +00:00
"io"
2016-07-12 16:54:16 +00:00
"sync/atomic"
2016-12-08 15:27:41 +00:00
2021-02-16 20:31:50 +00: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 20:54:08 +00:00
)
2021-05-19 21:28:52 +00:00
var globalConv = uint32(dice.RollUint16())
2016-07-12 16:54:16 +00:00
func fetchInput(_ context.Context, input io.Reader, reader PacketReader, conn *Connection) {
2018-02-24 22:23:10 +00:00
cache := make(chan *buf.Buffer, 1024)
go func() {
for {
payload := buf.New()
2018-11-02 14:01:33 +00:00
if _, err := payload.ReadFrom(input); err != nil {
2018-02-24 22:23:10 +00:00
payload.Release()
close(cache)
return
}
select {
case cache <- payload:
default:
payload.Release()
}
2016-11-27 07:58:31 +00:00
}
2018-02-24 22:23:10 +00:00
}()
for payload := range cache {
2017-12-03 20:29:27 +00:00
segments := reader.Read(payload.Bytes())
2018-02-24 22:23:10 +00:00
payload.Release()
2017-12-03 20:29:27 +00:00
if len(segments) > 0 {
conn.Input(segments)
2016-11-27 07:58:31 +00:00
}
}
}
2019-02-22 23:01:23 +00: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 20:28:12 +00:00
newError("dialing mKCP to ", dest).WriteToLog()
2016-11-27 07:58:31 +00:00
rawConn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
if err != nil {
2017-04-14 12:58:32 +00:00
return nil, newError("failed to dial to dest: ", err).AtWarning().Base(err)
}
2016-06-14 22:30:11 +00:00
kcpSettings := streamSettings.ProtocolSettings.(*Config)
2016-10-02 21:43:58 +00:00
2016-12-08 15:27:41 +00:00
header, err := kcpSettings.GetPackerHeader()
2016-08-06 19:59:22 +00:00
if err != nil {
2017-04-09 11:30:46 +00:00
return nil, newError("failed to create packet header").Base(err)
2016-12-08 15:27:41 +00:00
}
security, err := kcpSettings.GetSecurity()
if err != nil {
2017-04-09 11:30:46 +00:00
return nil, newError("failed to create security").Base(err)
2016-08-06 19:59:22 +00:00
}
2017-12-03 20:29:27 +00:00
reader := &KCPPacketReader{
Header: header,
Security: security,
}
writer := &KCPPacketWriter{
Header: header,
Security: security,
Writer: rawConn,
}
2016-07-12 16:54:16 +00:00
conv := uint16(atomic.AddUint32(&globalConv, 1))
2017-12-14 22:24:40 +00:00
session := NewConnection(ConnMetadata{
LocalAddr: rawConn.LocalAddr(),
RemoteAddr: rawConn.RemoteAddr(),
Conversation: conv,
2017-12-03 20:29:27 +00:00
}, writer, rawConn, kcpSettings)
go fetchInput(ctx, rawConn, reader, session)
2016-06-17 14:51:41 +00:00
2017-11-23 22:46:46 +00:00
var iConn internet.Connection = session
2016-09-30 14:53:40 +00:00
2020-10-06 16:25:02 +00:00
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
iConn = tls.Client(iConn, config.GetTLSConfig(tls.WithDestination(dest)))
2016-09-30 14:53:40 +00:00
}
return iConn, nil
2016-06-14 20:54:08 +00:00
}
func init() {
common.Must(internet.RegisterTransportDialer(protocolName, DialKCP))
}