v2fly/transport/internet/kcp/config.go

110 lines
2.5 KiB
Go
Raw Normal View History

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