config.go 1.1 KB

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