config_json.go 1.6 KB

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