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