config_json.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // +build json
  2. package http
  3. import (
  4. "crypto/tls"
  5. "encoding/json"
  6. "github.com/v2ray/v2ray-core/proxy/internal/config"
  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. config.RegisterInboundConfig("http",
  55. func(data []byte) (interface{}, error) {
  56. rawConfig := new(Config)
  57. err := json.Unmarshal(data, rawConfig)
  58. return rawConfig, err
  59. })
  60. }