mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-02-20 23:47:21 -05:00
simplify context retrieval
This commit is contained in:
parent
af967c2b72
commit
88b25d38cb
@ -22,10 +22,7 @@ type Commander struct {
|
||||
}
|
||||
|
||||
func NewCommander(ctx context.Context, config *Config) (*Commander, error) {
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
}
|
||||
v := core.MustFromContext(ctx)
|
||||
c := &Commander{
|
||||
config: *config,
|
||||
ohm: v.OutboundHandlerManager(),
|
||||
|
@ -27,11 +27,7 @@ type DefaultDispatcher struct {
|
||||
|
||||
// NewDefaultDispatcher create a new DefaultDispatcher.
|
||||
func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatcher, error) {
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
d := &DefaultDispatcher{
|
||||
ohm: v.OutboundHandlerManager(),
|
||||
router: v.Router(),
|
||||
|
@ -54,11 +54,7 @@ func New(ctx context.Context, config *Config) (*Server, error) {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
if err := v.RegisterFeature((*core.DNSClient)(nil), server); err != nil {
|
||||
return nil, newError("unable to register DNSClient.").Base(err)
|
||||
}
|
||||
|
@ -41,10 +41,7 @@ func (s *service) Register(server *grpc.Server) {
|
||||
|
||||
func init() {
|
||||
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
||||
s := core.FromContext(ctx)
|
||||
if s == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
}
|
||||
s := core.MustFromContext(ctx)
|
||||
return &service{v: s}, nil
|
||||
}))
|
||||
}
|
||||
|
@ -139,10 +139,7 @@ func (s *service) Register(server *grpc.Server) {
|
||||
|
||||
func init() {
|
||||
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
||||
s := core.FromContext(ctx)
|
||||
if s == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
}
|
||||
s := core.MustFromContext(ctx)
|
||||
return &service{v: s}, nil
|
||||
}))
|
||||
}
|
||||
|
@ -29,10 +29,7 @@ type DynamicInboundHandler struct {
|
||||
}
|
||||
|
||||
func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
}
|
||||
v := core.MustFromContext(ctx)
|
||||
h := &DynamicInboundHandler{
|
||||
tag: tag,
|
||||
proxyConfig: proxyConfig,
|
||||
|
@ -24,10 +24,7 @@ func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error)
|
||||
m := &Manager{
|
||||
taggedHandlers: make(map[string]core.InboundHandler),
|
||||
}
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context")
|
||||
}
|
||||
v := core.MustFromContext(ctx)
|
||||
if err := v.RegisterFeature((*core.InboundHandlerManager)(nil), m); err != nil {
|
||||
return nil, newError("unable to register InboundHandlerManager").Base(err)
|
||||
}
|
||||
|
@ -261,7 +261,7 @@ type Server struct {
|
||||
// NewServer creates a new mux.Server.
|
||||
func NewServer(ctx context.Context) *Server {
|
||||
s := &Server{
|
||||
dispatcher: core.FromContext(ctx).Dispatcher(),
|
||||
dispatcher: core.MustFromContext(ctx).Dispatcher(),
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
@ -22,10 +22,7 @@ type Handler struct {
|
||||
}
|
||||
|
||||
func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (*Handler, error) {
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context")
|
||||
}
|
||||
v := core.MustFromContext(ctx)
|
||||
h := &Handler{
|
||||
config: config,
|
||||
outboundManager: v.OutboundHandlerManager(),
|
||||
|
@ -25,10 +25,7 @@ func New(ctx context.Context, config *proxyman.OutboundConfig) (*Manager, error)
|
||||
m := &Manager{
|
||||
taggedHandler: make(map[string]core.OutboundHandler),
|
||||
}
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context")
|
||||
}
|
||||
v := core.MustFromContext(ctx)
|
||||
if err := v.RegisterFeature((*core.OutboundHandlerManager)(nil), m); err != nil {
|
||||
return nil, newError("unable to register OutboundHandlerManager").Base(err)
|
||||
}
|
||||
|
@ -18,11 +18,7 @@ type Router struct {
|
||||
}
|
||||
|
||||
func NewRouter(ctx context.Context, config *Config) (*Router, error) {
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context")
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
r := &Router{
|
||||
domainStrategy: config.DomainStrategy,
|
||||
rules: make([]Rule, len(config.Rule)),
|
||||
|
11
context.go
11
context.go
@ -8,10 +8,19 @@ type key int
|
||||
|
||||
const v2rayKey key = 1
|
||||
|
||||
// FromContext returns a Instance from the given context, or nil if the context doesn't contain one.
|
||||
// FromContext returns an Instance from the given context, or nil if the context doesn't contain one.
|
||||
func FromContext(ctx context.Context) *Instance {
|
||||
if s, ok := ctx.Value(v2rayKey).(*Instance); ok {
|
||||
return s
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MustFromContext returns an Instance from the given context, or panics if not present.
|
||||
func MustFromContext(ctx context.Context) *Instance {
|
||||
v := FromContext(ctx)
|
||||
if v == nil {
|
||||
panic("V is not in context.")
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
@ -28,11 +28,7 @@ func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
|
||||
if config.NetworkList == nil || config.NetworkList.Size() == 0 {
|
||||
return nil, newError("no network specified")
|
||||
}
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
d := &DokodemoDoor{
|
||||
config: config,
|
||||
address: config.GetPredefinedAddress(),
|
||||
|
@ -27,11 +27,7 @@ type Handler struct {
|
||||
|
||||
// New creates a new Freedom handler.
|
||||
func New(ctx context.Context, config *Config) (*Handler, error) {
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not found in context.")
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
f := &Handler{
|
||||
config: *config,
|
||||
policyManager: v.PolicyManager(),
|
||||
|
@ -31,10 +31,7 @@ type Server struct {
|
||||
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
||||
s := &Server{
|
||||
config: config,
|
||||
v: core.FromContext(ctx),
|
||||
}
|
||||
if s.v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
v: core.MustFromContext(ctx),
|
||||
}
|
||||
|
||||
return s, nil
|
||||
|
@ -32,12 +32,8 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
||||
}
|
||||
client := &Client{
|
||||
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
||||
v: core.FromContext(ctx),
|
||||
v: core.MustFromContext(ctx),
|
||||
}
|
||||
if client.v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
|
@ -39,11 +39,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
||||
config: config,
|
||||
user: config.GetUser(),
|
||||
account: account,
|
||||
v: core.FromContext(ctx),
|
||||
}
|
||||
|
||||
if s.v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
v: core.MustFromContext(ctx),
|
||||
}
|
||||
|
||||
return s, nil
|
||||
|
@ -32,11 +32,7 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
||||
return nil, newError("0 target server")
|
||||
}
|
||||
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context")
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
return &Client{
|
||||
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
||||
policyManager: v.PolicyManager(),
|
||||
|
@ -27,10 +27,7 @@ type Server struct {
|
||||
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
||||
s := &Server{
|
||||
config: config,
|
||||
v: core.FromContext(ctx),
|
||||
}
|
||||
if s.v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
v: core.MustFromContext(ctx),
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
@ -105,11 +105,7 @@ type Handler struct {
|
||||
|
||||
// New creates a new VMess inbound handler.
|
||||
func New(ctx context.Context, config *Config) (*Handler, error) {
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
}
|
||||
|
||||
v := core.MustFromContext(ctx)
|
||||
handler := &Handler{
|
||||
policyManager: v.PolicyManager(),
|
||||
inboundHandlerManager: v.InboundHandlerManager(),
|
||||
|
@ -35,11 +35,7 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
|
||||
handler := &Handler{
|
||||
serverList: serverList,
|
||||
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
||||
v: core.FromContext(ctx),
|
||||
}
|
||||
|
||||
if handler.v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
v: core.MustFromContext(ctx),
|
||||
}
|
||||
|
||||
return handler, nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user