blackhole.go 1.4 KB

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