connection_json.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // +build json
  2. package internet
  3. import (
  4. "encoding/json"
  5. "strings"
  6. "errors"
  7. "github.com/golang/protobuf/ptypes"
  8. v2net "v2ray.com/core/common/net"
  9. v2tls "v2ray.com/core/transport/internet/tls"
  10. )
  11. func (this *StreamConfig) UnmarshalJSON(data []byte) error {
  12. type JSONConfig struct {
  13. Network *v2net.Network `json:"network"`
  14. Security string `json:"security"`
  15. TLSSettings *v2tls.Config `json:"tlsSettings"`
  16. }
  17. this.Network = v2net.Network_RawTCP
  18. jsonConfig := new(JSONConfig)
  19. if err := json.Unmarshal(data, jsonConfig); err != nil {
  20. return err
  21. }
  22. if jsonConfig.Network != nil {
  23. this.Network = *jsonConfig.Network
  24. }
  25. this.SecurityType = SecurityType_None
  26. if strings.ToLower(jsonConfig.Security) == "tls" {
  27. this.SecurityType = SecurityType_TLS
  28. }
  29. if jsonConfig.TLSSettings != nil {
  30. anyTLSSettings, err := ptypes.MarshalAny(jsonConfig.TLSSettings)
  31. if err != nil {
  32. return errors.New("Internet: Failed to parse TLS settings: " + err.Error())
  33. }
  34. this.SecuritySettings = append(this.SecuritySettings, &SecuritySettings{
  35. Type: SecurityType_TLS,
  36. Settings: anyTLSSettings,
  37. })
  38. }
  39. return nil
  40. }