config_json.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // +build json
  2. package blackhole
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. "v2ray.com/core/common/loader"
  8. "v2ray.com/core/proxy/registry"
  9. )
  10. func (this *Config) UnmarshalJSON(data []byte) error {
  11. type JSONConfig struct {
  12. Response json.RawMessage `json:"response"`
  13. }
  14. jsonConfig := new(JSONConfig)
  15. if err := json.Unmarshal(data, jsonConfig); err != nil {
  16. return errors.New("Blackhole: Failed to parse config: " + err.Error())
  17. }
  18. if jsonConfig.Response != nil {
  19. response, rType, err := configLoader.Load(jsonConfig.Response)
  20. if err != nil {
  21. return errors.New("Blackhole: Failed to parse response config: " + err.Error())
  22. }
  23. this.Response = new(Response)
  24. switch rType {
  25. case strings.ToLower(Response_Type_name[int32(Response_None)]):
  26. this.Response.Type = Response_None
  27. case strings.ToLower(Response_Type_name[int32(Response_HTTP)]):
  28. this.Response.Type = Response_HTTP
  29. }
  30. this.Response.Settings = response.(ResponseConfig).AsAny()
  31. }
  32. return nil
  33. }
  34. var (
  35. configLoader = loader.NewJSONConfigLoader(cache, "type", "")
  36. )
  37. func init() {
  38. registry.RegisterOutboundConfig("blackhole", func() interface{} { return new(Config) })
  39. }