config_json.go 996 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // +build json
  2. package transport
  3. import (
  4. "encoding/json"
  5. "github.com/v2ray/v2ray-core/common/log"
  6. "github.com/v2ray/v2ray-core/transport/hub/kcpv"
  7. )
  8. func (this *Config) UnmarshalJSON(data []byte) error {
  9. type JsonConfig struct {
  10. ConnectionReuse bool `json:"connectionReuse"`
  11. EnableKcp bool `json:"EnableKCP,omitempty"`
  12. KcpConfig *kcpv.Config `json:"KcpConfig,omitempty"`
  13. }
  14. jsonConfig := &JsonConfig{
  15. ConnectionReuse: true,
  16. EnableKcp: false,
  17. }
  18. if err := json.Unmarshal(data, jsonConfig); err != nil {
  19. return err
  20. }
  21. this.ConnectionReuse = jsonConfig.ConnectionReuse
  22. this.enableKcp = jsonConfig.EnableKcp
  23. if jsonConfig.KcpConfig != nil {
  24. this.kcpConfig = jsonConfig.KcpConfig
  25. if jsonConfig.KcpConfig.AdvancedConfigs == nil {
  26. jsonConfig.KcpConfig.AdvancedConfigs = kcpv.DefaultAdvancedConfigs
  27. }
  28. } else {
  29. if jsonConfig.EnableKcp {
  30. log.Error("transport: You have enabled KCP but no configure is given")
  31. }
  32. }
  33. return nil
  34. }