config.go 2.5 KB

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