2016-10-23 09:44:42 +02:00
|
|
|
package main
|
|
|
|
|
2016-10-23 09:51:40 +02:00
|
|
|
import (
|
2016-10-23 14:02:34 +02:00
|
|
|
"flag"
|
2016-10-23 09:51:40 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2016-11-30 15:19:01 +01:00
|
|
|
"strings"
|
2016-10-23 14:02:34 +02:00
|
|
|
|
2016-11-11 16:02:46 +01:00
|
|
|
"github.com/ecix/birdwatcher/bird"
|
2016-11-11 15:33:08 +01:00
|
|
|
"github.com/ecix/birdwatcher/endpoints"
|
2016-11-30 13:40:34 +01:00
|
|
|
|
2016-10-23 14:02:34 +02:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2016-10-23 09:51:40 +02:00
|
|
|
)
|
|
|
|
|
2017-01-26 12:12:02 +01:00
|
|
|
//go:generate versionize
|
2017-07-13 12:16:53 +02:00
|
|
|
var VERSION = "1.10.2"
|
2017-01-26 12:12:02 +01:00
|
|
|
|
2016-12-06 13:17:43 +01:00
|
|
|
func isModuleEnabled(module string, modulesEnabled []string) bool {
|
|
|
|
for _, enabled := range modulesEnabled {
|
|
|
|
if enabled == module {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeRouter(config endpoints.ServerConfig) *httprouter.Router {
|
|
|
|
whitelist := config.ModulesEnabled
|
|
|
|
|
2016-11-11 14:14:38 +01:00
|
|
|
r := httprouter.New()
|
2016-12-06 13:17:43 +01:00
|
|
|
if isModuleEnabled("status", whitelist) {
|
2017-02-15 12:20:55 +01:00
|
|
|
r.GET("/version", endpoints.Version(VERSION))
|
2016-12-06 13:17:43 +01:00
|
|
|
r.GET("/status", endpoints.Endpoint(endpoints.Status))
|
|
|
|
}
|
|
|
|
if isModuleEnabled("protocols", whitelist) {
|
|
|
|
r.GET("/protocols", endpoints.Endpoint(endpoints.Protocols))
|
|
|
|
}
|
|
|
|
if isModuleEnabled("protocols_bgp", whitelist) {
|
|
|
|
r.GET("/protocols/bgp", endpoints.Endpoint(endpoints.Bgp))
|
|
|
|
}
|
|
|
|
if isModuleEnabled("symbols", whitelist) {
|
|
|
|
r.GET("/symbols", endpoints.Endpoint(endpoints.Symbols))
|
|
|
|
}
|
|
|
|
if isModuleEnabled("symbols_tables", whitelist) {
|
|
|
|
r.GET("/symbols/tables", endpoints.Endpoint(endpoints.SymbolTables))
|
|
|
|
}
|
|
|
|
if isModuleEnabled("symbols_protocols", whitelist) {
|
|
|
|
r.GET("/symbols/protocols", endpoints.Endpoint(endpoints.SymbolProtocols))
|
|
|
|
}
|
|
|
|
if isModuleEnabled("routes_protocol", whitelist) {
|
|
|
|
r.GET("/routes/protocol/:protocol", endpoints.Endpoint(endpoints.ProtoRoutes))
|
|
|
|
}
|
|
|
|
if isModuleEnabled("routes_table", whitelist) {
|
|
|
|
r.GET("/routes/table/:table", endpoints.Endpoint(endpoints.TableRoutes))
|
|
|
|
}
|
|
|
|
if isModuleEnabled("routes_count_protocol", whitelist) {
|
|
|
|
r.GET("/routes/count/protocol/:protocol", endpoints.Endpoint(endpoints.ProtoCount))
|
|
|
|
}
|
|
|
|
if isModuleEnabled("routes_count_table", whitelist) {
|
|
|
|
r.GET("/routes/count/table/:table", endpoints.Endpoint(endpoints.TableCount))
|
|
|
|
}
|
2016-12-06 17:20:27 +01:00
|
|
|
if isModuleEnabled("routes_filtered", whitelist) {
|
2016-12-09 11:49:45 +01:00
|
|
|
r.GET("/routes/filtered/:protocol", endpoints.Endpoint(endpoints.RoutesFiltered))
|
2016-12-06 17:20:27 +01:00
|
|
|
}
|
2017-04-07 11:14:03 +02:00
|
|
|
if isModuleEnabled("routes_noexport", whitelist) {
|
2017-04-07 11:18:51 +02:00
|
|
|
r.GET("/routes/noexport/:protocol", endpoints.Endpoint(endpoints.RoutesNoExport))
|
2017-04-07 11:14:03 +02:00
|
|
|
}
|
2016-12-08 11:09:25 +01:00
|
|
|
if isModuleEnabled("routes_prefixed", whitelist) {
|
2016-12-15 14:42:37 +01:00
|
|
|
r.GET("/routes/prefix", endpoints.Endpoint(endpoints.RoutesPrefixed))
|
2016-12-08 11:09:25 +01:00
|
|
|
}
|
2016-12-06 13:17:43 +01:00
|
|
|
if isModuleEnabled("route_net", whitelist) {
|
|
|
|
r.GET("/route/net/:net", endpoints.Endpoint(endpoints.RouteNet))
|
|
|
|
r.GET("/route/net/:net/table/:table", endpoints.Endpoint(endpoints.RouteNetTable))
|
|
|
|
}
|
2017-02-15 12:20:55 +01:00
|
|
|
if isModuleEnabled("routes_peer", whitelist) {
|
|
|
|
r.GET("/routes/peer", endpoints.Endpoint(endpoints.RoutesPeer))
|
|
|
|
}
|
2017-06-22 15:06:54 +02:00
|
|
|
if isModuleEnabled("routes_dump", whitelist) {
|
|
|
|
r.GET("/routes/dump", endpoints.Endpoint(endpoints.RoutesDump))
|
|
|
|
}
|
2016-11-11 14:14:38 +01:00
|
|
|
return r
|
2016-10-23 14:02:34 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 12:55:22 +01:00
|
|
|
// Print service information like, listen address,
|
|
|
|
// access restrictions and configuration flags
|
2016-11-30 13:40:34 +01:00
|
|
|
func PrintServiceInfo(conf *Config, birdConf bird.BirdConfig) {
|
2016-11-30 12:55:22 +01:00
|
|
|
// General Info
|
|
|
|
log.Println("Starting Birdwatcher")
|
2016-12-06 13:17:43 +01:00
|
|
|
log.Println(" Using:", birdConf.BirdCmd)
|
|
|
|
log.Println(" Listen:", birdConf.Listen)
|
2016-11-30 15:19:01 +01:00
|
|
|
|
|
|
|
// Endpoint Info
|
|
|
|
if len(conf.Server.AllowFrom) == 0 {
|
2016-12-06 13:17:43 +01:00
|
|
|
log.Println(" AllowFrom: ALL")
|
2016-11-30 15:19:01 +01:00
|
|
|
} else {
|
2016-12-06 13:17:43 +01:00
|
|
|
log.Println(" AllowFrom:", strings.Join(conf.Server.AllowFrom, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(" ModulesEnabled:")
|
|
|
|
for _, m := range conf.Server.ModulesEnabled {
|
|
|
|
log.Println(" -", m)
|
2016-11-30 15:19:01 +01:00
|
|
|
}
|
2017-05-29 09:55:37 +02:00
|
|
|
|
|
|
|
log.Println(" Per Peer Tables:", conf.Parser.PerPeerTables)
|
2017-07-17 11:53:23 +02:00
|
|
|
if conf.Parser.PerPeerTables == true {
|
|
|
|
log.Println(" - Match:", conf.Parser.PeerProtocolMatch)
|
|
|
|
log.Println(" - Template:", conf.Parser.PipeProtocolTemplate)
|
|
|
|
}
|
2016-11-30 12:55:22 +01:00
|
|
|
}
|
|
|
|
|
2016-10-23 09:44:42 +02:00
|
|
|
func main() {
|
2016-11-30 12:55:22 +01:00
|
|
|
bird6 := flag.Bool("6", false, "Use bird6 instead of bird")
|
2017-02-22 18:49:17 +01:00
|
|
|
configfile := flag.String("config", "./etc/ecix/birdwatcher.conf", "Configuration file location")
|
2016-10-23 14:02:34 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2017-01-26 12:12:02 +01:00
|
|
|
endpoints.VERSION = VERSION
|
2016-11-25 15:50:59 +01:00
|
|
|
bird.InstallRateLimitReset()
|
2016-11-30 12:55:22 +01:00
|
|
|
// Load configurations
|
2017-02-22 18:49:17 +01:00
|
|
|
conf, err := LoadConfigs(ConfigOptions(*configfile))
|
2016-11-30 12:55:22 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Loading birdwatcher configuration failed:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get config according to flags
|
|
|
|
birdConf := conf.Bird
|
|
|
|
if *bird6 {
|
|
|
|
birdConf = conf.Bird6
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintServiceInfo(conf, birdConf)
|
|
|
|
|
2016-11-30 15:19:01 +01:00
|
|
|
// Configuration
|
2016-12-02 17:07:30 +01:00
|
|
|
bird.ClientConf = birdConf
|
|
|
|
bird.StatusConf = conf.Status
|
2016-12-13 11:01:28 +01:00
|
|
|
bird.RateLimitConf.Conf = conf.Ratelimit
|
2017-02-22 18:09:45 +01:00
|
|
|
bird.ParserConf = conf.Parser
|
2016-11-30 15:19:01 +01:00
|
|
|
endpoints.Conf = conf.Server
|
2016-11-11 16:00:36 +01:00
|
|
|
|
2016-11-30 13:21:40 +01:00
|
|
|
// Make server
|
2016-12-06 13:17:43 +01:00
|
|
|
r := makeRouter(conf.Server)
|
2016-11-30 13:21:40 +01:00
|
|
|
log.Fatal(http.ListenAndServe(birdConf.Listen, r))
|
2016-10-23 09:44:42 +02:00
|
|
|
}
|