blackhole.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Package blackhole is an outbound handler that blocks all connections.
  2. package blackhole
  3. //go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg blackhole -path Proxy,Blackhole
  4. import (
  5. "context"
  6. "time"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/proxy"
  9. "v2ray.com/core/transport/ray"
  10. )
  11. // Handler is an outbound connection that silently swallow the entire payload.
  12. type Handler struct {
  13. response ResponseConfig
  14. }
  15. // New creates a new blackhole handler.
  16. func New(ctx context.Context, config *Config) (*Handler, error) {
  17. response, err := config.GetInternalResponse()
  18. if err != nil {
  19. return nil, err
  20. }
  21. return &Handler{
  22. response: response,
  23. }, nil
  24. }
  25. // Process implements OutboundHandler.Dispatch().
  26. func (h *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
  27. h.response.WriteTo(outboundRay.OutboundOutput())
  28. // Sleep a little here to make sure the response is sent to client.
  29. time.Sleep(time.Second)
  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. }