config.go 2.2 KB

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