blackhole.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Package blackhole is an outbound handler that blocks all connections.
  2. package blackhole
  3. import (
  4. "context"
  5. "time"
  6. "v2ray.com/core/common"
  7. v2net "v2ray.com/core/common/net"
  8. "v2ray.com/core/transport/ray"
  9. )
  10. // Handler is an outbound connection that sliently swallow the entire payload.
  11. type Handler struct {
  12. response ResponseConfig
  13. }
  14. // New creates a new blackhole handler.
  15. func New(ctx context.Context, config *Config) (*Handler, error) {
  16. response, err := config.GetInternalResponse()
  17. if err != nil {
  18. return nil, err
  19. }
  20. return &Handler{
  21. response: response,
  22. }, nil
  23. }
  24. // Dispatch implements OutboundHandler.Dispatch().
  25. func (v *Handler) Dispatch(destination v2net.Destination, ray ray.OutboundRay) {
  26. v.response.WriteTo(ray.OutboundOutput())
  27. // CloseError() will immediately close the connection.
  28. // Sleep a little here to make sure the response is sent to client.
  29. time.Sleep(time.Millisecond * 500)
  30. ray.OutboundInput().CloseError()
  31. ray.OutboundOutput().CloseError()
  32. }
  33. func init() {
  34. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  35. return New(ctx, config.(*Config))
  36. }))
  37. }