blackhole.go 1.2 KB

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