config.go 2.4 KB

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