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. "v2ray.com/core/transport/ray"
  8. )
  9. // Handler is an outbound connection that sliently swallow the entire payload.
  10. type Handler struct {
  11. response ResponseConfig
  12. }
  13. // New creates a new blackhole handler.
  14. func New(ctx context.Context, config *Config) (*Handler, error) {
  15. response, err := config.GetInternalResponse()
  16. if err != nil {
  17. return nil, err
  18. }
  19. return &Handler{
  20. response: response,
  21. }, nil
  22. }
  23. // Dispatch implements OutboundHandler.Dispatch().
  24. func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay) error {
  25. v.response.WriteTo(outboundRay.OutboundOutput())
  26. // CloseError() will immediately close the connection.
  27. // Sleep a little here to make sure the response is sent to client.
  28. time.Sleep(time.Millisecond * 500)
  29. outboundRay.OutboundInput().CloseError()
  30. outboundRay.OutboundOutput().CloseError()
  31. return nil
  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. }