blackhole.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Package blackhole is an outbound handler that blocks all connections.
  2. package blackhole
  3. //go:generate errorgen
  4. import (
  5. "context"
  6. "time"
  7. "v2ray.com/core"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/proxy"
  10. "v2ray.com/core/transport/pipe"
  11. )
  12. // Handler is an outbound connection that silently swallow the entire payload.
  13. type Handler struct {
  14. response ResponseConfig
  15. }
  16. // New creates a new blackhole handler.
  17. func New(ctx context.Context, config *Config) (*Handler, error) {
  18. response, err := config.GetInternalResponse()
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &Handler{
  23. response: response,
  24. }, nil
  25. }
  26. // Process implements OutboundHandler.Dispatch().
  27. func (h *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dialer) error {
  28. nBytes := h.response.WriteTo(link.Writer)
  29. if nBytes > 0 {
  30. // Sleep a little here to make sure the response is sent to client.
  31. time.Sleep(time.Second)
  32. }
  33. pipe.CloseError(link.Writer)
  34. return nil
  35. }
  36. func init() {
  37. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  38. return New(ctx, config.(*Config))
  39. }))
  40. }