v2ray-core/tools/conf/blackhole.go

52 lines
1.2 KiB
Go
Raw Normal View History

2016-10-17 12:35:13 +00:00
package conf
import (
"encoding/json"
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
2016-10-17 12:35:13 +00:00
"v2ray.com/core/common/loader"
"v2ray.com/core/proxy/blackhole"
)
type NoneResponse struct{}
func (*NoneResponse) Build() (*loader.TypedSettings, error) {
return loader.NewTypedSettings(new(blackhole.NoneResponse)), nil
}
type HttpResponse struct{}
func (*HttpResponse) Build() (*loader.TypedSettings, error) {
return loader.NewTypedSettings(new(blackhole.HTTPResponse)), nil
}
type BlackholeConfig struct {
Response json.RawMessage `json:"response"`
}
2016-11-27 20:39:09 +00:00
func (v *BlackholeConfig) Build() (*loader.TypedSettings, error) {
2016-10-17 12:35:13 +00:00
config := new(blackhole.Config)
2016-11-27 20:39:09 +00:00
if v.Response != nil {
response, _, err := configLoader.Load(v.Response)
2016-10-17 12:35:13 +00:00
if err != nil {
2016-12-04 08:43:33 +00:00
return nil, errors.Base(err).Message("Blackhole: Failed to parse response config.")
2016-10-17 12:35:13 +00:00
}
responseSettings, err := response.(Buildable).Build()
if err != nil {
return nil, err
}
config.Response = responseSettings
}
return loader.NewTypedSettings(config), nil
}
var (
configLoader = NewJSONConfigLoader(
ConfigCreatorCache{
"none": func() interface{} { return new(NoneResponse) },
"http": func() interface{} { return new(HttpResponse) },
},
"type",
"")
)