2016-01-15 06:43:06 -05:00
|
|
|
// +build json
|
|
|
|
|
|
|
|
package blackhole
|
|
|
|
|
|
|
|
import (
|
2016-06-10 17:01:17 -04:00
|
|
|
"encoding/json"
|
2016-06-11 16:52:37 -04:00
|
|
|
"errors"
|
2016-06-10 17:01:17 -04:00
|
|
|
|
2016-09-22 06:01:36 -04:00
|
|
|
"strings"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/loader"
|
|
|
|
"v2ray.com/core/proxy/registry"
|
2016-01-15 06:43:06 -05:00
|
|
|
)
|
|
|
|
|
2016-06-10 16:26:39 -04:00
|
|
|
func (this *Config) UnmarshalJSON(data []byte) error {
|
2016-06-10 17:01:17 -04:00
|
|
|
type JSONConfig struct {
|
|
|
|
Response json.RawMessage `json:"response"`
|
|
|
|
}
|
|
|
|
jsonConfig := new(JSONConfig)
|
|
|
|
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
2016-06-11 16:52:37 -04:00
|
|
|
return errors.New("Blackhole: Failed to parse config: " + err.Error())
|
2016-06-10 17:01:17 -04:00
|
|
|
}
|
2016-06-10 17:09:14 -04:00
|
|
|
|
2016-06-10 19:32:55 -04:00
|
|
|
if jsonConfig.Response != nil {
|
2016-09-22 06:01:36 -04:00
|
|
|
response, rType, err := configLoader.Load(jsonConfig.Response)
|
2016-06-10 17:01:17 -04:00
|
|
|
if err != nil {
|
2016-06-11 16:52:37 -04:00
|
|
|
return errors.New("Blackhole: Failed to parse response config: " + err.Error())
|
2016-06-10 17:01:17 -04:00
|
|
|
}
|
2016-09-22 06:01:36 -04:00
|
|
|
this.Response = new(Response)
|
|
|
|
switch rType {
|
|
|
|
case strings.ToLower(Response_Type_name[int32(Response_None)]):
|
|
|
|
this.Response.Type = Response_None
|
|
|
|
case strings.ToLower(Response_Type_name[int32(Response_HTTP)]):
|
|
|
|
this.Response.Type = Response_HTTP
|
|
|
|
}
|
|
|
|
this.Response.Settings = response.(ResponseConfig).AsAny()
|
2016-06-10 17:01:17 -04:00
|
|
|
}
|
|
|
|
|
2016-06-10 16:26:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-22 06:01:36 -04:00
|
|
|
var (
|
|
|
|
configLoader = loader.NewJSONConfigLoader(cache, "type", "")
|
|
|
|
)
|
|
|
|
|
2016-01-15 06:43:06 -05:00
|
|
|
func init() {
|
2016-08-17 17:30:15 -04:00
|
|
|
registry.RegisterOutboundConfig("blackhole", func() interface{} { return new(Config) })
|
2016-01-15 06:43:06 -05:00
|
|
|
}
|