config.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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)
  18. }
  19. // WriteTo implements ResponseConfig.WriteTo().
  20. func (*NoneResponse) WriteTo(buf.Writer) {}
  21. // WriteTo implements ResponseConfig.WriteTo().
  22. func (*HTTPResponse) WriteTo(writer buf.Writer) {
  23. b := buf.NewLocal(512)
  24. common.Must(b.AppendSupplier(serial.WriteString(http403response)))
  25. writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
  26. }
  27. // GetInternalResponse converts response settings from proto to internal data structure.
  28. func (c *Config) GetInternalResponse() (ResponseConfig, error) {
  29. if c.GetResponse() == nil {
  30. return new(NoneResponse), nil
  31. }
  32. config, err := c.GetResponse().GetInstance()
  33. if err != nil {
  34. return nil, err
  35. }
  36. return config.(ResponseConfig), nil
  37. }