config_json.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // +build json
  2. package http
  3. import (
  4. "crypto/tls"
  5. "encoding/json"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. "github.com/v2ray/v2ray-core/proxy/internal/config"
  8. )
  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. }
  26. func (this *TlsConfig) UnmarshalJSON(data []byte) error {
  27. type JsonConfig struct {
  28. Enabled bool `json:"enable"`
  29. Certs []*CertificateConfig `json:"certs"`
  30. }
  31. jsonConfig := new(JsonConfig)
  32. if err := json.Unmarshal(data, jsonConfig); err != nil {
  33. return err
  34. }
  35. this.Enabled = jsonConfig.Enabled
  36. this.Certs = jsonConfig.Certs
  37. return nil
  38. }
  39. func (this *Config) UnmarshalJSON(data []byte) error {
  40. type JsonConfig struct {
  41. Hosts []v2net.AddressJson `json:"ownHosts"`
  42. Tls *TlsConfig `json:"tls"`
  43. }
  44. jsonConfig := new(JsonConfig)
  45. if err := json.Unmarshal(data, jsonConfig); err != nil {
  46. return err
  47. }
  48. this.OwnHosts = make([]v2net.Address, len(jsonConfig.Hosts), len(jsonConfig.Hosts)+1)
  49. for idx, host := range jsonConfig.Hosts {
  50. this.OwnHosts[idx] = host.Address
  51. }
  52. v2rayHost := v2net.DomainAddress("local.v2ray.com")
  53. if !this.IsOwnHost(v2rayHost) {
  54. this.OwnHosts = append(this.OwnHosts, v2rayHost)
  55. }
  56. this.TlsConfig = jsonConfig.Tls
  57. return nil
  58. }
  59. func init() {
  60. config.RegisterInboundConfig("http",
  61. func(data []byte) (interface{}, error) {
  62. rawConfig := new(Config)
  63. err := json.Unmarshal(data, rawConfig)
  64. return rawConfig, err
  65. })
  66. }