2016-06-14 16:54:08 -04:00
|
|
|
package kcp
|
|
|
|
|
|
|
|
import (
|
2017-01-26 14:46:44 -05:00
|
|
|
"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
|
|
|
|
2019-02-01 10:56:43 -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"
|
2017-08-29 06:56:57 -04:00
|
|
|
"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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
func DialKCP(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
|
2017-08-29 06:56:57 -04:00
|
|
|
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
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
rawConn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
|
2017-04-07 15:54:40 -04:00
|
|
|
if err != nil {
|
2017-04-14 08:58:32 -04:00
|
|
|
return nil, newError("failed to dial to dest: ", err).AtWarning().Base(err)
|
2017-04-07 15:54:40 -04:00
|
|
|
}
|
2016-06-14 18:30:11 -04:00
|
|
|
|
2018-11-21 08:54:40 -05: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
|
|
|
|
2018-11-21 08:54:40 -05: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
|
|
|
}
|
2019-02-01 10:56:43 -05:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterTransportDialer(protocolName, DialKCP))
|
|
|
|
}
|