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/tools/generrorgen/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 (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
  27. v.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. time.Sleep(time.Second * 2)
  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. }