config_json.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // +build json
  2. package http
  3. import (
  4. "crypto/tls"
  5. "encoding/json"
  6. "github.com/v2ray/v2ray-core/proxy/internal"
  7. )
  8. // UnmarshalJSON implements json.Unmarshaler
  9. func (this *CertificateConfig) UnmarshalJSON(data []byte) error {
  10. type JsonConfig struct {
  11. Domain string `json:"domain"`
  12. CertFile string `json:"cert"`
  13. KeyFile string `json:"key"`
  14. }
  15. jsonConfig := new(JsonConfig)
  16. if err := json.Unmarshal(data, jsonConfig); err != nil {
  17. return err
  18. }
  19. cert, err := tls.LoadX509KeyPair(jsonConfig.CertFile, jsonConfig.KeyFile)
  20. if err != nil {
  21. return err
  22. }
  23. this.Domain = jsonConfig.Domain
  24. this.Certificate = cert
  25. return nil
  26. }
  27. // UnmarshalJSON implements json.Unmarshaler
  28. func (this *TLSConfig) UnmarshalJSON(data []byte) error {
  29. type JsonConfig struct {
  30. Enabled bool `json:"enable"`
  31. Certs []*CertificateConfig `json:"certs"`
  32. }
  33. jsonConfig := new(JsonConfig)
  34. if err := json.Unmarshal(data, jsonConfig); err != nil {
  35. return err
  36. }
  37. this.Enabled = jsonConfig.Enabled
  38. this.Certs = jsonConfig.Certs
  39. return nil
  40. }
  41. // UnmarshalJSON implements json.Unmarshaler
  42. func (this *Config) UnmarshalJSON(data []byte) error {
  43. type JsonConfig struct {
  44. Tls *TLSConfig `json:"tls"`
  45. }
  46. jsonConfig := new(JsonConfig)
  47. if err := json.Unmarshal(data, jsonConfig); err != nil {
  48. return err
  49. }
  50. this.TLSConfig = jsonConfig.Tls
  51. return nil
  52. }
  53. func init() {
  54. internal.RegisterInboundConfig("http", func() interface{} { return new(Config) })
  55. }