config_json.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // +build json
  2. package kcp
  3. import (
  4. "encoding/json"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/log"
  7. "v2ray.com/core/transport/internet"
  8. )
  9. func (this *Config) UnmarshalJSON(data []byte) error {
  10. type JSONConfig struct {
  11. Mtu *uint32 `json:"mtu"`
  12. Tti *uint32 `json:"tti"`
  13. UpCap *uint32 `json:"uplinkCapacity"`
  14. DownCap *uint32 `json:"downlinkCapacity"`
  15. Congestion *bool `json:"congestion"`
  16. ReadBufferSize *uint32 `json:"readBufferSize"`
  17. WriteBufferSize *uint32 `json:"writeBufferSize"`
  18. HeaderConfig json.RawMessage `json:"header"`
  19. }
  20. jsonConfig := new(JSONConfig)
  21. if err := json.Unmarshal(data, &jsonConfig); err != nil {
  22. return err
  23. }
  24. if jsonConfig.Mtu != nil {
  25. mtu := *jsonConfig.Mtu
  26. if mtu < 576 || mtu > 1460 {
  27. log.Error("KCP|Config: Invalid MTU size: ", mtu)
  28. return common.ErrBadConfiguration
  29. }
  30. this.Mtu = mtu
  31. }
  32. if jsonConfig.Tti != nil {
  33. tti := *jsonConfig.Tti
  34. if tti < 10 || tti > 100 {
  35. log.Error("KCP|Config: Invalid TTI: ", tti)
  36. return common.ErrBadConfiguration
  37. }
  38. this.Tti = tti
  39. }
  40. if jsonConfig.UpCap != nil {
  41. this.UplinkCapacity = *jsonConfig.UpCap
  42. }
  43. if jsonConfig.DownCap != nil {
  44. this.DownlinkCapacity = *jsonConfig.DownCap
  45. }
  46. if jsonConfig.Congestion != nil {
  47. this.Congestion = *jsonConfig.Congestion
  48. }
  49. if jsonConfig.ReadBufferSize != nil {
  50. size := *jsonConfig.ReadBufferSize
  51. if size > 0 {
  52. this.ReadBuffer = size * 1024 * 1024
  53. } else {
  54. this.ReadBuffer = 512 * 1024
  55. }
  56. }
  57. if jsonConfig.WriteBufferSize != nil {
  58. size := *jsonConfig.WriteBufferSize
  59. if size > 0 {
  60. this.WriteBuffer = size * 1024 * 1024
  61. } else {
  62. this.WriteBuffer = 512 * 1024
  63. }
  64. }
  65. if len(jsonConfig.HeaderConfig) > 0 {
  66. name, config, err := internet.CreateAuthenticatorConfig(jsonConfig.HeaderConfig)
  67. if err != nil {
  68. log.Error("KCP|Config: Failed to parse header config: ", err)
  69. return err
  70. }
  71. authConfig, err := internet.NewAuthenticatorConfig(name, config)
  72. if err != nil {
  73. log.Error("KCP:Config: Failed to create header config: ", err)
  74. return err
  75. }
  76. this.HeaderConfig = authConfig
  77. }
  78. return nil
  79. }