blackhole.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // +build !confonly
  2. // Package blackhole is an outbound handler that blocks all connections.
  3. package blackhole
  4. //go:generate errorgen
  5. import (
  6. "context"
  7. "time"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/transport"
  10. "v2ray.com/core/transport/internet"
  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 *transport.Link, dialer internet.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. common.Interrupt(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. }