v2ray-core/transport/internet/kcp/dialer.go

153 lines
3.3 KiB
Go
Raw Normal View History

2016-06-14 20:54:08 +00:00
package kcp
import (
"context"
2017-01-13 23:27:45 +00:00
"crypto/cipher"
2016-09-30 14:53:40 +00:00
"crypto/tls"
2016-06-14 22:30:11 +00:00
"net"
2016-11-27 07:58:31 +00:00
"sync"
2016-07-12 16:54:16 +00:00
"sync/atomic"
2016-12-08 15:27:41 +00:00
2017-02-10 15:42:24 +00:00
"v2ray.com/core/app/log"
2017-01-03 14:16:48 +00:00
"v2ray.com/core/common"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/dice"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
2016-09-30 14:53:40 +00:00
v2tls "v2ray.com/core/transport/internet/tls"
2016-06-14 20:54:08 +00:00
)
2016-07-12 16:54:16 +00:00
var (
2017-02-14 21:29:44 +00:00
globalConv = uint32(dice.RandomUint16())
2016-07-12 16:54:16 +00:00
)
2016-11-27 07:58:31 +00:00
type ClientConnection struct {
2016-12-08 15:27:41 +00:00
sync.RWMutex
2016-11-27 07:58:31 +00:00
net.Conn
2016-12-08 15:27:41 +00:00
input func([]Segment)
reader PacketReader
writer PacketWriter
}
2017-04-13 20:17:58 +00:00
func (c *ClientConnection) Overhead() int {
c.RLock()
defer c.RUnlock()
if c.writer == nil {
2016-12-08 15:27:41 +00:00
return 0
}
2017-04-13 20:17:58 +00:00
return c.writer.Overhead()
2016-12-08 15:27:41 +00:00
}
2017-04-13 20:17:58 +00:00
func (c *ClientConnection) Write(b []byte) (int, error) {
c.RLock()
defer c.RUnlock()
2016-12-08 15:27:41 +00:00
2017-04-13 20:17:58 +00:00
if c.writer == nil {
2016-12-08 15:27:41 +00:00
return len(b), nil
}
2017-04-13 20:17:58 +00:00
return c.writer.Write(b)
2016-11-27 07:58:31 +00:00
}
2017-04-13 20:17:58 +00:00
func (*ClientConnection) Read([]byte) (int, error) {
2016-11-27 07:58:31 +00:00
panic("KCP|ClientConnection: Read should not be called.")
}
2017-04-13 20:17:58 +00:00
func (c *ClientConnection) Close() error {
return c.Conn.Close()
2016-11-27 07:58:31 +00:00
}
2017-04-13 20:17:58 +00:00
func (c *ClientConnection) Reset(inputCallback func([]Segment)) {
c.Lock()
c.input = inputCallback
c.Unlock()
2016-12-08 15:27:41 +00:00
}
2017-04-13 20:17:58 +00:00
func (c *ClientConnection) ResetSecurity(header internet.PacketHeader, security cipher.AEAD) {
c.Lock()
if c.reader == nil {
c.reader = new(KCPPacketReader)
2016-12-08 15:27:41 +00:00
}
2017-04-13 20:17:58 +00:00
c.reader.(*KCPPacketReader).Header = header
c.reader.(*KCPPacketReader).Security = security
if c.writer == nil {
c.writer = new(KCPPacketWriter)
2016-12-08 15:27:41 +00:00
}
2017-04-13 20:17:58 +00:00
c.writer.(*KCPPacketWriter).Header = header
c.writer.(*KCPPacketWriter).Security = security
c.writer.(*KCPPacketWriter).Writer = c.Conn
2016-12-08 15:27:41 +00:00
2017-04-13 20:17:58 +00:00
c.Unlock()
2016-11-27 07:58:31 +00:00
}
2017-04-13 20:17:58 +00:00
func (c *ClientConnection) Run() {
2017-04-15 19:19:21 +00:00
payload := buf.New()
2016-11-27 07:58:31 +00:00
defer payload.Release()
for {
2017-04-13 20:17:58 +00:00
err := payload.Reset(buf.ReadFrom(c.Conn))
2016-11-27 07:58:31 +00:00
if err != nil {
payload.Release()
return
}
2017-04-13 20:17:58 +00:00
c.RLock()
if c.input != nil {
segments := c.reader.Read(payload.Bytes())
2016-12-08 15:27:41 +00:00
if len(segments) > 0 {
2017-04-13 20:17:58 +00:00
c.input(segments)
2016-12-08 15:27:41 +00:00
}
2016-11-27 07:58:31 +00:00
}
2017-04-13 20:17:58 +00:00
c.RUnlock()
2016-11-27 07:58:31 +00:00
}
}
func DialKCP(ctx context.Context, dest v2net.Destination) (internet.Connection, error) {
2016-09-20 09:53:05 +00:00
dest.Network = v2net.Network_UDP
2017-04-09 11:30:46 +00:00
log.Trace(newError("dialing mKCP to ", dest))
2016-11-27 07:58:31 +00:00
src := internet.DialerSourceFromContext(ctx)
rawConn, err := internet.DialSystem(ctx, src, dest)
if err != nil {
2017-04-14 12:58:32 +00:00
return nil, newError("failed to dial to dest: ", err).AtWarning().Base(err)
}
conn := &ClientConnection{
Conn: rawConn,
2016-06-14 20:54:08 +00:00
}
go conn.Run()
2016-06-14 22:30:11 +00:00
kcpSettings := internet.TransportSettingsFromContext(ctx).(*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
}
conn.ResetSecurity(header, security)
2016-07-12 16:54:16 +00:00
conv := uint16(atomic.AddUint32(&globalConv, 1))
session := NewConnection(conv, conn, kcpSettings)
2016-06-17 14:51:41 +00:00
2016-09-30 14:53:40 +00:00
var iConn internet.Connection
iConn = session
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
2016-10-16 12:22:21 +00:00
switch securitySettings := securitySettings.(type) {
case *v2tls.Config:
config := securitySettings.GetTLSConfig()
if dest.Address.Family().IsDomain() {
config.ServerName = dest.Address.Domain()
}
2016-12-20 12:03:20 +00:00
tlsConn := tls.Client(iConn, config)
iConn = tlsConn
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(internet.TransportProtocol_MKCP, DialKCP))
2016-06-14 20:54:08 +00:00
}