ソースを参照

simplify context retrieval

Darien Raymond 8 年 前
コミット
88b25d38cb

+ 1 - 4
app/commander/commander.go

@@ -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(),

+ 1 - 5
app/dispatcher/default.go

@@ -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(),

+ 1 - 5
app/dns/server.go

@@ -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)
 	}

+ 1 - 4
app/log/command/command.go

@@ -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
 	}))
 }

+ 1 - 4
app/proxyman/command/command.go

@@ -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
 	}))
 }

+ 1 - 4
app/proxyman/inbound/dynamic.go

@@ -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,

+ 1 - 4
app/proxyman/inbound/inbound.go

@@ -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)
 	}

+ 1 - 1
app/proxyman/mux/mux.go

@@ -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
 }

+ 1 - 4
app/proxyman/outbound/handler.go

@@ -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(),

+ 1 - 4
app/proxyman/outbound/outbound.go

@@ -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)
 	}

+ 1 - 5
app/router/router.go

@@ -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)),

+ 10 - 1
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
+}

+ 1 - 5
proxy/dokodemo/dokodemo.go

@@ -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(),

+ 1 - 5
proxy/freedom/freedom.go

@@ -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(),

+ 1 - 4
proxy/http/server.go

@@ -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

+ 1 - 5
proxy/shadowsocks/client.go

@@ -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
 }
 

+ 1 - 5
proxy/shadowsocks/server.go

@@ -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

+ 1 - 5
proxy/socks/client.go

@@ -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(),

+ 1 - 4
proxy/socks/server.go

@@ -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
 }

+ 1 - 5
proxy/vmess/inbound/inbound.go

@@ -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(),

+ 1 - 5
proxy/vmess/outbound/outbound.go

@@ -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