Skip to content

Commit 88b25d3

Browse files
committed
simplify context retrieval
1 parent af967c2 commit 88b25d3

File tree

21 files changed

+30
-88
lines changed

21 files changed

+30
-88
lines changed

app/commander/commander.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ type Commander struct {
2222
}
2323

2424
func NewCommander(ctx context.Context, config *Config) (*Commander, error) {
25-
v := core.FromContext(ctx)
26-
if v == nil {
27-
return nil, newError("V is not in context.")
28-
}
25+
v := core.MustFromContext(ctx)
2926
c := &Commander{
3027
config: *config,
3128
ohm: v.OutboundHandlerManager(),

app/dispatcher/default.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ type DefaultDispatcher struct {
2727

2828
// NewDefaultDispatcher create a new DefaultDispatcher.
2929
func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatcher, error) {
30-
v := core.FromContext(ctx)
31-
if v == nil {
32-
return nil, newError("V is not in context.")
33-
}
34-
30+
v := core.MustFromContext(ctx)
3531
d := &DefaultDispatcher{
3632
ohm: v.OutboundHandlerManager(),
3733
router: v.Router(),

app/dns/server.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ func New(ctx context.Context, config *Config) (*Server, error) {
5454
return nil
5555
},
5656
}
57-
v := core.FromContext(ctx)
58-
if v == nil {
59-
return nil, newError("V is not in context.")
60-
}
61-
57+
v := core.MustFromContext(ctx)
6258
if err := v.RegisterFeature((*core.DNSClient)(nil), server); err != nil {
6359
return nil, newError("unable to register DNSClient.").Base(err)
6460
}

app/log/command/command.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ func (s *service) Register(server *grpc.Server) {
4141

4242
func init() {
4343
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
44-
s := core.FromContext(ctx)
45-
if s == nil {
46-
return nil, newError("V is not in context.")
47-
}
44+
s := core.MustFromContext(ctx)
4845
return &service{v: s}, nil
4946
}))
5047
}

app/proxyman/command/command.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ func (s *service) Register(server *grpc.Server) {
139139

140140
func init() {
141141
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
142-
s := core.FromContext(ctx)
143-
if s == nil {
144-
return nil, newError("V is not in context.")
145-
}
142+
s := core.MustFromContext(ctx)
146143
return &service{v: s}, nil
147144
}))
148145
}

app/proxyman/inbound/dynamic.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ type DynamicInboundHandler struct {
2929
}
3030

3131
func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
32-
v := core.FromContext(ctx)
33-
if v == nil {
34-
return nil, newError("V is not in context.")
35-
}
32+
v := core.MustFromContext(ctx)
3633
h := &DynamicInboundHandler{
3734
tag: tag,
3835
proxyConfig: proxyConfig,

app/proxyman/inbound/inbound.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error)
2424
m := &Manager{
2525
taggedHandlers: make(map[string]core.InboundHandler),
2626
}
27-
v := core.FromContext(ctx)
28-
if v == nil {
29-
return nil, newError("V is not in context")
30-
}
27+
v := core.MustFromContext(ctx)
3128
if err := v.RegisterFeature((*core.InboundHandlerManager)(nil), m); err != nil {
3229
return nil, newError("unable to register InboundHandlerManager").Base(err)
3330
}

