blackhole.go 1.1 KB

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