simplify context retrieval

This commit is contained in:
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) {
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(),

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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