app/proxyman/mux/mux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ type Server struct {
261261
// NewServer creates a new mux.Server.
262262
func NewServer(ctx context.Context) *Server {
263263
s := &Server{
264-
dispatcher: core.FromContext(ctx).Dispatcher(),
264+
dispatcher: core.MustFromContext(ctx).Dispatcher(),
265265
}
266266
return s
267267
}

app/proxyman/outbound/handler.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ type Handler struct {
2222
}
2323

2424
func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (*Handler, error) {
25-
v := core.FromContext(ctx)
26-
if v == nil {
27-
return nil, newError("V is not in context")
28-
}
25+
v := core.MustFromContext(ctx)
2926
h := &Handler{
3027
config: config,
3128
outboundManager: v.OutboundHandlerManager(),

app/proxyman/outbound/outbound.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ func New(ctx context.Context, config *proxyman.OutboundConfig) (*Manager, error)
2525
m := &Manager{
2626
taggedHandler: make(map[string]core.OutboundHandler),
2727
}
28-
v := core.FromContext(ctx)
29-
if v == nil {
30-
return nil, newError("V is not in context")
31-
}
28+
v := core.MustFromContext(ctx)
3229
if err := v.RegisterFeature((*core.OutboundHandlerManager)(nil), m); err != nil {
3330
return nil, newError("unable to register OutboundHandlerManager").Base(err)
3431
}

app/router/router.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ type Router struct {
1818
}
1919

2020
func NewRouter(ctx context.Context, config *Config) (*Router, error) {
21-
v := core.FromContext(ctx)
22-
if v == nil {
23-
return nil, newError("V is not in context")
24-
}
25-
21+
v := core.MustFromContext(ctx)
2622
r := &Router{
2723
domainStrategy: config.DomainStrategy,
2824
rules: make([]Rule, len(config.Rule)),

context.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ type key int
88

99
const v2rayKey key = 1
1010

11-
// FromContext returns a Instance from the given context, or nil if the context doesn't contain one.
11+
// FromContext returns an Instance from the given context, or nil if the context doesn't contain one.
1212
func FromContext(ctx context.Context) *Instance {
1313
if s, ok := ctx.Value(v2rayKey).(*Instance); ok {
1414
return s
1515
}
1616
return nil
1717
}
18+
19+
// MustFromContext returns an Instance from the given context, or panics if not present.
20+
func MustFromContext(ctx context.Context) *Instance {
21+
v := FromContext(ctx)
22+
if v == nil {
23+
panic("V is not in context.")
24+
}
25+
return v
26+
}

proxy/dokodemo/dokodemo.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
2828
if config.NetworkList == nil || config.NetworkList.Size() == 0 {
2929
return nil, newError("no network specified")
3030
}
31-
v := core.FromContext(ctx)
32-
if v == nil {
33-
return nil, newError("V is not in context.")
34-
}
35-
31+
v := core.MustFromContext(ctx)
3632
d := &DokodemoDoor{
3733
config: config,
3834
address: config.GetPredefinedAddress(),

proxy/freedom/freedom.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ type Handler struct {
2727

2828
// New creates a new Freedom handler.
2929
func New(ctx context.Context, config *Config) (*Handler, error) {
30-
v := core.FromContext(ctx)
31-
if v == nil {
32-
return nil, newError("V is not found in context.")
33-
}
34-
30+
v := core.MustFromContext(ctx)
3531
f := &Handler{
3632
config: *config,
3733
policyManager: v.PolicyManager(),

proxy/http/server.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ type Server struct {
3131
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
3232
s := &Server{
3333
config: config,
34-
v: core.FromContext(ctx),
35-
}
36-
if s.v == nil {
37-
return nil, newError("V is not in context.")
34+
v: core.MustFromContext(ctx),
3835
}
3936

4037
return s, nil

proxy/shadowsocks/client.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
3232
}
3333
client := &Client{
3434
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
35-
v: core.FromContext(ctx),
35+
v: core.MustFromContext(ctx),
3636
}
37-
if client.v == nil {
38-
return nil, newError("V is not in context.")
39-
}
40-
4137
return client, nil
4238
}
4339

proxy/shadowsocks/server.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
3939
config: config,
4040
user: config.GetUser(),
4141
account: account,
42-
v: core.FromContext(ctx),
43-
}
44-
45-
if s.v == nil {
46-
return nil, newError("V is not in context.")
42+
v: core.MustFromContext(ctx),
4743
}
4844

4945
return s, nil

proxy/socks/client.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
3232
return nil, newError("0 target server")
3333
}
3434

35-
v := core.FromContext(ctx)
36-
if v == nil {
37-
return nil, newError("V is not in context")
38-
}
39-
35+
v := core.MustFromContext(ctx)
4036
return &Client{
4137
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
4238
policyManager: v.PolicyManager(),

proxy/socks/server.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ type Server struct {
2727
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
2828
s := &Server{
2929
config: config,
30-
v: core.FromContext(ctx),
31-
}
32-
if s.v == nil {
33-
return nil, newError("V is not in context.")
30+
v: core.MustFromContext(ctx),
3431
}
3532
return s, nil
3633
}

proxy/vmess/inbound/inbound.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ type Handler struct {
105105

106106
// New creates a new VMess inbound handler.
107107
func New(ctx context.Context, config *Config) (*Handler, error) {
108-
v := core.FromContext(ctx)
109-
if v == nil {
110-
return nil, newError("V is not in context.")
111-
}
112-
108+
v := core.MustFromContext(ctx)
113109
handler := &Handler{
114110
policyManager: v.PolicyManager(),
115111
inboundHandlerManager: v.InboundHandlerManager(),

proxy/vmess/outbound/outbound.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
3535
handler := &Handler{
3636
serverList: serverList,
3737
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
38-
v: core.FromContext(ctx),
39-
}
40-
41-
if handler.v == nil {
42-
return nil, newError("V is not in context.")
38+
v: core.MustFromContext(ctx),
4339
}
4440

4541
return handler, nil

0 commit comments

Comments
 (0)