From 791752399d2205d821ab0b7e35c0095a16c6ea15 Mon Sep 17 00:00:00 2001 From: mzz2017 <2017@duck.com> Date: Fri, 17 Feb 2023 02:11:08 +0800 Subject: [PATCH] optimize: show outbound name when notify kernel connectivity change --- component/outbound/dialer/connectivity_check.go | 2 +- control/connectivity.go | 6 +++--- control/control_plane.go | 7 ++++--- control/control_plane_core.go | 7 ++++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/component/outbound/dialer/connectivity_check.go b/component/outbound/dialer/connectivity_check.go index e320c25..b96c058 100644 --- a/component/outbound/dialer/connectivity_check.go +++ b/component/outbound/dialer/connectivity_check.go @@ -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) { diff --git a/control/connectivity.go b/control/connectivity.go index 3eb3178..efd9e15 100644 --- a/control/connectivity.go +++ b/control/connectivity.go @@ -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) diff --git a/control/control_plane.go b/control/control_plane.go index 7ac9811..9073b6a 100644 --- a/control/control_plane.go +++ b/control/control_plane.go @@ -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, diff --git a/control/control_plane_core.go b/control/control_plane_core.go index dd3addc..afc93f9 100644 --- a/control/control_plane_core.go +++ b/control/control_plane_core.go @@ -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 }