config.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package blackhole
  2. import (
  3. "v2ray.com/core/common/buf"
  4. "v2ray.com/core/common/serial"
  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)
  17. }
  18. // WriteTo implements ResponseConfig.WriteTo().
  19. func (v *NoneResponse) WriteTo(buf.Writer) {}
  20. // WriteTo implements ResponseConfig.WriteTo().
  21. func (v *HTTPResponse) WriteTo(writer buf.Writer) {
  22. b := buf.NewLocal(512)
  23. b.AppendSupplier(serial.WriteString(http403response))
  24. mb := buf.NewMultiBuffer()
  25. mb.Append(b)
  26. writer.Write(mb)
  27. }
  28. // GetInternalResponse converts response settings from proto to internal data structure.
  29. func (v *Config) GetInternalResponse() (ResponseConfig, error) {
  30. if v.GetResponse() == nil {
  31. return new(NoneResponse), nil
  32. }
  33. config, err := v.GetResponse().GetInstance()
  34. if err != nil {
  35. return nil, err
  36. }
  37. return config.(ResponseConfig), nil
  38. }