Skip to content

Commit

Permalink
fix: Try to fix reload panic
Browse files Browse the repository at this point in the history
  • Loading branch information
layou233 committed Nov 4, 2024
1 parent 57d61b1 commit 967ca48
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions route/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,33 @@ func (r *Router) Initialize(ctx context.Context, logger *log.Logger, options Rou
if r.started {
return errors.New("already initialized")
}
r.ctx = ctx
r.logger = logger
r.outboundMap = options.OutboundMap
r.listMap = options.ListMap
r.ruleRegistry = options.RuleRegistry
r.snifferRegistry = options.SnifferRegistry
r.rules = make([]Rule, 0, len(options.Config.Rules))
rules := make([]Rule, 0, len(options.Config.Rules))
for i, ruleConfig := range options.Config.Rules {
rule, err := NewRule(logger, ruleConfig, r.listMap, r.ruleRegistry)
rule, err := NewRule(logger, ruleConfig, options.ListMap, options.RuleRegistry)
if err != nil {
return fmt.Errorf("initialize rule [index=%d]: %w", i, err)
}
r.rules = append(r.rules, rule)
rules = append(rules, rule)
}
if options.Config.DefaultOutbound != "" {
var err error
r.defaultOutbound, err = r.FindOutboundByName(options.Config.DefaultOutbound)
defaultOutbound, err := r.findOutboundByName(options.OutboundMap, options.Config.DefaultOutbound)
if err != nil {
return common.Cause("default outbound is not found: ", err)
}
r.defaultOutbound = defaultOutbound
} else {
r.defaultOutbound, _ = protocol.NewOutbound(r.logger, &config.Outbound{
Name: "default",
})
r.defaultOutbound.PostInitialize(r)
r.defaultOutbound.PostInitialize(r) // this is dangerous since the router is not fully initialized yet
}
r.ctx = ctx
r.logger = logger
r.outboundMap = options.OutboundMap
r.listMap = options.ListMap
r.ruleRegistry = options.RuleRegistry
r.snifferRegistry = options.SnifferRegistry
r.rules = rules
r.started = true
return nil
}
Expand Down Expand Up @@ -169,16 +170,20 @@ func (r *Router) HandleConnection(conn net.Conn, metadata *adapter.Metadata) {
}

func (r *Router) FindOutboundByName(name string) (adapter.Outbound, error) {
return r.findOutboundByName(r.outboundMap, name)
}

func (r *Router) findOutboundByName(outboundMap map[string]adapter.Outbound, name string) (adapter.Outbound, error) {
switch name {
case "REJECT":
return rejectOutbound{}, nil
case "RESET":
return resetOutbound{}, nil
}
if r.outboundMap == nil {
if outboundMap == nil {
return nil, errors.New("outbounds are not initialized")
}
outbound, ok := r.outboundMap[name]
outbound, ok := outboundMap[name]
if !ok {
return nil, fmt.Errorf("outbound not found [%s]", name)
}
Expand Down

0 comments on commit 967ca48

Please sign in to comment.