config_json.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // +build json
  2. package http
  3. import (
  4. "encoding/json"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. "github.com/v2ray/v2ray-core/proxy/internal/config"
  7. )
  8. func (this *TlsConfig) UnmarshalJSON(data []byte) error {
  9. type JsonConfig struct {
  10. Enabled bool
  11. CertFile string
  12. KeyFile string
  13. }
  14. jsonConfig := new(JsonConfig)
  15. if err := json.Unmarshal(data, jsonConfig); err != nil {
  16. return err
  17. }
  18. this.Enabled = jsonConfig.Enabled
  19. this.CertFile = jsonConfig.CertFile
  20. this.KeyFile = jsonConfig.KeyFile
  21. return nil
  22. }
  23. func (this *Config) UnmarshalJSON(data []byte) error {
  24. type JsonConfig struct {
  25. Hosts []v2net.AddressJson `json:"ownHosts"`
  26. Tls *TlsConfig `json:"tls"`
  27. }
  28. jsonConfig := new(JsonConfig)
  29. if err := json.Unmarshal(data, jsonConfig); err != nil {
  30. return err
  31. }
  32. this.OwnHosts = make([]v2net.Address, len(jsonConfig.Hosts), len(jsonConfig.Hosts)+1)
  33. for idx, host := range jsonConfig.Hosts {
  34. this.OwnHosts[idx] = host.Address
  35. }
  36. v2rayHost := v2net.DomainAddress("local.v2ray.com")
  37. if !this.IsOwnHost(v2rayHost) {
  38. this.OwnHosts = append(this.OwnHosts, v2rayHost)
  39. }
  40. this.TlsConfig = jsonConfig.Tls
  41. return nil
  42. }
  43. func init() {
  44. config.RegisterInboundConfig("http",
  45. func(data []byte) (interface{}, error) {
  46. rawConfig := new(Config)
  47. err := json.Unmarshal(data, rawConfig)
  48. return rawConfig, err
  49. })
  50. }