connection_json.go 902 B

123456789101112131415161718192021222324252627282930313233343536
  1. // +build json
  2. package internet
  3. import (
  4. "encoding/json"
  5. "strings"
  6. "v2ray.com/core/common/loader"
  7. v2net "v2ray.com/core/common/net"
  8. v2tls "v2ray.com/core/transport/internet/tls"
  9. )
  10. func (this *StreamConfig) UnmarshalJSON(data []byte) error {
  11. type JSONConfig struct {
  12. Network *v2net.Network `json:"network"`
  13. Security string `json:"security"`
  14. TLSSettings *v2tls.Config `json:"tlsSettings"`
  15. }
  16. this.Network = v2net.Network_RawTCP
  17. jsonConfig := new(JSONConfig)
  18. if err := json.Unmarshal(data, jsonConfig); err != nil {
  19. return err
  20. }
  21. if jsonConfig.Network != nil {
  22. this.Network = *jsonConfig.Network
  23. }
  24. if strings.ToLower(jsonConfig.Security) == "tls" {
  25. tlsSettings := jsonConfig.TLSSettings
  26. if tlsSettings == nil {
  27. tlsSettings = &v2tls.Config{}
  28. }
  29. this.SecuritySettings = append(this.SecuritySettings, loader.NewTypedSettings(tlsSettings))
  30. }
  31. return nil
  32. }