github.com/v2fly/v2ray-core/v4@v4.45.2/infra/conf/blackhole.go (about)

     1  package conf
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/golang/protobuf/proto"
     7  
     8  	"github.com/v2fly/v2ray-core/v4/common/serial"
     9  	"github.com/v2fly/v2ray-core/v4/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 configLoader = NewJSONConfigLoader(
    46  	ConfigCreatorCache{
    47  		"none": func() interface{} { return new(NoneResponse) },
    48  		"http": func() interface{} { return new(HTTPResponse) },
    49  	},
    50  	"type",
    51  	"")