connection_json.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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:"certificateFile"`
  13. KeyFile string `json:"keyFile"`
  14. }
  15. type JSONConfig struct {
  16. Insecure bool `json:"allowInsecure"`
  17. Certs []*JSONCertConfig `json:"certificates"`
  18. }
  19. jsonConfig := new(JSONConfig)
  20. if err := json.Unmarshal(data, jsonConfig); err != nil {
  21. return err
  22. }
  23. this.Certs = make([]tls.Certificate, len(jsonConfig.Certs))
  24. for idx, certConf := range jsonConfig.Certs {
  25. cert, err := tls.LoadX509KeyPair(certConf.CertFile, certConf.KeyFile)
  26. if err != nil {
  27. return errors.New("Internet|TLS: Failed to load certificate file: " + err.Error())
  28. }
  29. this.Certs[idx] = cert
  30. }
  31. this.AllowInsecure = jsonConfig.Insecure
  32. return nil
  33. }
  34. func (this *StreamSettings) UnmarshalJSON(data []byte) error {
  35. type JSONConfig struct {
  36. Network v2net.NetworkList `json:"network"`
  37. Security string `json:"security"`
  38. TLSSettings *TLSSettings `json:"tlsSettings"`
  39. }
  40. this.Type = StreamConnectionTypeRawTCP
  41. jsonConfig := new(JSONConfig)
  42. if err := json.Unmarshal(data, jsonConfig); err != nil {
  43. return err
  44. }
  45. if jsonConfig.Network.HasNetwork(v2net.KCPNetwork) {
  46. this.Type |= StreamConnectionTypeKCP
  47. }
  48. if jsonConfig.Network.HasNetwork(v2net.WSNetwork) {
  49. this.Type |= StreamConnectionTypeWebSocket
  50. }
  51. if jsonConfig.Network.HasNetwork(v2net.TCPNetwork) {
  52. this.Type |= StreamConnectionTypeTCP
  53. }
  54. this.Security = StreamSecurityTypeNone
  55. if strings.ToLower(jsonConfig.Security) == "tls" {
  56. this.Security = StreamSecurityTypeTLS
  57. }
  58. if jsonConfig.TLSSettings != nil {
  59. this.TLSSettings = jsonConfig.TLSSettings
  60. }
  61. return nil
  62. }