config.go 1.1 KB

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