config_json.go 1.8 KB

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