blackhole.go 946 B

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