package main import ( "net/http" "strings" "html" ) func webDispatcherIPv4Summary(w http.ResponseWriter, r *http.Request) { webHandler(w, r, false, r.URL.Path[len("/ipv4/summary/"):], "show protocols") } func webDispatcherIPv6Summary(w http.ResponseWriter, r *http.Request) { webHandler(w, r, true, r.URL.Path[len("/ipv6/summary/"):], "show protocols") } func webDispatcherIPv4Detail(w http.ResponseWriter, r *http.Request) { split := strings.Split(r.URL.Path[len("/ipv4/detail/"):], "/") webHandler(w, r, false, split[0], "show protocols all " + split[1]) } func webDispatcherIPv6Detail(w http.ResponseWriter, r *http.Request) { split := strings.Split(r.URL.Path[len("/ipv6/detail/"):], "/") webHandler(w, r, true, split[0], "show protocols all " + split[1]) } func webDispatcherIPv4Route(w http.ResponseWriter, r *http.Request) { split := strings.Split(r.URL.Path[len("/ipv4/route/"):], "/") webHandler(w, r, false, split[0], "show route for " + strings.Join(split[1:], "/")) } func webDispatcherIPv6Route(w http.ResponseWriter, r *http.Request) { split := strings.Split(r.URL.Path[len("/ipv6/route/"):], "/") webHandler(w, r, true, split[0], "show route for " + strings.Join(split[1:], "/")) } func webDispatcherIPv4RouteAll(w http.ResponseWriter, r *http.Request) { split := strings.Split(r.URL.Path[len("/ipv4/route_all/"):], "/") webHandler(w, r, false, split[0], "show route for " + strings.Join(split[1:], "/") + " all") } func webDispatcherIPv6RouteAll(w http.ResponseWriter, r *http.Request) { split := strings.Split(r.URL.Path[len("/ipv6/route_all/"):], "/") webHandler(w, r, true, split[0], "show route for " + strings.Join(split[1:], "/") + " all") } func webDispatcherWhois(w http.ResponseWriter, r *http.Request) { var target string = r.URL.Path[len("/whois/"):] templateHeader(w, r, "Bird-lg Go - WHOIS " + html.EscapeString(target)) w.Write([]byte("

whois " + html.EscapeString(target) + "

")) smartWriter(w, whois(target)) templateFooter(w) } func webHandler(w http.ResponseWriter, r *http.Request, isIPv6 bool, serverQuery string, command string) { templateHeader(w, r, "Bird-lg Go - " + html.EscapeString(command)) var servers []string = strings.Split(serverQuery, "+") var responses []string if isIPv6 { responses = batchRequest(servers, "bird6", command) } else { responses = batchRequest(servers, "bird", command) } for i, response := range responses { w.Write([]byte("

" + html.EscapeString(servers[i]) + ": " + html.EscapeString(command) + "

")) if command == "show protocols" && response[0:4] == "name" { summaryTable(w, isIPv6, response, servers[i]) } else { smartWriter(w, response) } } templateFooter(w) } func defaultRedirect(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/ipv4/summary/" + strings.Join(settingServers[:], "+"), 302) } func navbarFormRedirect(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() 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) } else { http.Redirect(w, r, "/" + query.Get("proto") + "/" + query.Get("action") + "/" + query.Get("server") + "/" + query.Get("target"), 302) } } func webServerStart() { // Start HTTP server http.HandleFunc("/", defaultRedirect) http.HandleFunc("/ipv4/summary/", webDispatcherIPv4Summary) http.HandleFunc("/ipv6/summary/", webDispatcherIPv6Summary) http.HandleFunc("/ipv4/detail/", webDispatcherIPv4Detail) http.HandleFunc("/ipv6/detail/", webDispatcherIPv6Detail) http.HandleFunc("/ipv4/route/", webDispatcherIPv4Route) http.HandleFunc("/ipv6/route/", webDispatcherIPv6Route) http.HandleFunc("/ipv4/route_all/", webDispatcherIPv4RouteAll) http.HandleFunc("/ipv6/route_all/", webDispatcherIPv6RouteAll) http.HandleFunc("/whois/", webDispatcherWhois) http.HandleFunc("/redir/", navbarFormRedirect) http.ListenAndServe(":5000", nil) }