blackhole.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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/ray"
  9. )
  10. // BlackHole is an outbound connection that sliently swallow the entire payload.
  11. type BlackHole struct {
  12. meta *proxy.OutboundHandlerMeta
  13. response Response
  14. }
  15. func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) *BlackHole {
  16. return &BlackHole{
  17. meta: meta,
  18. response: config.Response,
  19. }
  20. }
  21. func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
  22. payload.Release()
  23. this.response.WriteTo(ray.OutboundOutput())
  24. ray.OutboundOutput().Close()
  25. ray.OutboundInput().Release()
  26. return nil
  27. }
  28. func init() {
  29. internal.MustRegisterOutboundHandlerCreator("blackhole",
  30. func(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  31. return NewBlackHole(space, config.(*Config), meta), nil
  32. })
  33. }