github.com/v2fly/v2ray-core/v4@v4.45.2/proxy/blackhole/blackhole.go (about) 1 //go:build !confonly 2 // +build !confonly 3 4 // Package blackhole is an outbound handler that blocks all connections. 5 package blackhole 6 7 //go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen 8 9 import ( 10 "context" 11 "time" 12 13 "github.com/v2fly/v2ray-core/v4/common" 14 "github.com/v2fly/v2ray-core/v4/transport" 15 "github.com/v2fly/v2ray-core/v4/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 }