blackhole.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Package blackhole is an outbound handler that blocks all connections.
  2. package blackhole
  3. import (
  4. "time"
  5. "v2ray.com/core/app"
  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, ray ray.OutboundRay) {
  28. v.response.WriteTo(ray.OutboundOutput())
  29. // CloseError() will immediately close the connection.
  30. // Sleep a little here to make sure the response is sent to client.
  31. time.Sleep(time.Millisecond * 500)
  32. ray.OutboundInput().CloseError()
  33. ray.OutboundOutput().CloseError()
  34. }
  35. // Factory is an utility for creating blackhole handlers.
  36. type Factory struct{}
  37. // Create implements OutboundHandlerFactory.Create().
  38. func (v *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  39. return New(space, config.(*Config), meta)
  40. }