config.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // +build !confonly
  2. package kcp
  3. import (
  4. "crypto/cipher"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/transport/internet"
  7. )
  8. const protocolName = "mkcp"
  9. // GetMTUValue returns the value of MTU settings.
  10. func (c *Config) GetMTUValue() uint32 {
  11. if c == nil || c.Mtu == nil {
  12. return 1350
  13. }
  14. return c.Mtu.Value
  15. }
  16. // GetTTIValue returns the value of TTI settings.
  17. func (c *Config) GetTTIValue() uint32 {
  18. if c == nil || c.Tti == nil {
  19. return 50
  20. }
  21. return c.Tti.Value
  22. }
  23. // GetUplinkCapacityValue returns the value of UplinkCapacity settings.
  24. func (c *Config) GetUplinkCapacityValue() uint32 {
  25. if c == nil || c.UplinkCapacity == nil {
  26. return 5
  27. }
  28. return c.UplinkCapacity.Value
  29. }
  30. // GetDownlinkCapacityValue returns the value of DownlinkCapacity settings.
  31. func (c *Config) GetDownlinkCapacityValue() uint32 {
  32. if c == nil || c.DownlinkCapacity == nil {
  33. return 20
  34. }
  35. return c.DownlinkCapacity.Value
  36. }
  37. // GetWriteBufferSize returns the size of WriterBuffer in bytes.
  38. func (c *Config) GetWriteBufferSize() uint32 {
  39. if c == nil || c.WriteBuffer == nil {
  40. return 2 * 1024 * 1024
  41. }
  42. return c.WriteBuffer.Size
  43. }
  44. // GetReadBufferSize returns the size of ReadBuffer in bytes.
  45. func (c *Config) GetReadBufferSize() uint32 {
  46. if c == nil || c.ReadBuffer == nil {
  47. return 2 * 1024 * 1024
  48. }
  49. return c.ReadBuffer.Size
  50. }
  51. // GetSecurity returns the security settings.
  52. func (*Config) GetSecurity() (cipher.AEAD, error) {
  53. return NewSimpleAuthenticator(), nil
  54. }
  55. func (c *Config) GetPackerHeader() (internet.PacketHeader, error) {
  56. if c.HeaderConfig != nil {
  57. rawConfig, err := c.HeaderConfig.GetInstance()
  58. if err != nil {
  59. return nil, err
  60. }
  61. return internet.CreatePacketHeader(rawConfig)
  62. }
  63. return nil, nil
  64. }
  65. func (c *Config) GetSendingInFlightSize() uint32 {
  66. size := c.GetUplinkCapacityValue() * 1024 * 1024 / c.GetMTUValue() / (1000 / c.GetTTIValue())
  67. if size < 8 {
  68. size = 8
  69. }
  70. return size
  71. }
  72. func (c *Config) GetSendingBufferSize() uint32 {
  73. return c.GetWriteBufferSize() / c.GetMTUValue()
  74. }
  75. func (c *Config) GetReceivingInFlightSize() uint32 {
  76. size := c.GetDownlinkCapacityValue() * 1024 * 1024 / c.GetMTUValue() / (1000 / c.GetTTIValue())
  77. if size < 8 {
  78. size = 8
  79. }
  80. return size
  81. }
  82. func (c *Config) GetReceivingBufferSize() uint32 {
  83. return c.GetReadBufferSize() / c.GetMTUValue()
  84. }
  85. func init() {
  86. common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
  87. return new(Config)
  88. }))
  89. }