config_json.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // +build json
  2. package blackhole
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "github.com/v2ray/v2ray-core/common/loader"
  7. "github.com/v2ray/v2ray-core/proxy/registry"
  8. )
  9. func (this *Config) UnmarshalJSON(data []byte) error {
  10. type JSONConfig struct {
  11. Response json.RawMessage `json:"response"`
  12. }
  13. jsonConfig := new(JSONConfig)
  14. if err := json.Unmarshal(data, jsonConfig); err != nil {
  15. return errors.New("Blackhole: Failed to parse config: " + err.Error())
  16. }
  17. this.Response = new(NoneResponse)
  18. if jsonConfig.Response != nil {
  19. loader := loader.NewJSONConfigLoader("type", "")
  20. loader.RegisterCreator("none", func() interface{} { return new(NoneResponse) })
  21. loader.RegisterCreator("http", func() interface{} { return new(HTTPResponse) })
  22. response, _, err := loader.Load(jsonConfig.Response)
  23. if err != nil {
  24. return errors.New("Blackhole: Failed to parse response config: " + err.Error())
  25. }
  26. this.Response = response.(Response)
  27. }
  28. return nil
  29. }
  30. func init() {
  31. registry.RegisterOutboundConfig("blackhole", func() interface{} { return new(Config) })
  32. }