fea: display protocol for prefix route.
Some checks failed
Coverage CI / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
Ubuntu CI / build (push) Has been cancelled

This commit is contained in:
Daniel Ding
2025-09-05 14:23:01 +08:00
parent 19829f1c5a
commit 6b041b62ce
5 changed files with 57 additions and 19 deletions

View File

@@ -19,9 +19,9 @@ func (r Prefix) Url(prefix string) string {
func (r Prefix) Tmpl() string {
return `# total {{ len . }}
{{ps -18 "destination"}} {{ps -15 "nexthop"}} {{ps -16 "link"}} {{ps -15 "source"}} {{"metric"}}
{{ps -18 "destination"}} {{ps -15 "nexthop"}} {{ps -16 "link"}} {{ps -15 "source"}} {{ps -6 "metric" }} {{ "protocol" }}
{{- range . }}
{{ps -18 .Prefix}} {{ps -15 .NextHop}} {{ps -16 .Link}} {{ps -15 .Source}} {{.Metric}}
{{ps -18 .Prefix}} {{ps -15 .NextHop}} {{ps -16 .Link}} {{ps -15 .Source}} {{pi -6 .Metric}} {{ .Protocol }}
{{- end }}
`
}

View File

@@ -24,12 +24,13 @@ func (l Prefix) List(w http.ResponseWriter, r *http.Request) {
routes, _ := libol.ListRoutes()
for _, prefix := range routes {
item := schema.PrefixRoute{
Link: prefix.Link,
Metric: prefix.Priority,
Table: prefix.Table,
Source: prefix.Src,
NextHop: prefix.Gw,
Prefix: prefix.Dst,
Link: prefix.Link,
Metric: prefix.Priority,
Table: prefix.Table,
Source: prefix.Src,
NextHop: prefix.Gw,
Prefix: prefix.Dst,
Protocol: prefix.Protocol,
}
items = append(items, item)

View File

@@ -1,6 +1,7 @@
package libol
import (
"fmt"
"net"
"github.com/vishvananda/netlink"
@@ -45,6 +46,44 @@ func GetLocalByGw(addr string) (net.IP, error) {
return local, nil
}
const (
RTPROT_BGP = 0xba
RTPROT_BIRD = 0xc
RTPROT_KERNEL = 0x2
RTPROT_BOOT = 0x3
RTPROT_DHCP = 0x10
RTPROT_ISIS = 0xbb
RTPROT_OSPF = 0xbc
RTPROT_REDIRECT = 0x1
RTPROT_RIP = 0xbd
RTPROT_STATIC = 0x4
RTPROT_UNSPEC = 0x0
RTPROT_ZEBRA = 0xb
)
func RouteProtocol(code int) string {
switch code {
case RTPROT_BGP:
return "bgp"
case RTPROT_KERNEL:
return "kernel"
case RTPROT_BOOT:
return "boot"
case RTPROT_DHCP:
return "dhcp"
case RTPROT_ISIS:
return "isis"
case RTPROT_OSPF:
return "ospf"
case RTPROT_RIP:
return "rip"
case RTPROT_STATIC:
return "static"
default:
return fmt.Sprintf("%d", code)
}
}
func ListRoutes() ([]Prefix, error) {
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err != nil {
@@ -53,17 +92,14 @@ func ListRoutes() ([]Prefix, error) {
var data []Prefix
for _, rte := range routes {
link, err := netlink.LinkByIndex(rte.LinkIndex)
if err != nil {
continue
}
entry := Prefix{
Protocol: rte.Protocol,
Protocol: RouteProtocol(rte.Protocol),
Priority: rte.Priority,
Link: link.Attrs().Name,
}
link, err := netlink.LinkByIndex(rte.LinkIndex)
if err == nil {
entry.Link = link.Attrs().Name
}
if rte.Dst == nil {
entry.Dst = "0.0.0.0/0"
} else {
@@ -71,13 +107,13 @@ func ListRoutes() ([]Prefix, error) {
}
if len(rte.Gw) == 0 {
entry.Gw = ""
entry.Gw = "0.0.0.0"
} else {
entry.Gw = rte.Gw.String()
}
if len(rte.Src) == 0 {
entry.Src = ""
entry.Src = "0.0.0.0"
} else {
entry.Src = rte.Src.String()
}

View File

@@ -5,7 +5,7 @@ type Prefix struct {
Dst string
Src string
Gw string
Protocol int
Protocol string
Priority int
Table int
}

View File

@@ -16,6 +16,7 @@ type PrefixRoute struct {
Link string `json:"link,omitempty"`
Table int `json:"table,omitempty"`
Source string `json:"source,omitempty"`
Protocol string `json:"protocol,omitempty"`
MultiPath []MultiPath `json:"multipath,omitempty"`
}