github.com/imannamdari/v2ray-core/v5@v5.0.5/proxy/blackhole/config.go (about)

     1  package blackhole
     2  
     3  import (
     4  	"github.com/imannamdari/v2ray-core/v5/common"
     5  	"github.com/imannamdari/v2ray-core/v5/common/buf"
     6  	"github.com/imannamdari/v2ray-core/v5/common/serial"
     7  )
     8  
     9  const (
    10  	http403response = `HTTP/1.1 403 Forbidden
    11  Connection: close
    12  Cache-Control: max-age=3600, public
    13  Content-Length: 0
    14  
    15  
    16  `
    17  )
    18  
    19  // ResponseConfig is the configuration for blackhole responses.
    20  type ResponseConfig interface {
    21  	// WriteTo writes predefined response to the give buffer.
    22  	WriteTo(buf.Writer) int32
    23  }
    24  
    25  // WriteTo implements ResponseConfig.WriteTo().
    26  func (*NoneResponse) WriteTo(buf.Writer) int32 { return 0 }
    27  
    28  // WriteTo implements ResponseConfig.WriteTo().
    29  func (*HTTPResponse) WriteTo(writer buf.Writer) int32 {
    30  	b := buf.New()
    31  	common.Must2(b.WriteString(http403response))
    32  	n := b.Len()
    33  	writer.WriteMultiBuffer(buf.MultiBuffer{b})
    34  	return n
    35  }
    36  
    37  // GetInternalResponse converts response settings from proto to internal data structure.
    38  func (c *Config) GetInternalResponse() (ResponseConfig, error) {
    39  	if c.GetResponse() == nil {
    40  		return new(NoneResponse), nil
    41  	}
    42  
    43  	config, err := serial.GetInstanceOf(c.GetResponse())
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return config.(ResponseConfig), nil
    48  }