github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/proxy/blackhole/config.go (about)

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