blackhole.go 978 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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/common/connhandler"
  7. "github.com/v2ray/v2ray-core/transport/ray"
  8. )
  9. // BlackHole is an outbound connection that sliently swallow the entire payload.
  10. type BlackHole struct {
  11. }
  12. func NewBlackHole() *BlackHole {
  13. return &BlackHole{}
  14. }
  15. func (this *BlackHole) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
  16. if chunk := firstPacket.Chunk(); chunk != nil {
  17. chunk.Release()
  18. }
  19. close(ray.OutboundOutput())
  20. if firstPacket.MoreChunks() {
  21. v2net.ChanToWriter(ioutil.Discard, ray.OutboundInput())
  22. }
  23. return nil
  24. }
  25. type BlackHoleFactory struct {
  26. }
  27. func (this BlackHoleFactory) Create(space app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) {
  28. return NewBlackHole(), nil
  29. }
  30. func init() {
  31. connhandler.RegisterOutboundConnectionHandlerFactory("blackhole", BlackHoleFactory{})
  32. }