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