blackhole.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package blackhole
  2. import (
  3. "v2ray.com/core/app"
  4. "v2ray.com/core/common/alloc"
  5. v2net "v2ray.com/core/common/net"
  6. "v2ray.com/core/proxy"
  7. "v2ray.com/core/proxy/registry"
  8. "v2ray.com/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 ResponseConfig
  14. }
  15. func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) (*BlackHole, error) {
  16. response, err := config.Response.GetInternalResponse()
  17. if err != nil {
  18. return nil, err
  19. }
  20. return &BlackHole{
  21. meta: meta,
  22. response: response,
  23. }, nil
  24. }
  25. func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
  26. payload.Release()
  27. this.response.WriteTo(ray.OutboundOutput())
  28. ray.OutboundOutput().Close()
  29. ray.OutboundInput().Release()
  30. return nil
  31. }
  32. type Factory struct{}
  33. func (this *Factory) StreamCapability() v2net.NetworkList {
  34. return v2net.NetworkList{
  35. Network: []v2net.Network{v2net.Network_RawTCP},
  36. }
  37. }
  38. func (this *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  39. return NewBlackHole(space, config.(*Config), meta)
  40. }
  41. func init() {
  42. registry.MustRegisterOutboundHandlerCreator("blackhole", new(Factory))
  43. }