optimize: show outbound name when notify kernel connectivity change

This commit is contained in:
mzz2017 2023-02-17 02:11:08 +08:00
parent 739682dabc
commit 791752399d
4 changed files with 12 additions and 10 deletions

View File

@ -517,7 +517,7 @@ func (d *Dialer) HttpCheck(ctx context.Context, u *netutils.URL, ip netip.Addr)
if page := path.Base(req.URL.Path); strings.HasPrefix(page, "generate_") {
return strconv.Itoa(resp.StatusCode) == strings.TrimPrefix(page, "generate_"), nil
}
return resp.StatusCode >= 200 && resp.StatusCode < 400, nil
return resp.StatusCode >= 200 && resp.StatusCode < 500, nil
}
func (d *Dialer) DnsCheck(ctx context.Context, dns netip.AddrPort, tcp bool) (ok bool, err error) {

View File

@ -26,9 +26,9 @@ func FormatL4Proto(l4proto uint8) string {
func (c *ControlPlaneCore) OutboundAliveChangeCallback(outbound uint8) func(alive bool, networkType *dialer.NetworkType) {
return func(alive bool, networkType *dialer.NetworkType) {
c.log.WithFields(logrus.Fields{
"alive": alive,
"network": networkType.StringWithoutDns(),
"outbound_id": outbound,
"alive": alive,
"network": networkType.StringWithoutDns(),
"outbound": c.outboundId2Name[outbound],
}).Warnf("Outbound alive state changed, notify the kernel program.")
value := uint32(0)

View File

@ -41,8 +41,7 @@ type ControlPlane struct {
listenIp string
// TODO: add mutex?
outbounds []*outbound.DialerGroup
outboundName2Id map[string]uint8
outbounds []*outbound.DialerGroup
SimulatedLpmTries [][]netip.Prefix
SimulatedDomainSet []DomainSet
@ -266,12 +265,15 @@ func NewControlPlane(
return nil, fmt.Errorf("too many outbounds")
}
outboundName2Id := make(map[string]uint8)
outboundId2Name := make(map[uint8]string)
for i, o := range outbounds {
if _, exist := outboundName2Id[o.Name]; exist {
return nil, fmt.Errorf("duplicated outbound name: %v", o.Name)
}
outboundName2Id[o.Name] = uint8(i)
outboundId2Name[uint8(i)] = o.Name
}
core.outboundId2Name = outboundId2Name
builder := NewRoutingMatcherBuilder(outboundName2Id, &bpf)
var rules []*config_parser.RoutingRule
if rules, err = routing.ApplyRulesOptimizers(routingA.Rules,
@ -304,7 +306,6 @@ func NewControlPlane(
deferFuncs: nil,
listenIp: "0.0.0.0",
outbounds: outbounds,
outboundName2Id: outboundName2Id,
SimulatedLpmTries: builder.SimulatedLpmTries,
SimulatedDomainSet: builder.SimulatedDomainSet,
Final: routingA.Final,

View File

@ -21,9 +21,10 @@ import (
)
type ControlPlaneCore struct {
log *logrus.Logger
deferFuncs []func() error
bpf *bpfObjects
log *logrus.Logger
deferFuncs []func() error
bpf *bpfObjects
outboundId2Name map[uint8]string
kernelVersion *internal.Version
}