connection_json.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // +build json
  2. package internet
  3. import (
  4. "crypto/tls"
  5. "encoding/json"
  6. "errors"
  7. "strings"
  8. v2net "github.com/v2ray/v2ray-core/common/net"
  9. )
  10. func (this *TLSSettings) UnmarshalJSON(data []byte) error {
  11. type JSONCertConfig struct {
  12. CertFile string `json:"certFile"`
  13. KeyFile string `json:"keyFile"`
  14. }
  15. type JSONConfig struct {
  16. Certs []*JSONCertConfig `json:"certs"`
  17. }
  18. jsonConfig := new(JSONConfig)
  19. if err := json.Unmarshal(data, jsonConfig); err != nil {
  20. return err
  21. }
  22. this.Certs = make([]tls.Certificate, len(jsonConfig.Certs))
  23. for idx, certConf := range jsonConfig.Certs {
  24. cert, err := tls.LoadX509KeyPair(certConf.CertFile, certConf.KeyFile)
  25. if err != nil {
  26. return errors.New("Internet|TLS: Failed to load certificate file: " + err.Error())
  27. }
  28. this.Certs[idx] = cert
  29. }
  30. return nil
  31. }
  32. func (this *StreamSettings) UnmarshalJSON(data []byte) error {
  33. type JSONConfig struct {
  34. Network v2net.NetworkList `json:"network"`
  35. Security string `json:"security"`
  36. TLSSettings *TLSSettings `json:"tlsSettings"`
  37. }
  38. this.Type = StreamConnectionTypeRawTCP
  39. jsonConfig := new(JSONConfig)
  40. if err := json.Unmarshal(data, jsonConfig); err != nil {
  41. return err
  42. }
  43. if jsonConfig.Network.HasNetwork(v2net.KCPNetwork) {
  44. this.Type |= StreamConnectionTypeKCP
  45. }
  46. if jsonConfig.Network.HasNetwork(v2net.TCPNetwork) {
  47. this.Type |= StreamConnectionTypeTCP
  48. }
  49. this.Security = StreamSecurityTypeNone
  50. if strings.ToLower(jsonConfig.Security) == "tls" {
  51. this.Security = StreamSecurityTypeTLS
  52. }
  53. if jsonConfig.TLSSettings != nil {
  54. this.TLSSettings = jsonConfig.TLSSettings
  55. }
  56. return nil
  57. }