config.go 2.5 KB

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