mirror of
https://github.com/luscis/openlan.git
synced 2025-10-05 08:36:59 +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 {
|
func (r Prefix) Tmpl() string {
|
||||||
return `# total {{ len . }}
|
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 . }}
|
{{- 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 }}
|
{{- end }}
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
@@ -24,12 +24,13 @@ func (l Prefix) List(w http.ResponseWriter, r *http.Request) {
|
|||||||
routes, _ := libol.ListRoutes()
|
routes, _ := libol.ListRoutes()
|
||||||
for _, prefix := range routes {
|
for _, prefix := range routes {
|
||||||
item := schema.PrefixRoute{
|
item := schema.PrefixRoute{
|
||||||
Link: prefix.Link,
|
Link: prefix.Link,
|
||||||
Metric: prefix.Priority,
|
Metric: prefix.Priority,
|
||||||
Table: prefix.Table,
|
Table: prefix.Table,
|
||||||
Source: prefix.Src,
|
Source: prefix.Src,
|
||||||
NextHop: prefix.Gw,
|
NextHop: prefix.Gw,
|
||||||
Prefix: prefix.Dst,
|
Prefix: prefix.Dst,
|
||||||
|
Protocol: prefix.Protocol,
|
||||||
}
|
}
|
||||||
items = append(items, item)
|
items = append(items, item)
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package libol
|
package libol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
@@ -45,6 +46,44 @@ func GetLocalByGw(addr string) (net.IP, error) {
|
|||||||
return local, nil
|
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) {
|
func ListRoutes() ([]Prefix, error) {
|
||||||
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
|
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -53,17 +92,14 @@ func ListRoutes() ([]Prefix, error) {
|
|||||||
|
|
||||||
var data []Prefix
|
var data []Prefix
|
||||||
for _, rte := range routes {
|
for _, rte := range routes {
|
||||||
link, err := netlink.LinkByIndex(rte.LinkIndex)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := Prefix{
|
entry := Prefix{
|
||||||
Protocol: rte.Protocol,
|
Protocol: RouteProtocol(rte.Protocol),
|
||||||
Priority: rte.Priority,
|
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 {
|
if rte.Dst == nil {
|
||||||
entry.Dst = "0.0.0.0/0"
|
entry.Dst = "0.0.0.0/0"
|
||||||
} else {
|
} else {
|
||||||
@@ -71,13 +107,13 @@ func ListRoutes() ([]Prefix, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(rte.Gw) == 0 {
|
if len(rte.Gw) == 0 {
|
||||||
entry.Gw = ""
|
entry.Gw = "0.0.0.0"
|
||||||
} else {
|
} else {
|
||||||
entry.Gw = rte.Gw.String()
|
entry.Gw = rte.Gw.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(rte.Src) == 0 {
|
if len(rte.Src) == 0 {
|
||||||
entry.Src = ""
|
entry.Src = "0.0.0.0"
|
||||||
} else {
|
} else {
|
||||||
entry.Src = rte.Src.String()
|
entry.Src = rte.Src.String()
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@ type Prefix struct {
|
|||||||
Dst string
|
Dst string
|
||||||
Src string
|
Src string
|
||||||
Gw string
|
Gw string
|
||||||
Protocol int
|
Protocol string
|
||||||
Priority int
|
Priority int
|
||||||
Table int
|
Table int
|
||||||
}
|
}
|
||||||
|
@@ -16,6 +16,7 @@ type PrefixRoute struct {
|
|||||||
Link string `json:"link,omitempty"`
|
Link string `json:"link,omitempty"`
|
||||||
Table int `json:"table,omitempty"`
|
Table int `json:"table,omitempty"`
|
||||||
Source string `json:"source,omitempty"`
|
Source string `json:"source,omitempty"`
|
||||||
|
Protocol string `json:"protocol,omitempty"`
|
||||||
MultiPath []MultiPath `json:"multipath,omitempty"`
|
MultiPath []MultiPath `json:"multipath,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user