simplify context retrieval

pull/876/head
Darien Raymond 2018-02-21 17:05:29 +01:00
parent af967c2b72
commit 88b25d38cb
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
21 changed files with 30 additions and 88 deletions

View File

@ -22,10 +22,7 @@ type Commander struct {
} }
func NewCommander(ctx context.Context, config *Config) (*Commander, error) { func NewCommander(ctx context.Context, config *Config) (*Commander, error) {
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context.")
}
c := &Commander{ c := &Commander{
config: *config, config: *config,
ohm: v.OutboundHandlerManager(), ohm: v.OutboundHandlerManager(),

View File

@ -27,11 +27,7 @@ type DefaultDispatcher struct {
// NewDefaultDispatcher create a new DefaultDispatcher. // NewDefaultDispatcher create a new DefaultDispatcher.
func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatcher, error) { func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatcher, error) {
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context.")
}
d := &DefaultDispatcher{ d := &DefaultDispatcher{
ohm: v.OutboundHandlerManager(), ohm: v.OutboundHandlerManager(),
router: v.Router(), router: v.Router(),

View File

@ -54,11 +54,7 @@ func New(ctx context.Context, config *Config) (*Server, error) {
return nil return nil
}, },
} }
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context.")
}
if err := v.RegisterFeature((*core.DNSClient)(nil), server); err != nil { if err := v.RegisterFeature((*core.DNSClient)(nil), server); err != nil {
return nil, newError("unable to register DNSClient.").Base(err) return nil, newError("unable to register DNSClient.").Base(err)
} }

View File

@ -41,10 +41,7 @@ func (s *service) Register(server *grpc.Server) {
func init() { func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) { common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
s := core.FromContext(ctx) s := core.MustFromContext(ctx)
if s == nil {
return nil, newError("V is not in context.")
}
return &service{v: s}, nil return &service{v: s}, nil
})) }))
} }

View File

@ -139,10 +139,7 @@ func (s *service) Register(server *grpc.Server) {
func init() { func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) { common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
s := core.FromContext(ctx) s := core.MustFromContext(ctx)
if s == nil {
return nil, newError("V is not in context.")
}
return &service{v: s}, nil return &service{v: s}, nil
})) }))
} }

View File

@ -29,10 +29,7 @@ type DynamicInboundHandler struct {
} }
func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) { func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context.")
}
h := &DynamicInboundHandler{ h := &DynamicInboundHandler{
tag: tag, tag: tag,
proxyConfig: proxyConfig, proxyConfig: proxyConfig,

View File

@ -24,10 +24,7 @@ func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error)
m := &Manager{ m := &Manager{
taggedHandlers: make(map[string]core.InboundHandler), taggedHandlers: make(map[string]core.InboundHandler),
} }
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context")
}
if err := v.RegisterFeature((*core.InboundHandlerManager)(nil), m); err != nil { if err := v.RegisterFeature((*core.InboundHandlerManager)(nil), m); err != nil {
return nil, newError("unable to register InboundHandlerManager").Base(err) return nil, newError("unable to register InboundHandlerManager").Base(err)
} }

View File

