Connect to bird on demand, to survive bird restarts

This commit is contained in:
Lan Tian 2020-04-02 16:35:49 +08:00
parent ad8f22f17d
commit b430242fab
No known key found for this signature in database
GPG Key ID: 27F31700E751EC22
2 changed files with 21 additions and 39 deletions

View File

@ -4,17 +4,8 @@ import (
"io" "io"
"net" "net"
"net/http" "net/http"
"sync"
) )
// BIRDv4 connection & mutex lock
var bird net.Conn
var birdMutex = &sync.Mutex{}
// BIRDv6 connection & mutex lock
var bird6 net.Conn
var bird6Mutex = &sync.Mutex{}
// Read a line from bird socket, removing preceding status number, output it. // Read a line from bird socket, removing preceding status number, output it.
// Returns if there are more lines. // Returns if there are more lines.
func birdReadln(bird io.Reader, w io.Writer) bool { func birdReadln(bird io.Reader, w io.Writer) bool {
@ -83,10 +74,16 @@ func birdHandler(httpW http.ResponseWriter, httpR *http.Request) {
if query == "" { if query == "" {
invalidHandler(httpW, httpR) invalidHandler(httpW, httpR)
} else { } else {
birdMutex.Lock() // Initialize BIRDv4 socket
defer birdMutex.Unlock() bird, err := net.Dial("unix", setting.birdSocket)
if err != nil {
panic(err)
}
defer bird.Close()
println(query) birdReadln(bird, nil)
birdWriteln(bird, "restrict")
birdReadln(bird, nil)
birdWriteln(bird, query) birdWriteln(bird, query)
for birdReadln(bird, httpW) { for birdReadln(bird, httpW) {
} }
@ -99,10 +96,16 @@ func bird6Handler(httpW http.ResponseWriter, httpR *http.Request) {
if query == "" { if query == "" {
invalidHandler(httpW, httpR) invalidHandler(httpW, httpR)
} else { } else {
bird6Mutex.Lock() // Initialize BIRDv6 socket
defer bird6Mutex.Unlock() bird6, err := net.Dial("unix", setting.bird6Socket)
if err != nil {
panic(err)
}
defer bird6.Close()
println(query) birdReadln(bird6, nil)
birdWriteln(bird6, "restrict")
birdReadln(bird6, nil)
birdWriteln(bird6, query) birdWriteln(bird6, query)
for birdReadln(bird6, httpW) { for birdReadln(bird6, httpW) {
} }

View File

@ -2,7 +2,6 @@ package main
import ( import (
"flag" "flag"
"net"
"net/http" "net/http"
"os" "os"
) )
@ -28,8 +27,6 @@ var setting settingType
// Wrapper of tracer // Wrapper of tracer
func main() { func main() {
var err error
// Prepare default socket paths, use environment variable if possible // Prepare default socket paths, use environment variable if possible
var settingDefault = settingType{ var settingDefault = settingType{
"/var/run/bird/bird.ctl", "/var/run/bird/bird.ctl",
@ -53,27 +50,9 @@ func main() {
listenParam := flag.String("listen", settingDefault.listen, "listen address, set either in parameter or environment variable BIRDLG_LISTEN") listenParam := flag.String("listen", settingDefault.listen, "listen address, set either in parameter or environment variable BIRDLG_LISTEN")
flag.Parse() flag.Parse()
// Initialize BIRDv4 socket setting.birdSocket = *birdParam
bird, err = net.Dial("unix", *birdParam) setting.bird6Socket = *bird6Param
if err != nil { setting.listen = *listenParam
panic(err)
}
defer bird.Close()
birdReadln(bird, nil)
birdWriteln(bird, "restrict")
birdReadln(bird, nil)
// Initialize BIRDv6 socket
bird6, err = net.Dial("unix", *bird6Param)
if err != nil {
panic(err)
}
defer bird6.Close()
birdReadln(bird6, nil)
birdWriteln(bird6, "restrict")
birdReadln(bird6, nil)
// Start HTTP server // Start HTTP server
http.HandleFunc("/", invalidHandler) http.HandleFunc("/", invalidHandler)