config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package kcp
  2. import (
  3. "crypto/cipher"
  4. "github.com/v2fly/v2ray-core/v4/common"
  5. "github.com/v2fly/v2ray-core/v4/common/serial"
  6. "github.com/v2fly/v2ray-core/v4/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 (c *Config) GetSecurity() (cipher.AEAD, error) {
  53. if c.Seed != nil {
  54. return NewAEADAESGCMBasedOnSeed(c.Seed.Seed), nil
  55. }
  56. return NewSimpleAuthenticator(), nil
  57. }
  58. func (c *Config) GetPackerHeader() (internet.PacketHeader, error) {
  59. if c.HeaderConfig != nil {
  60. rawConfig, err := serial.GetInstanceOf(c.HeaderConfig)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return internet.CreatePacketHeader(rawConfig)
  65. }
  66. return nil, nil
  67. }
  68. func (c *Config) GetSendingInFlightSize() uint32 {
  69. size := c.GetUplinkCapacityValue() * 1024 * 1024 / c.GetMTUValue() / (1000 / c.GetTTIValue())
  70. if size < 8 {
  71. size = 8
  72. }
  73. return size
  74. }
  75. func (c *Config) GetSendingBufferSize() uint32 {
  76. return c.GetWriteBufferSize() / c.GetMTUValue()
  77. }
  78. func (c *Config) GetReceivingInFlightSize() uint32 {
  79. size := c.GetDownlinkCapacityValue() * 1024 * 1024 / c.GetMTUValue() / (1000 / c.GetTTIValue())
  80. if size < 8 {
  81. size = 8
  82. }
  83. return size
  84. }
  85. func (c *Config) GetReceivingBufferSize() uint32 {
  86. return c.GetReadBufferSize() / c.GetMTUValue()
  87. }
  88. func init() {
  89. common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
  90. return new(Config)
  91. }))
  92. }