github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/infra/conf/blackhole.go (about)

     1  package conf
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/golang/protobuf/proto"
     7  
     8  	"v2ray.com/core/common/serial"
     9  	"v2ray.com/core/proxy/blackhole"
    10  )
    11  
    12  type NoneResponse struct{}
    13  
    14  func (*NoneResponse) Build() (proto.Message, error) {
    15  	return new(blackhole.NoneResponse), nil
    16  }
    17  
    18  type HttpResponse struct{}
    19  
    20  func (*HttpResponse) Build() (proto.Message, error) {
    21  	return new(blackhole.HTTPResponse), nil
    22  }
    23  
    24  type BlackholeConfig struct {
    25  	Response json.RawMessage `json:"response"`
    26  }
    27  
    28  func (v *BlackholeConfig) Build() (proto.Message, error) {
    29  	config := new(blackhole.Config)
    30  	if v.Response != nil {
    31  		response, _, err := configLoader.Load(v.Response)
    32  		if err != nil {
    33  			return nil, newError("Config: Failed to parse Blackhole response config.").Base(err)
    34  		}
    35  		responseSettings, err := response.(Buildable).Build()
    36  		if err != nil {
    37  			return nil, err
    38  		}
    39  		config.Response = serial.ToTypedMessage(responseSettings)
    40  	}
    41  
    42  	return config, nil
    43  }
    44  
    45  var (
    46  	configLoader = NewJSONConfigLoader(
    47  		ConfigCreatorCache{
    48  			"none": func() interface{} { return new(NoneResponse) },
    49  			"http": func() interface{} { return new(HttpResponse) },
    50  		},
    51  		"type",
    52  		"")
    53  )