Files
mirror-3x-ui/internal/web/service/network_traffic_routes_linux.go
T
Hamed e0c01942ec feat(monitoring): allow selecting the network interface for traffic statistics (#6972)
The panel's network totals and speed add up every interface that is not on a fixed list of virtual names. On hosts where a second interface carries the same traffic, such as an SR-IOV function beside eth0, the dashboard and the bot reports counted it twice (#6906).

Add a traffic mode to General settings: legacy keeps today's sum, auto follows the default-route interface, manual names one. An interface that is not available shows as unavailable in the dashboard and in the Telegram and Discord reports instead of 0 B, and a saved manual choice is only re-checked when it changes.
2026-11-24 19:55:55 +03:30

58 lines
1.6 KiB
Go

package service
import (
stdnet "net"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)
func readNetworkDefaultRoutes(interfaces []stdnet.Interface) ([]networkDefaultRoute, error) {
routes, err := netlink.RouteListFiltered(netlink.FAMILY_ALL, &netlink.Route{Table: unix.RT_TABLE_MAIN}, netlink.RT_FILTER_TABLE)
if err != nil {
return nil, err
}
return networkRoutesFromNetlink(routes, interfaces), nil
}
func networkRoutesFromNetlink(routes []netlink.Route, interfaces []stdnet.Interface) []networkDefaultRoute {
names := make(map[int]string, len(interfaces))
for _, iface := range interfaces {
names[iface.Index] = iface.Name
}
var defaults []networkDefaultRoute
for _, route := range routes {
if route.Family != unix.AF_INET && route.Family != unix.AF_INET6 {
continue
}
if route.Table != unix.RT_TABLE_MAIN || route.Type != unix.RTN_UNICAST || route.Flags&unix.RTNH_F_LINKDOWN != 0 {
continue
}
if route.Dst != nil {
ones, bits := route.Dst.Mask.Size()
if ones != 0 || bits == 0 || !route.Dst.IP.IsUnspecified() {
continue
}
}
family := 4
if route.Family == unix.AF_INET6 {
family = 6
}
add := func(index, flags int) {
if flags&(unix.RTNH_F_DEAD|unix.RTNH_F_LINKDOWN) == 0 && names[index] != "" {
defaults = append(defaults, networkDefaultRoute{names[index], family, uint32(route.Priority)})
}
}
if len(route.MultiPath) == 0 {
add(route.LinkIndex, route.Flags)
} else {
for _, hop := range route.MultiPath {
if hop != nil {
add(hop.LinkIndex, hop.Flags)
}
}
}
}
return defaults
}