github.com/v2fly/v2ray-core/v4@v4.45.2/transport/internet/kcp/dialer.go (about) 1 //go:build !confonly 2 // +build !confonly 3 4 package kcp 5 6 import ( 7 "context" 8 "io" 9 "sync/atomic" 10 11 "github.com/v2fly/v2ray-core/v4/common" 12 "github.com/v2fly/v2ray-core/v4/common/buf" 13 "github.com/v2fly/v2ray-core/v4/common/dice" 14 "github.com/v2fly/v2ray-core/v4/common/net" 15 "github.com/v2fly/v2ray-core/v4/transport/internet" 16 "github.com/v2fly/v2ray-core/v4/transport/internet/tls" 17 ) 18 19 var globalConv = uint32(dice.RollUint16()) 20 21 func fetchInput(_ context.Context, input io.Reader, reader PacketReader, conn *Connection) { 22 cache := make(chan *buf.Buffer, 1024) 23 go func() { 24 for { 25 payload := buf.New() 26 if _, err := payload.ReadFrom(input); err != nil { 27 payload.Release() 28 close(cache) 29 return 30 } 31 select { 32 case cache <- payload: 33 default: 34 payload.Release() 35 } 36 } 37 }() 38 39 for payload := range cache { 40 segments := reader.Read(payload.Bytes()) 41 payload.Release() 42 if len(segments) > 0 { 43 conn.Input(segments) 44 } 45 } 46 } 47 48 // DialKCP dials a new KCP connections to the specific destination. 49 func DialKCP(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) { 50 dest.Network = net.Network_UDP 51 newError("dialing mKCP to ", dest).WriteToLog() 52 53 rawConn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings) 54 if err != nil { 55 return nil, newError("failed to dial to dest: ", err).AtWarning().Base(err) 56 } 57 58 kcpSettings := streamSettings.ProtocolSettings.(*Config) 59 60 header, err := kcpSettings.GetPackerHeader() 61 if err != nil { 62 return nil, newError("failed to create packet header").Base(err) 63 } 64 security, err := kcpSettings.GetSecurity() 65 if err != nil { 66 return nil, newError("failed to create security").Base(err) 67 } 68 reader := &KCPPacketReader{ 69 Header: header, 70 Security: security, 71 } 72 writer := &KCPPacketWriter{ 73 Header: header, 74 Security: security, 75 Writer: rawConn, 76 } 77 78 conv := uint16(atomic.AddUint32(&globalConv, 1)) 79 session := NewConnection(ConnMetadata{ 80 LocalAddr: rawConn.LocalAddr(), 81 RemoteAddr: rawConn.RemoteAddr(), 82 Conversation: conv, 83 }, writer, rawConn, kcpSettings) 84 85 go fetchInput(ctx, rawConn, reader, session) 86 87 var iConn internet.Connection = session 88 89 if config := tls.ConfigFromStreamSettings(streamSettings); config != nil { 90 iConn = tls.Client(iConn, config.GetTLSConfig(tls.WithDestination(dest))) 91 } 92 93 return iConn, nil 94 } 95 96 func init() { 97 common.Must(internet.RegisterTransportDialer(protocolName, DialKCP)) 98 }