config_json.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // +build json
  2. package kcp
  3. import (
  4. "encoding/json"
  5. "github.com/v2ray/v2ray-core/common"
  6. "github.com/v2ray/v2ray-core/common/log"
  7. "github.com/v2ray/v2ray-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. this.HeaderType = name
  72. this.HeaderConfig = config
  73. }
  74. return nil
  75. }