github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/proxy/blackhole/blackhole.go (about) 1 // +build !confonly 2 3 // Package blackhole is an outbound handler that blocks all connections. 4 5 package blackhole 6 7 //go:generate go run v2ray.com/core/common/errors/errorgen 8 9 import ( 10 "context" 11 "time" 12 13 "v2ray.com/core/common" 14 "v2ray.com/core/transport" 15 "v2ray.com/core/transport/internet" 16 ) 17 18 // Handler is an outbound connection that silently swallow the entire payload. 19 type Handler struct { 20 response ResponseConfig 21 } 22 23 // New creates a new blackhole handler. 24 func New(ctx context.Context, config *Config) (*Handler, error) { 25 response, err := config.GetInternalResponse() 26 if err != nil { 27 return nil, err 28 } 29 return &Handler{ 30 response: response, 31 }, nil 32 } 33 34 // Process implements OutboundHandler.Dispatch(). 35 func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error { 36 nBytes := h.response.WriteTo(link.Writer) 37 if nBytes > 0 { 38 // Sleep a little here to make sure the response is sent to client. 39 time.Sleep(time.Second) 40 } 41 common.Interrupt(link.Writer) 42 return nil 43 } 44 45 func init() { 46 common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) { 47 return New(ctx, config.(*Config)) 48 })) 49 }