2016-06-14 16:54:08 -04:00
|
|
|
package kcp
|
2016-06-11 05:30:38 -04:00
|
|
|
|
2016-08-06 15:59:22 -04:00
|
|
|
import (
|
2016-12-08 10:27:41 -05:00
|
|
|
"crypto/cipher"
|
2021-06-19 09:36:54 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/serial"
|
2020-10-11 07:22:46 -04:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
2016-08-06 15:59:22 -04:00
|
|
|
)
|
|
|
|
|
2018-08-06 07:48:35 -04:00
|
|
|
const protocolName = "mkcp"
|
|
|
|
|
2016-12-23 06:42:25 -05:00
|
|
|
// GetMTUValue returns the value of MTU settings.
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetMTUValue() uint32 {
|
|
|
|
if c == nil || c.Mtu == nil {
|
2016-09-21 15:08:05 -04:00
|
|
|
return 1350
|
|
|
|
}
|
2017-12-03 15:45:58 -05:00
|
|
|
return c.Mtu.Value
|
2016-09-21 15:08:05 -04:00
|
|
|
}
|
|
|
|
|
2016-12-23 06:42:25 -05:00
|
|
|
// GetTTIValue returns the value of TTI settings.
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetTTIValue() uint32 {
|
|
|
|
if c == nil || c.Tti == nil {
|
2016-09-21 15:08:05 -04:00
|
|
|
return 50
|
|
|
|
}
|
2017-12-03 15:45:58 -05:00
|
|
|
return c.Tti.Value
|
2016-09-21 15:08:05 -04:00
|
|
|
}
|
|
|
|
|
2016-12-23 06:42:25 -05:00
|
|
|
// GetUplinkCapacityValue returns the value of UplinkCapacity settings.
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetUplinkCapacityValue() uint32 {
|
|
|
|
if c == nil || c.UplinkCapacity == nil {
|
2016-09-21 15:08:05 -04:00
|
|
|
return 5
|
|
|
|
}
|
2017-12-03 15:45:58 -05:00
|
|
|
return c.UplinkCapacity.Value
|
2016-09-21 15:08:05 -04:00
|
|
|
}
|
|
|
|
|
2016-12-23 06:42:25 -05:00
|
|
|
// GetDownlinkCapacityValue returns the value of DownlinkCapacity settings.
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetDownlinkCapacityValue() uint32 {
|
|
|
|
if c == nil || c.DownlinkCapacity == nil {
|
2016-09-21 15:08:05 -04:00
|
|
|
return 20
|
|
|
|
}
|
2017-12-03 15:45:58 -05:00
|
|
|
return c.DownlinkCapacity.Value
|
2016-09-21 15:08:05 -04:00
|
|
|
}
|
|
|
|
|
2016-12-23 06:42:25 -05:00
|
|
|
// GetWriteBufferSize returns the size of WriterBuffer in bytes.
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetWriteBufferSize() uint32 {
|
|
|
|
if c == nil || c.WriteBuffer == nil {
|
2016-11-30 15:36:40 -05:00
|
|
|
return 2 * 1024 * 1024
|
2016-09-21 15:08:05 -04:00
|
|
|
}
|
2017-12-03 15:45:58 -05:00
|
|
|
return c.WriteBuffer.Size
|
2016-09-21 15:08:05 -04:00
|
|
|
}
|
|
|
|
|
2016-12-23 06:42:25 -05:00
|
|
|
// GetReadBufferSize returns the size of ReadBuffer in bytes.
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetReadBufferSize() uint32 {
|
|
|
|
if c == nil || c.ReadBuffer == nil {
|
2016-11-30 15:36:40 -05:00
|
|
|
return 2 * 1024 * 1024
|
2016-09-21 15:08:05 -04:00
|
|
|
}
|
2017-12-03 15:45:58 -05:00
|
|
|
return c.ReadBuffer.Size
|
2016-09-21 15:08:05 -04:00
|
|
|
}
|
|
|
|
|
2016-12-08 10:37:04 -05:00
|
|
|
// GetSecurity returns the security settings.
|
2020-06-08 20:36:40 -04:00
|
|
|
func (c *Config) GetSecurity() (cipher.AEAD, error) {
|
|
|
|
if c.Seed != nil {
|
|
|
|
return NewAEADAESGCMBasedOnSeed(c.Seed.Seed), nil
|
|
|
|
}
|
2016-12-08 10:27:41 -05:00
|
|
|
return NewSimpleAuthenticator(), nil
|
|
|
|
}
|
|
|
|
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetPackerHeader() (internet.PacketHeader, error) {
|
|
|
|
if c.HeaderConfig != nil {
|
2021-06-19 09:36:54 -04:00
|
|
|
rawConfig, err := serial.GetInstanceOf(c.HeaderConfig)
|
2016-10-16 08:22:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-12 16:47:10 -05:00
|
|
|
return internet.CreatePacketHeader(rawConfig)
|
2016-08-06 15:59:22 -04:00
|
|
|
}
|
2016-12-08 10:27:41 -05:00
|
|
|
return nil, nil
|
2016-08-06 15:59:22 -04:00
|
|
|
}
|
|
|
|
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetSendingInFlightSize() uint32 {
|
|
|
|
size := c.GetUplinkCapacityValue() * 1024 * 1024 / c.GetMTUValue() / (1000 / c.GetTTIValue())
|
2016-08-24 01:50:33 -04:00
|
|
|
if size < 8 {
|
2016-06-29 10:46:26 -04:00
|
|
|
size = 8
|
|
|
|
}
|
|
|
|
return size
|
2016-06-20 10:10:47 -04:00
|
|
|
}
|
|
|
|
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetSendingBufferSize() uint32 {
|
|
|
|
return c.GetWriteBufferSize() / c.GetMTUValue()
|
2016-07-02 05:19:32 -04:00
|
|
|
}
|
|
|
|
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetReceivingInFlightSize() uint32 {
|
|
|
|
size := c.GetDownlinkCapacityValue() * 1024 * 1024 / c.GetMTUValue() / (1000 / c.GetTTIValue())
|
2016-08-24 01:50:33 -04:00
|
|
|
if size < 8 {
|
2016-06-29 10:46:26 -04:00
|
|
|
size = 8
|
|
|
|
}
|
|
|
|
return size
|
2016-06-20 10:10:47 -04:00
|
|
|
}
|
|
|
|
|
2017-12-03 15:45:58 -05:00
|
|
|
func (c *Config) GetReceivingBufferSize() uint32 {
|
|
|
|
return c.GetReadBufferSize() / c.GetMTUValue()
|
2016-06-17 10:57:48 -04:00
|
|
|
}
|
2019-02-01 10:56:43 -05:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
|
|
|
|
return new(Config)
|
|
|
|
}))
|
|
|
|
}
|