blackhole.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Package blackhole is an outbound handler that blocks all connections.
  2. package blackhole
  3. import (
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/common/buf"
  6. v2net "v2ray.com/core/common/net"
  7. "v2ray.com/core/proxy"
  8. "v2ray.com/core/transport/ray"
  9. )
  10. // Handler is an outbound connection that sliently swallow the entire payload.
  11. type Handler struct {
  12. meta *proxy.OutboundHandlerMeta
  13. response ResponseConfig
  14. }
  15. // New creates a new blackhole handler.
  16. func New(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  17. response, err := config.GetInternalResponse()
  18. if err != nil {
  19. return nil, err
  20. }
  21. return &Handler{
  22. meta: meta,
  23. response: response,
  24. }, nil
  25. }
  26. // Dispatch implements OutboundHandler.Dispatch().
  27. func (v *Handler) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
  28. payload.Release()
  29. v.response.WriteTo(ray.OutboundOutput())
  30. ray.OutboundOutput().Close()
  31. ray.OutboundInput().Release()
  32. }
  33. // Factory is an utility for creating blackhole handlers.
  34. type Factory struct{}
  35. // StreamCapability implements OutboundHandlerFactory.StreamCapability().
  36. func (v *Factory) StreamCapability() v2net.NetworkList {
  37. return v2net.NetworkList{
  38. Network: []v2net.Network{v2net.Network_TCP},
  39. }
  40. }
  41. // Create implements OutboundHandlerFactory.Create().
  42. func (v *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  43. return New(space, config.(*Config), meta)
  44. }