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