blackhole.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package blackhole
  2. import (
  3. "v2ray.com/core/app"
  4. "v2ray.com/core/common/alloc"
  5. v2net "v2ray.com/core/common/net"
  6. "v2ray.com/core/proxy"
  7. "v2ray.com/core/proxy/registry"
  8. "v2ray.com/core/transport/internet"
  9. "v2ray.com/core/transport/ray"
  10. )
  11. // BlackHole is an outbound connection that sliently swallow the entire payload.
  12. type BlackHole struct {
  13. meta *proxy.OutboundHandlerMeta
  14. response ResponseConfig
  15. }
  16. func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) (*BlackHole, error) {
  17. response, err := config.Response.GetInternalResponse()
  18. if err != nil {
  19. return nil, err
  20. }
  21. return &BlackHole{
  22. meta: meta,
  23. response: response,
  24. }, nil
  25. }
  26. func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
  27. payload.Release()
  28. this.response.WriteTo(ray.OutboundOutput())
  29. ray.OutboundOutput().Close()
  30. ray.OutboundInput().Release()
  31. return nil
  32. }
  33. type Factory struct{}
  34. func (this *Factory) StreamCapability() internet.StreamConnectionType {
  35. return internet.StreamConnectionTypeRawTCP
  36. }
  37. func (this *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  38. return NewBlackHole(space, config.(*Config), meta)
  39. }
  40. func init() {
  41. registry.MustRegisterOutboundHandlerCreator("blackhole", new(Factory))
  42. }