blackhole.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package blackhole
  2. import (
  3. "v2ray.com/core/app"
  4. "v2ray.com/core/common/alloc"
  5. "v2ray.com/core/common/loader"
  6. v2net "v2ray.com/core/common/net"
  7. "v2ray.com/core/proxy"
  8. "v2ray.com/core/proxy/registry"
  9. "v2ray.com/core/transport/ray"
  10. )
  11. // BlackHole is an outbound connection that sliently swallow the entire payload.
  12. type BlackHole struct {
  13. meta *proxy.OutboundHandlerMeta
  14. response ResponseConfig
  15. }
  16. func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) (*BlackHole, error) {
  17. response, err := config.GetInternalResponse()
  18. if err != nil {
  19. return nil, err
  20. }
  21. return &BlackHole{
  22. meta: meta,
  23. response: response,
  24. }, nil
  25. }
  26. func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
  27. payload.Release()
  28. this.response.WriteTo(ray.OutboundOutput())
  29. ray.OutboundOutput().Close()
  30. ray.OutboundInput().Release()
  31. return nil
  32. }
  33. type Factory struct{}
  34. func (this *Factory) StreamCapability() v2net.NetworkList {
  35. return v2net.NetworkList{
  36. Network: []v2net.Network{v2net.Network_RawTCP},
  37. }
  38. }
  39. func (this *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  40. return NewBlackHole(space, config.(*Config), meta)
  41. }
  42. func init() {
  43. registry.MustRegisterOutboundHandlerCreator(loader.GetType(new(Config)), new(Factory))
  44. }