mirror of
https://github.com/luscis/openlan.git
synced 2025-09-27 12:52:16 +08:00
fea: display protocol for prefix route.
This commit is contained in:
@@ -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 }}
|
||||
`
|
||||
}
|
||||
|
@@ -30,6 +30,7 @@ func (l Prefix) List(w http.ResponseWriter, r *http.Request) {
|
||||
Source: prefix.Src,
|
||||
NextHop: prefix.Gw,
|
||||
Prefix: prefix.Dst,
|
||||
Protocol: prefix.Protocol,
|
||||
}
|
||||
items = append(items, item)
|
||||
|
||||
|
@@ -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()
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@ type Prefix struct {
|
||||
Dst string
|
||||
Src string
|
||||
Gw string
|
||||
Protocol int
|
||||
Protocol string
|
||||
Priority int
|
||||
Table int
|
||||
}
|
||||
|
@@ -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"`
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user