@ -261,7 +261,7 @@ type Server struct {
// NewServer creates a new mux.Server. // NewServer creates a new mux.Server.
func NewServer(ctx context.Context) *Server { func NewServer(ctx context.Context) *Server {
s := &Server{ s := &Server{
dispatcher: core.FromContext(ctx).Dispatcher(), dispatcher: core.MustFromContext(ctx).Dispatcher(),
} }
return s return s
} }

View File

@ -22,10 +22,7 @@ type Handler struct {
} }
func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (*Handler, error) { func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (*Handler, error) {
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context")
}
h := &Handler{ h := &Handler{
config: config, config: config,
outboundManager: v.OutboundHandlerManager(), outboundManager: v.OutboundHandlerManager(),

View File

@ -25,10 +25,7 @@ func New(ctx context.Context, config *proxyman.OutboundConfig) (*Manager, error)
m := &Manager{ m := &Manager{
taggedHandler: make(map[string]core.OutboundHandler), taggedHandler: make(map[string]core.OutboundHandler),
} }
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context")
}
if err := v.RegisterFeature((*core.OutboundHandlerManager)(nil), m); err != nil { if err := v.RegisterFeature((*core.OutboundHandlerManager)(nil), m); err != nil {
return nil, newError("unable to register OutboundHandlerManager").Base(err) return nil, newError("unable to register OutboundHandlerManager").Base(err)
} }

View File

@ -18,11 +18,7 @@ type Router struct {
} }
func NewRouter(ctx context.Context, config *Config) (*Router, error) { func NewRouter(ctx context.Context, config *Config) (*Router, error) {
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context")
}
r := &Router{ r := &Router{
domainStrategy: config.DomainStrategy, domainStrategy: config.DomainStrategy,
rules: make([]Rule, len(config.Rule)), rules: make([]Rule, len(config.Rule)),

View File

@ -8,10 +8,19 @@ type key int
const v2rayKey key = 1 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 { func FromContext(ctx context.Context) *Instance {
if s, ok := ctx.Value(v2rayKey).(*Instance); ok { if s, ok := ctx.Value(v2rayKey).(*Instance); ok {
return s return s
} }
return nil 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
}

View File

@ -28,11 +28,7 @@ func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
if config.NetworkList == nil || config.NetworkList.Size() == 0 { if config.NetworkList == nil || config.NetworkList.Size() == 0 {
return nil, newError("no network specified") return nil, newError("no network specified")
} }
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context.")
}
d := &DokodemoDoor{ d := &DokodemoDoor{
config: config, config: config,
address: config.GetPredefinedAddress(), address: config.GetPredefinedAddress(),

View File

@ -27,11 +27,7 @@ type Handler struct {
// New creates a new Freedom handler. // New creates a new Freedom handler.
func New(ctx context.Context, config *Config) (*Handler, error) { func New(ctx context.Context, config *Config) (*Handler, error) {
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not found in context.")
}
f := &Handler{ f := &Handler{
config: *config, config: *config,
policyManager: v.PolicyManager(), policyManager: v.PolicyManager(),

View File

@ -31,10 +31,7 @@ type Server struct {
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) { func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
s := &Server{ s := &Server{
config: config, config: config,
v: core.FromContext(ctx), v: core.MustFromContext(ctx),
}
if s.v == nil {
return nil, newError("V is not in context.")
} }
return s, nil return s, nil

View File

@ -32,12 +32,8 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
} }
client := &Client{ client := &Client{
serverPicker: protocol.NewRoundRobinServerPicker(serverList), 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 return client, nil
} }

View File

@ -39,11 +39,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
config: config, config: config,
user: config.GetUser(), user: config.GetUser(),
account: account, account: account,
v: core.FromContext(ctx), v: core.MustFromContext(ctx),
}
if s.v == nil {
return nil, newError("V is not in context.")
} }
return s, nil return s, nil

View File

@ -32,11 +32,7 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
return nil, newError("0 target server") return nil, newError("0 target server")
} }
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context")
}
return &Client{ return &Client{
serverPicker: protocol.NewRoundRobinServerPicker(serverList), serverPicker: protocol.NewRoundRobinServerPicker(serverList),
policyManager: v.PolicyManager(), policyManager: v.PolicyManager(),

View File

@ -27,10 +27,7 @@ type Server struct {
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) { func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
s := &Server{ s := &Server{
config: config, config: config,
v: core.FromContext(ctx), v: core.MustFromContext(ctx),
}
if s.v == nil {
return nil, newError("V is not in context.")
} }
return s, nil return s, nil
} }

View File

@ -105,11 +105,7 @@ type Handler struct {
// New creates a new VMess inbound handler. // New creates a new VMess inbound handler.
func New(ctx context.Context, config *Config) (*Handler, error) { func New(ctx context.Context, config *Config) (*Handler, error) {
v := core.FromContext(ctx) v := core.MustFromContext(ctx)
if v == nil {
return nil, newError("V is not in context.")
}
handler := &Handler{ handler := &Handler{
policyManager: v.PolicyManager(), policyManager: v.PolicyManager(),
inboundHandlerManager: v.InboundHandlerManager(), inboundHandlerManager: v.InboundHandlerManager(),

View File

@ -35,11 +35,7 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
handler := &Handler{ handler := &Handler{
serverList: serverList, serverList: serverList,
serverPicker: protocol.NewRoundRobinServerPicker(serverList), serverPicker: protocol.NewRoundRobinServerPicker(serverList),
v: core.FromContext(ctx), v: core.MustFromContext(ctx),
}
if handler.v == nil {
return nil, newError("V is not in context.")
} }
return handler, nil return handler, nil