config.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. writer.Write(b)
  25. }
  26. // GetInternalResponse converts response settings from proto to internal data structure.
  27. func (v *Config) GetInternalResponse() (ResponseConfig, error) {
  28. if v.GetResponse() == nil {
  29. return new(NoneResponse), nil
  30. }
  31. config, err := v.GetResponse().GetInstance()
  32. if err != nil {
  33. return nil, err
  34. }
  35. return config.(ResponseConfig), nil
  36. }