config.go 2.5 KB

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