blackhole.go 1.1 KB

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