You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/kcp/dialer.go

178 lines
4.0 KiB

package kcp
import (
"crypto/tls"
"net"
"sync"
9 years ago
"sync/atomic"
"crypto/cipher"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/internal"
v2tls "v2ray.com/core/transport/internet/tls"
)
9 years ago
var (
globalConv = uint32(dice.Roll(65536))
globalPool = internal.NewConnectionPool()
9 years ago
)
type ClientConnection struct {
sync.RWMutex
net.Conn
id internal.ConnectionId
input func([]Segment)
reader PacketReader
writer PacketWriter
}
func (o *ClientConnection) Overhead() int {
o.RLock()
defer o.RUnlock()
if o.writer == nil {
return 0
}
return o.writer.Overhead()
}
func (o *ClientConnection) Write(b []byte) (int, error) {
o.RLock()
defer o.RUnlock()
if o.writer == nil {
return len(b), nil
}
return o.writer.Write(b)
}
func (o *ClientConnection) Read([]byte) (int, error) {
panic("KCP|ClientConnection: Read should not be called.")
}
func (o *ClientConnection) Id() internal.ConnectionId {
return o.id
}
func (o *ClientConnection) Close() error {
return o.Conn.Close()
}
func (o *ClientConnection) Reset(inputCallback func([]Segment)) {
o.Lock()
o.input = inputCallback
o.Unlock()
}
func (o *ClientConnection) ResetSecurity(header internet.PacketHeader, security cipher.AEAD) {
o.Lock()
if o.reader == nil {
o.reader = new(KCPPacketReader)
}
o.reader.(*KCPPacketReader).Header = header
o.reader.(*KCPPacketReader).Security = security
if o.writer == nil {
o.writer = new(KCPPacketWriter)
}
o.writer.(*KCPPacketWriter).Header = header
o.writer.(*KCPPacketWriter).Security = security
o.writer.(*KCPPacketWriter).Writer = o.Conn
o.Unlock()
}
func (o *ClientConnection) Run() {
payload := alloc.NewSmallBuffer()
defer payload.Release()
for {
payload.Clear()
_, err := payload.FillFrom(o.Conn)
if err != nil {
payload.Release()
return
}
o.RLock()
if o.input != nil {
segments := o.reader.Read(payload.Bytes())
if len(segments) > 0 {
o.input(segments)
}
}
o.RUnlock()
}
}
func DialKCP(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
dest.Network = v2net.Network_UDP
log.Info("KCP|Dialer: Dialing KCP to ", dest)
id := internal.NewConnectionId(src, dest)
conn := globalPool.Get(id)
if conn == nil {
rawConn, err := internet.DialToDest(src, dest)
if err != nil {
log.Error("KCP|Dialer: Failed to dial to dest: ", err)
return nil, err
}
c := &ClientConnection{
Conn: rawConn,
id: id,
}
go c.Run()
conn = c
}
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
if err != nil {
log.Error("KCP|Dialer: Failed to get KCP settings: ", err)
return nil, err
}
kcpSettings := networkSettings.(*Config)
clientConn := conn.(*ClientConnection)
header, err := kcpSettings.GetPackerHeader()
if err != nil {
return nil, errors.Base(err).Message("KCP|Dialer: Failed to create packet header.")
}
security, err := kcpSettings.GetSecurity()
if err != nil {
return nil, errors.Base(err).Message("KCP|Dialer: Failed to create security.")
}
clientConn.ResetSecurity(header, security)
9 years ago
conv := uint16(atomic.AddUint32(&globalConv, 1))
session := NewConnection(conv, clientConn, globalPool, kcpSettings)
var iConn internet.Connection
iConn = session
if options.Stream != nil && options.Stream.HasSecuritySettings() {
securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
if err != nil {
log.Error("KCP|Dialer: Failed to get security settings: ", err)
return nil, err
}
switch securitySettings := securitySettings.(type) {
case *v2tls.Config:
config := securitySettings.GetTLSConfig()
if dest.Address.Family().IsDomain() {
config.ServerName = dest.Address.Domain()
}
tlsConn := tls.Client(conn, config)
iConn = v2tls.NewConnection(tlsConn)
}
}
return iConn, nil
}
func init() {
internet.KCPDialer = DialKCP
}