github.com/eagleql/xray-core@v1.4.4/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/eagleql/xray-core/common/serial"
     9  	"github.com/eagleql/xray-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  )