You've already forked bird-lg-go
mirror of
https://github.com/xddxdd/bird-lg-go
synced 2025-10-17 22:42:12 +02:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e9750a8278 | ||
![]() |
d40dd3a4d3 | ||
![]() |
ffdeeac06e | ||
![]() |
7eb4d75bbf | ||
![]() |
6e5e190d32 |
@@ -73,13 +73,14 @@ func TestApiSummaryHandler(t *testing.T) {
|
||||
|
||||
summary := response.Result[0].(*apiSummaryResultPair)
|
||||
assert.Equal(t, summary.Server, "alpha")
|
||||
assert.Equal(t, len(summary.Data), 7)
|
||||
// Protocol list will be sorted
|
||||
assert.Equal(t, summary.Data[1].Name, "device1")
|
||||
assert.Equal(t, summary.Data[1].Proto, "Device")
|
||||
assert.Equal(t, summary.Data[1].Table, "---")
|
||||
assert.Equal(t, summary.Data[1].State, "up")
|
||||
assert.Equal(t, summary.Data[1].Since, "2021-08-27")
|
||||
assert.Equal(t, summary.Data[1].Info, "")
|
||||
assert.Equal(t, summary.Data[0].Name, "device1")
|
||||
assert.Equal(t, summary.Data[0].Proto, "Device")
|
||||
assert.Equal(t, summary.Data[0].Table, "---")
|
||||
assert.Equal(t, summary.Data[0].State, "up")
|
||||
assert.Equal(t, summary.Data[0].Since, "2021-08-27")
|
||||
assert.Equal(t, summary.Data[0].Info, "")
|
||||
}
|
||||
|
||||
func TestApiSummaryHandlerError(t *testing.T) {
|
||||
|
@@ -35,15 +35,6 @@ var optionsMap = map[string]string{
|
||||
"traceroute": "traceroute ...",
|
||||
}
|
||||
|
||||
// pre-compiled regexp and constant statemap for summary rendering
|
||||
var splitSummaryLine = regexp.MustCompile(`(\w+)(\s+)(\w+)(\s+)([\w-]+)(\s+)(\w+)(\s+)([0-9\-\. :]+)(.*)`)
|
||||
var summaryStateMap = map[string]string{
|
||||
"up": "success",
|
||||
"down": "secondary",
|
||||
"start": "danger",
|
||||
"passive": "info",
|
||||
}
|
||||
|
||||
// render the page template
|
||||
func renderPageTemplate(w http.ResponseWriter, r *http.Request, title string, content template.HTML) {
|
||||
path := r.URL.Path[1:]
|
||||
@@ -143,63 +134,23 @@ func summaryParse(data string, serverName string) (TemplateSummary, error) {
|
||||
|
||||
// parse each line
|
||||
for _, line := range rows {
|
||||
|
||||
// Ignore empty lines
|
||||
line = strings.TrimSpace(line)
|
||||
if len(line) == 0 {
|
||||
row := SummaryRowDataFromLine(line)
|
||||
if row == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Parse a total of 6 columns from bird summary
|
||||
lineSplitted := splitSummaryLine.FindStringSubmatch(line)
|
||||
if lineSplitted == nil {
|
||||
// Filter row name
|
||||
if setting.nameFilter != "" && nameFilterRegexp.MatchString(row.Name) {
|
||||
continue
|
||||
}
|
||||
|
||||
var row SummaryRowData
|
||||
|
||||
if len(lineSplitted) >= 2 {
|
||||
row.Name = strings.TrimSpace(lineSplitted[1])
|
||||
if setting.nameFilter != "" && nameFilterRegexp.MatchString(row.Name) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if len(lineSplitted) >= 4 {
|
||||
row.Proto = strings.TrimSpace(lineSplitted[3])
|
||||
// Filter away unwanted protocol types, if setting.protocolFilter is non-empty
|
||||
found := false
|
||||
for _, protocol := range setting.protocolFilter {
|
||||
if strings.EqualFold(row.Proto, protocol) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(setting.protocolFilter) > 0 && !found {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if len(lineSplitted) >= 6 {
|
||||
row.Table = strings.TrimSpace(lineSplitted[5])
|
||||
}
|
||||
if len(lineSplitted) >= 8 {
|
||||
row.State = strings.TrimSpace(lineSplitted[7])
|
||||
row.MappedState = summaryStateMap[row.State]
|
||||
}
|
||||
if len(lineSplitted) >= 10 {
|
||||
row.Since = strings.TrimSpace(lineSplitted[9])
|
||||
}
|
||||
if len(lineSplitted) >= 11 {
|
||||
row.Info = strings.TrimSpace(lineSplitted[10])
|
||||
}
|
||||
|
||||
// Dynamic BGP session, show without any color
|
||||
if strings.Contains(row.Info, "Passive") {
|
||||
row.MappedState = summaryStateMap["passive"]
|
||||
// Filter away unwanted protocol types, if setting.protocolFilter is non-empty
|
||||
if len(setting.protocolFilter) > 0 && !row.ProtocolMatches(setting.protocolFilter) {
|
||||
continue
|
||||
}
|
||||
|
||||
// add to the result
|
||||
args.Rows = append(args.Rows, row)
|
||||
args.Rows = append(args.Rows, *row)
|
||||
}
|
||||
|
||||
return args, nil
|
||||
|
@@ -8,8 +8,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
const BirdSummaryData = `BIRD 2.0.8 ready.
|
||||
Name Proto Table State Since Info
|
||||
const BirdSummaryData = `Name Proto Table State Since Info
|
||||
static1 Static master4 up 2021-08-27
|
||||
static2 Static master6 up 2021-08-27
|
||||
device1 Device --- up 2021-08-27
|
||||
|
@@ -3,11 +3,13 @@ package main
|
||||
import (
|
||||
"embed"
|
||||
"html/template"
|
||||
"net/url"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// import templates and other assets
|
||||
//
|
||||
//go:embed assets
|
||||
var assets embed.FS
|
||||
|
||||
@@ -64,6 +66,47 @@ func (r SummaryRowData) NameContains(prefix string) bool {
|
||||
return strings.Contains(r.Name, prefix)
|
||||
}
|
||||
|
||||
func (r SummaryRowData) ProtocolMatches(protocols []string) bool {
|
||||
for _, protocol := range protocols {
|
||||
if strings.EqualFold(r.Proto, protocol) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// pre-compiled regexp and constant statemap for summary rendering
|
||||
var splitSummaryLine = regexp.MustCompile(`^([\w-]+)\s+(\w+)\s+([\w-]+)\s+(\w+)\s+([0-9\-\. :]+)(.*)$`)
|
||||
var summaryStateMap = map[string]string{
|
||||
"up": "success",
|
||||
"down": "secondary",
|
||||
"start": "danger",
|
||||
"passive": "info",
|
||||
}
|
||||
|
||||
func SummaryRowDataFromLine(line string) *SummaryRowData {
|
||||
lineSplitted := splitSummaryLine.FindStringSubmatch(line)
|
||||
if lineSplitted == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var row SummaryRowData
|
||||
row.Name = strings.TrimSpace(lineSplitted[1])
|
||||
row.Proto = strings.TrimSpace(lineSplitted[2])
|
||||
row.Table = strings.TrimSpace(lineSplitted[3])
|
||||
row.State = strings.TrimSpace(lineSplitted[4])
|
||||
row.Since = strings.TrimSpace(lineSplitted[5])
|
||||
row.Info = strings.TrimSpace(lineSplitted[6])
|
||||
|
||||
if strings.Contains(row.Info, "Passive") {
|
||||
row.MappedState = summaryStateMap["passive"]
|
||||
} else {
|
||||
row.MappedState = summaryStateMap[row.State]
|
||||
}
|
||||
|
||||
return &row
|
||||
}
|
||||
|
||||
type TemplateSummary struct {
|
||||
ServerName string
|
||||
Raw string
|
||||
@@ -108,7 +151,7 @@ var requiredTemplates = [...]string{
|
||||
// define functions to be made available in templates
|
||||
|
||||
var funcMap = template.FuncMap{
|
||||
"pathescape": url.PathEscape,
|
||||
"pathescape": url.PathEscape,
|
||||
}
|
||||
|
||||
// import templates from embedded assets
|
||||
|
@@ -23,3 +23,67 @@ func TestSummaryRowDataNameContains(t *testing.T) {
|
||||
assert.Equal(t, data.NameContains("oc"), true)
|
||||
assert.Equal(t, data.NameContains("no"), false)
|
||||
}
|
||||
|
||||
func TestSummaryRowDataFromLine(t *testing.T) {
|
||||
data := SummaryRowDataFromLine("sys_device Device --- up 2025-06-27 21:23:08")
|
||||
|
||||
assert.Equal(t, data.Name, "sys_device")
|
||||
assert.Equal(t, data.Proto, "Device")
|
||||
assert.Equal(t, data.Table, "---")
|
||||
assert.Equal(t, data.State, "up")
|
||||
assert.Equal(t, data.Since, "2025-06-27 21:23:08")
|
||||
}
|
||||
|
||||
func TestSummaryRowDataFromLineNumeric(t *testing.T) {
|
||||
data := SummaryRowDataFromLine("12345 Device --- up 2025-06-27 21:23:08")
|
||||
|
||||
assert.Equal(t, data.Name, "12345")
|
||||
assert.Equal(t, data.Proto, "Device")
|
||||
assert.Equal(t, data.Table, "---")
|
||||
assert.Equal(t, data.State, "up")
|
||||
assert.Equal(t, data.Since, "2025-06-27 21:23:08")
|
||||
}
|
||||
|
||||
func TestSummaryRowDataFromLinePipe(t *testing.T) {
|
||||
data := SummaryRowDataFromLine("pipe Pipe --- up 2025-06-27 21:23:08 master4 <=> pipe_v4")
|
||||
|
||||
assert.Equal(t, data.Name, "pipe")
|
||||
assert.Equal(t, data.Proto, "Pipe")
|
||||
assert.Equal(t, data.Table, "---")
|
||||
assert.Equal(t, data.State, "up")
|
||||
assert.Equal(t, data.Since, "2025-06-27 21:23:08")
|
||||
assert.Equal(t, data.Info, "master4 <=> pipe_v4")
|
||||
}
|
||||
|
||||
func TestSummaryRowDataFromLineBGP(t *testing.T) {
|
||||
data := SummaryRowDataFromLine("bgp BGP --- up 2025-06-30 20:45:33 Established")
|
||||
|
||||
assert.Equal(t, data.Name, "bgp")
|
||||
assert.Equal(t, data.Proto, "BGP")
|
||||
assert.Equal(t, data.Table, "---")
|
||||
assert.Equal(t, data.State, "up")
|
||||
assert.Equal(t, data.Since, "2025-06-30 20:45:33")
|
||||
assert.Equal(t, data.Info, "Established")
|
||||
}
|
||||
|
||||
func TestSummaryRowDataFromLineBGPPassive(t *testing.T) {
|
||||
data := SummaryRowDataFromLine("passive BGP --- start 2025-06-27 21:23:08 Passive")
|
||||
|
||||
assert.Equal(t, data.Name, "passive")
|
||||
assert.Equal(t, data.Proto, "BGP")
|
||||
assert.Equal(t, data.Table, "---")
|
||||
assert.Equal(t, data.State, "start")
|
||||
assert.Equal(t, data.Since, "2025-06-27 21:23:08")
|
||||
assert.Equal(t, data.Info, "Passive")
|
||||
}
|
||||
|
||||
func TestSummaryRowDataFromLineWithDash(t *testing.T) {
|
||||
data := SummaryRowDataFromLine("ibgp_test-01 BGP --- up 07:16:51.656 Established")
|
||||
|
||||
assert.Equal(t, data.Name, "ibgp_test-01")
|
||||
assert.Equal(t, data.Proto, "BGP")
|
||||
assert.Equal(t, data.Table, "---")
|
||||
assert.Equal(t, data.State, "up")
|
||||
assert.Equal(t, data.Since, "07:16:51.656")
|
||||
assert.Equal(t, data.Info, "Established")
|
||||
}
|
||||
|
@@ -18,13 +18,13 @@ import (
|
||||
|
||||
var primitiveMap = map[string]string{
|
||||
"summary": "show protocols",
|
||||
"detail": "show protocols all %s",
|
||||
"route_from_protocol": "show route protocol %s",
|
||||
"route_from_protocol_all": "show route protocol %s all",
|
||||
"route_from_protocol_primary": "show route protocol %s primary",
|
||||
"route_from_protocol_all_primary": "show route protocol %s all primary",
|
||||
"route_filtered_from_protocol": "show route filtered protocol %s",
|
||||
"route_filtered_from_protocol_all": "show route filtered protocol %s all",
|
||||
"detail": "show protocols all \"%s\"",
|
||||
"route_from_protocol": "show route protocol \"%s\"",
|
||||
"route_from_protocol_all": "show route protocol \"%s\" all",
|
||||
"route_from_protocol_primary": "show route protocol \"%s\" primary",
|
||||
"route_from_protocol_all_primary": "show route protocol \"%s\" all primary",
|
||||
"route_filtered_from_protocol": "show route filtered protocol \"%s\"",
|
||||
"route_filtered_from_protocol_all": "show route filtered protocol \"%s\" all",
|
||||
"route_from_origin": "show route where bgp_path.last = %s",
|
||||
"route_from_origin_all": "show route where bgp_path.last = %s all",
|
||||
"route_from_origin_primary": "show route where bgp_path.last = %s primary",
|
||||
|
@@ -88,7 +88,7 @@ func TestWhoisWithoutServer(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWhoisConnectionError(t *testing.T) {
|
||||
setting.whoisServer = "127.0.0.1:0"
|
||||
setting.whoisServer = "127.0.0.1:1"
|
||||
result := whois("AS6939")
|
||||
if !strings.Contains(result, "connect: connection refused") {
|
||||
t.Errorf("Whois AS6939 without server produced output, got %s", result)
|
||||
|
Reference in New Issue
Block a user