blackhole.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package blackhole
  2. import (
  3. "github.com/v2ray/v2ray-core/app"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. "github.com/v2ray/v2ray-core/proxy"
  7. "github.com/v2ray/v2ray-core/proxy/internal"
  8. "github.com/v2ray/v2ray-core/transport/internet"
  9. "github.com/v2ray/v2ray-core/transport/ray"
  10. )
  11. // BlackHole is an outbound connection that sliently swallow the entire payload.
  12. type BlackHole struct {
  13. meta *proxy.OutboundHandlerMeta
  14. response Response
  15. }
  16. func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) *BlackHole {
  17. return &BlackHole{
  18. meta: meta,
  19. response: config.Response,
  20. }
  21. }
  22. func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
  23. payload.Release()
  24. this.response.WriteTo(ray.OutboundOutput())
  25. ray.OutboundOutput().Close()
  26. ray.OutboundInput().Release()
  27. return nil
  28. }
  29. type Factory struct{}
  30. func (this *Factory) StreamCapability() internet.StreamConnectionType {
  31. return internet.StreamConnectionTypeRawTCP
  32. }
  33. func (this *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  34. return NewBlackHole(space, config.(*Config), meta), nil
  35. }
  36. func init() {
  37. internal.MustRegisterOutboundHandlerCreator("blackhole", new(Factory))
  38. }