Remove BIRDv1 support

This commit is contained in:
Lan Tian
2020-10-30 23:10:03 +08:00
parent 79431effb2
commit 3bcfc3d36c
12 changed files with 141 additions and 229 deletions

5
frontend/go.mod Normal file
View File

@@ -0,0 +1,5 @@
module github.com/xddxdd/bird-lg-go/frontend
go 1.15
require github.com/gorilla/handlers v1.5.1

4
frontend/go.sum Normal file
View File

@@ -0,0 +1,4 @@
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=

View File

@@ -9,20 +9,20 @@ import (
func renderTemplate(w http.ResponseWriter, r *http.Request, title string, content string) {
path := r.URL.Path[1:]
split := strings.SplitN(path, "/", 4)
split := strings.SplitN(path, "/", 3)
isWhois := strings.ToLower(split[0]) == "whois"
whoisTarget := strings.Join(split[1:], "/")
// Use a default URL if the request URL is too short
// The URL is for return to IPv4 summary page
if len(split) < 3 {
path = "ipv4/summary/" + strings.Join(setting.servers, "+") + "/"
} else if len(split) == 3 {
// The URL is for return to summary page
if len(split) < 2 {
path = "summary/" + strings.Join(setting.servers, "+") + "/"
} else if len(split) == 2 {
path += "/"
}
split = strings.SplitN(path, "/", 4)
split = strings.SplitN(path, "/", 3)
var args tmplArguments
args.Options = map[string]string{
@@ -40,24 +40,20 @@ func renderTemplate(w http.ResponseWriter, r *http.Request, title string, conten
"traceroute": "traceroute ...",
}
args.Servers = setting.servers
args.AllServersLinkActive = strings.ToLower(split[2]) == strings.ToLower(strings.Join(setting.servers, "+"))
args.AllServersLinkActive = strings.ToLower(split[1]) == strings.ToLower(strings.Join(setting.servers, "+"))
args.AllServersURL = strings.Join(setting.servers, "+")
args.IsWhois = isWhois
args.WhoisTarget = whoisTarget
args.URLProto = strings.ToLower(split[0])
args.URLOption = strings.ToLower(split[1])
args.URLServer = strings.ToLower(split[2])
args.URLCommand = split[3]
args.URLOption = strings.ToLower(split[0])
args.URLServer = strings.ToLower(split[1])
args.URLCommand = split[2]
args.Title = setting.titleBrand + title
args.Brand = setting.navBarBrand
args.Content = content
err := tmpl.Execute(w, args)
if err != nil {
panic(err)
}
tmpl.Execute(w, args)
}
// Write the given text to http response, and add whois links for
@@ -87,7 +83,7 @@ type summaryTableArguments struct {
}
// Output a table for the summary page
func summaryTable(isIPv6 bool, data string, serverName string) string {
func summaryTable(data string, serverName string) string {
var result string
// Sort the table, excluding title row
@@ -152,11 +148,7 @@ func summaryTable(isIPv6 bool, data string, serverName string) string {
"passive": "table-info",
})[row[3]] + `">`
// Add link to detail for first column
if isIPv6 {
result += `<td><a href="/ipv6/detail/` + serverName + `/` + row[0] + `">` + row[0] + `</a></td>`
} else {
result += `<td><a href="/ipv4/detail/` + serverName + `/` + row[0] + `">` + row[0] + `</a></td>`
}
result += `<td><a href="/detail/` + serverName + `/` + row[0] + `">` + row[0] + `</a></td>`
// Draw the other cells
for i := 1; i < 6; i++ {
result += "<td>" + row[i] + "</td>"

View File

@@ -87,17 +87,13 @@ func webHandlerTelegramBot(w http.ResponseWriter, r *http.Request) {
commandResult := ""
// - traceroute
if telegramIsCommand(request.Message.Text, "trace") || telegramIsCommand(request.Message.Text, "trace4") {
if telegramIsCommand(request.Message.Text, "trace") {
commandResult = telegramBatchRequestFormat(servers, "traceroute", target, telegramDefaultPostProcess)
} else if telegramIsCommand(request.Message.Text, "trace6") {
commandResult = telegramBatchRequestFormat(servers, "traceroute6", target, telegramDefaultPostProcess)
} else if telegramIsCommand(request.Message.Text, "route") || telegramIsCommand(request.Message.Text, "route4") {
} else if telegramIsCommand(request.Message.Text, "route") {
commandResult = telegramBatchRequestFormat(servers, "bird", "show route for "+target+" primary", telegramDefaultPostProcess)
} else if telegramIsCommand(request.Message.Text, "route6") {
commandResult = telegramBatchRequestFormat(servers, "bird6", "show route for "+target+" primary", telegramDefaultPostProcess)
} else if telegramIsCommand(request.Message.Text, "path") || telegramIsCommand(request.Message.Text, "path4") {
} else if telegramIsCommand(request.Message.Text, "path") {
commandResult = telegramBatchRequestFormat(servers, "bird", "show route for "+target+" all primary", func(result string) string {
for _, s := range strings.Split(result, "\n") {
if strings.Contains(s, "BGP.as_path: ") {
@@ -106,15 +102,6 @@ func webHandlerTelegramBot(w http.ResponseWriter, r *http.Request) {
}
return ""
})
} else if telegramIsCommand(request.Message.Text, "path6") {
commandResult = telegramBatchRequestFormat(servers, "bird6", "show route for "+target+" all primary", func(result string) string {
for _, s := range strings.Split(result, "\n") {
if strings.Contains(s, "BGP.as_path: ") {
return strings.TrimSpace(strings.Split(s, ":")[1])
}
}
return ""
})
} else if telegramIsCommand(request.Message.Text, "whois") {
if setting.netSpecificMode == "dn42" {
@@ -137,9 +124,9 @@ func webHandlerTelegramBot(w http.ResponseWriter, r *http.Request) {
} else if telegramIsCommand(request.Message.Text, "help") {
commandResult = `
/[path|path6] <IP>
/[route|route6] <IP>
/[trace|trace6] <IP>
/path <IP>
/route <IP>
/trace <IP>
/whois <Target>
`
} else {

View File

@@ -17,7 +17,6 @@ type tmplArguments struct {
IsWhois bool
WhoisTarget string
URLProto string
URLOption string
URLServer string
URLCommand string
@@ -49,34 +48,37 @@ var tmpl = template.Must(template.New("tmpl").Parse(`
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
{{ $option := .URLOption }}
{{ $server := .URLServer }}
{{ $target := .URLCommand }}
{{ if .IsWhois }}
{{ $option = "summary" }}
{{ $server = .AllServersURL }}
{{ $target = "" }}
{{ end }}
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a class="nav-link{{ if eq "ipv4" .URLProto }} active{{ end }}" href="/ipv4/{{ .URLOption }}/{{ .URLServer }}/{{ .URLCommand }}"> IPv4 </a></li>
<li class="nav-item"><a class="nav-link{{ if eq "ipv6" .URLProto }} active{{ end }}" href="/ipv6/{{ .URLOption }}/{{ .URLServer }}/{{ .URLCommand }}"> IPv6 </a></li>
<span class="navbar-text">|</span>
<li class="nav-item">
<a class="nav-link{{ if .AllServersLinkActive }} active{{ end }}" href="/{{ .URLProto }}/{{ .URLOption }}/{{ .AllServersURL }}/{{ .URLCommand }}"> All Servers </a>
<a class="nav-link{{ if .AllServersLinkActive }} active{{ end }}"
href="/{{ $option }}/{{ .AllServersURL }}/{{ $target }}"> All Servers </a>
</li>
{{ range $k, $v := .Servers }}
<li class="nav-item">
<a class="nav-link{{ if eq $.URLServer $v }} active{{ end }}" href="/{{ $.URLProto }}/{{ $.URLOption }}/{{ $v }}/{{ $.URLCommand }}">{{ $v }}</a>
<a class="nav-link{{ if eq $server $v }} active{{ end }}"
href="/{{ $option }}/{{ $v }}/{{ $target }}">{{ $v }}</a>
</li>
{{ end }}
</ul>
{{ $option := .URLOption }}
{{ $target := .URLCommand }}
{{ if .IsWhois }}
{{ $option = "whois" }}
{{ $target = .WhoisTarget }}
{{ end }}
<form class="form-inline" action="/redir" method="GET">
<div class="input-group">
<select name="action" class="form-control">
{{ range $k, $v := .Options }}
<option value="{{ $k }}"{{ if eq $k $option }} selected{{end}}>{{ $v }}</option>
<option value="{{ $k }}"{{ if eq $k $.URLOption }} selected{{end}}>{{ $v }}</option>
{{ end }}
</select>
<input name="proto" class="d-none" value="{{ .URLProto }}">
<input name="server" class="d-none" value="{{ .URLServer }}">
<input name="server" class="d-none" value="{{ $server }}">
<input name="target" class="form-control" placeholder="Target" aria-label="Target" value="{{ $target }}">
<div class="input-group-append">
<button class="btn btn-outline-success" type="submit">&raquo;</button>

View File

@@ -40,8 +40,8 @@ func webBackendCommunicator(endpoint string, command string) func(w http.Respons
return func(w http.ResponseWriter, r *http.Request) {
split := strings.SplitN(r.URL.Path[1:], "/", 4)
var urlCommands string
if len(split) >= 4 {
urlCommands = split[3]
if len(split) >= 3 {
urlCommands = split[2]
}
var backendCommand string
@@ -52,14 +52,13 @@ func webBackendCommunicator(endpoint string, command string) func(w http.Respons
}
backendCommand = strings.TrimSpace(backendCommand)
var servers []string = strings.Split(split[2], "+")
var servers []string = strings.Split(split[1], "+")
var responses []string = batchRequest(servers, endpoint, backendCommand)
var result string
for i, response := range responses {
result += "<h2>" + html.EscapeString(servers[i]) + ": " + html.EscapeString(backendCommand) + "</h2>"
if (endpoint == "bird" || endpoint == "bird6") && backendCommand == "show protocols" && len(response) > 4 && strings.ToLower(response[0:4]) == "name" {
var isIPv6 bool = endpoint[len(endpoint)-1] == '6'
result += summaryTable(isIPv6, response, servers[i])
if (endpoint == "bird") && backendCommand == "show protocols" && len(response) > 4 && strings.ToLower(response[0:4]) == "name" {
result += summaryTable(response, servers[i])
} else {
result += smartFormatter(response)
}
@@ -85,7 +84,7 @@ func webHandlerBGPMap(endpoint string, command string) func(w http.ResponseWrite
return func(w http.ResponseWriter, r *http.Request) {
split := strings.Split(r.URL.Path[1:], "/")
urlCommands := strings.Join(split[3:], "/")
urlCommands := strings.Join(split[2:], "/")
var backendCommand string
if strings.Contains(backendCommandPrimitive, "%") {
@@ -94,7 +93,7 @@ func webHandlerBGPMap(endpoint string, command string) func(w http.ResponseWrite
backendCommand = backendCommandPrimitive
}
var servers []string = strings.Split(split[2], "+")
var servers []string = strings.Split(split[1], "+")
var responses []string = batchRequest(servers, endpoint, backendCommand)
renderTemplate(
w, r,
@@ -121,41 +120,41 @@ func webHandlerNavbarFormRedirect(w http.ResponseWriter, r *http.Request) {
if query.Get("action") == "whois" {
http.Redirect(w, r, "/"+query.Get("action")+"/"+query.Get("target"), 302)
} else if query.Get("action") == "summary" {
http.Redirect(w, r, "/"+query.Get("proto")+"/"+query.Get("action")+"/"+query.Get("server"), 302)
http.Redirect(w, r, "/"+query.Get("action")+"/"+query.Get("server")+"/", 302)
} else {
http.Redirect(w, r, "/"+query.Get("proto")+"/"+query.Get("action")+"/"+query.Get("server")+"/"+query.Get("target"), 302)
http.Redirect(w, r, "/"+query.Get("action")+"/"+query.Get("server")+"/"+query.Get("target"), 302)
}
}
func webHandlerRobotsTxt(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("User-agent: *\nDisallow: /\n"))
}
func webHandler404(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 not found\n"))
}
func webServerStart() {
// Start HTTP server
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/ipv4/summary/"+strings.Join(setting.servers, "+"), 302)
http.Redirect(w, r, "/summary/"+strings.Join(setting.servers, "+"), 302)
})
http.HandleFunc("/ipv4/summary/", webBackendCommunicator("bird", "summary"))
http.HandleFunc("/ipv6/summary/", webBackendCommunicator("bird6", "summary"))
http.HandleFunc("/ipv4/detail/", webBackendCommunicator("bird", "detail"))
http.HandleFunc("/ipv6/detail/", webBackendCommunicator("bird6", "detail"))
http.HandleFunc("/ipv4/route/", webBackendCommunicator("bird", "route"))
http.HandleFunc("/ipv6/route/", webBackendCommunicator("bird6", "route"))
http.HandleFunc("/ipv4/route_all/", webBackendCommunicator("bird", "route_all"))
http.HandleFunc("/ipv6/route_all/", webBackendCommunicator("bird6", "route_all"))
http.HandleFunc("/ipv4/route_bgpmap/", webHandlerBGPMap("bird", "route_bgpmap"))
http.HandleFunc("/ipv6/route_bgpmap/", webHandlerBGPMap("bird6", "route_bgpmap"))
http.HandleFunc("/ipv4/route_where/", webBackendCommunicator("bird", "route_where"))
http.HandleFunc("/ipv6/route_where/", webBackendCommunicator("bird6", "route_where"))
http.HandleFunc("/ipv4/route_where_all/", webBackendCommunicator("bird", "route_where_all"))
http.HandleFunc("/ipv6/route_where_all/", webBackendCommunicator("bird6", "route_where_all"))
http.HandleFunc("/ipv4/route_where_bgpmap/", webHandlerBGPMap("bird", "route_where_bgpmap"))
http.HandleFunc("/ipv6/route_where_bgpmap/", webHandlerBGPMap("bird6", "route_where_bgpmap"))
http.HandleFunc("/ipv4/route_generic/", webBackendCommunicator("bird", "route_generic"))
http.HandleFunc("/ipv6/route_generic/", webBackendCommunicator("bird6", "route_generic"))
http.HandleFunc("/ipv4/generic/", webBackendCommunicator("bird", "generic"))
http.HandleFunc("/ipv6/generic/", webBackendCommunicator("bird6", "generic"))
http.HandleFunc("/ipv4/traceroute/", webBackendCommunicator("traceroute", "traceroute"))
http.HandleFunc("/ipv6/traceroute/", webBackendCommunicator("traceroute6", "traceroute"))
http.HandleFunc("/summary/", webBackendCommunicator("bird", "summary"))
http.HandleFunc("/detail/", webBackendCommunicator("bird", "detail"))
http.HandleFunc("/route/", webBackendCommunicator("bird", "route"))
http.HandleFunc("/route_all/", webBackendCommunicator("bird", "route_all"))
http.HandleFunc("/route_bgpmap/", webHandlerBGPMap("bird", "route_bgpmap"))
http.HandleFunc("/route_where/", webBackendCommunicator("bird", "route_where"))
http.HandleFunc("/route_where_all/", webBackendCommunicator("bird", "route_where_all"))
http.HandleFunc("/route_where_bgpmap/", webHandlerBGPMap("bird", "route_where_bgpmap"))
http.HandleFunc("/route_generic/", webBackendCommunicator("bird", "route_generic"))
http.HandleFunc("/generic/", webBackendCommunicator("bird", "generic"))
http.HandleFunc("/traceroute/", webBackendCommunicator("traceroute", "traceroute"))
http.HandleFunc("/whois/", webHandlerWhois)
http.HandleFunc("/redir", webHandlerNavbarFormRedirect)
http.HandleFunc("/telegram/", webHandlerTelegramBot)
http.HandleFunc("/robots.txt", webHandlerRobotsTxt)
http.HandleFunc("/favicon.ico", webHandler404)
http.ListenAndServe(setting.listen, handlers.LoggingHandler(os.Stdout, http.DefaultServeMux))
}