blackhole.go 901 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package blackhole
  2. import (
  3. "io/ioutil"
  4. "github.com/v2ray/v2ray-core/app"
  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. }
  13. func NewBlackHole() *BlackHole {
  14. return &BlackHole{}
  15. }
  16. func (this *BlackHole) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
  17. if chunk := firstPacket.Chunk(); chunk != nil {
  18. chunk.Release()
  19. }
  20. close(ray.OutboundOutput())
  21. if firstPacket.MoreChunks() {
  22. v2net.ChanToWriter(ioutil.Discard, ray.OutboundInput())
  23. }
  24. return nil
  25. }
  26. func init() {
  27. internal.MustRegisterOutboundHandlerCreator("blackhole",
  28. func(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
  29. return NewBlackHole(), nil
  30. })
  31. }