2016-01-15 11:43:06 +00:00
|
|
|
// +build json
|
|
|
|
|
|
|
|
package blackhole
|
|
|
|
|
|
|
|
import (
|
2016-06-10 21:01:17 +00:00
|
|
|
"encoding/json"
|
2016-06-11 20:52:37 +00:00
|
|
|
"errors"
|
2016-06-10 21:01:17 +00:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/loader"
|
2016-08-17 21:30:15 +00:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/registry"
|
2016-01-15 11:43:06 +00:00
|
|
|
)
|
|
|
|
|
2016-06-10 20:26:39 +00:00
|
|
|
func (this *Config) UnmarshalJSON(data []byte) error {
|
2016-06-10 21:01:17 +00:00
|
|
|
type JSONConfig struct {
|
|
|
|
Response json.RawMessage `json:"response"`
|
|
|
|
}
|
|
|
|
jsonConfig := new(JSONConfig)
|
|
|
|
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
2016-06-11 20:52:37 +00:00
|
|
|
return errors.New("Blackhole: Failed to parse config: " + err.Error())
|
2016-06-10 21:01:17 +00:00
|
|
|
}
|
2016-06-10 21:09:14 +00:00
|
|
|
|
|
|
|
this.Response = new(NoneResponse)
|
2016-06-10 23:32:55 +00:00
|
|
|
if jsonConfig.Response != nil {
|
2016-06-10 21:01:17 +00:00
|
|
|
loader := loader.NewJSONConfigLoader("type", "")
|
|
|
|
loader.RegisterCreator("none", func() interface{} { return new(NoneResponse) })
|
|
|
|
loader.RegisterCreator("http", func() interface{} { return new(HTTPResponse) })
|
2016-08-06 19:59:22 +00:00
|
|
|
response, _, err := loader.Load(jsonConfig.Response)
|
2016-06-10 21:01:17 +00:00
|
|
|
if err != nil {
|
2016-06-11 20:52:37 +00:00
|
|
|
return errors.New("Blackhole: Failed to parse response config: " + err.Error())
|
2016-06-10 21:01:17 +00:00
|
|
|
}
|
|
|
|
this.Response = response.(Response)
|
|
|
|
}
|
|
|
|
|
2016-06-10 20:26:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 11:43:06 +00:00
|
|
|
func init() {
|
2016-08-17 21:30:15 +00:00
|
|
|
registry.RegisterOutboundConfig("blackhole", func() interface{} { return new(Config) })
|
2016-01-15 11:43:06 +00:00
|
|
|
}
|