config.go 2.4 KB

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