diff --git a/proxy/bird.go b/proxy/bird.go index 51a8d13..14a5015 100644 --- a/proxy/bird.go +++ b/proxy/bird.go @@ -4,17 +4,8 @@ import ( "io" "net" "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. // Returns if there are more lines. func birdReadln(bird io.Reader, w io.Writer) bool { @@ -83,10 +74,16 @@ func birdHandler(httpW http.ResponseWriter, httpR *http.Request) { if query == "" { invalidHandler(httpW, httpR) } else { - birdMutex.Lock() - defer birdMutex.Unlock() + // Initialize BIRDv4 socket + 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) for birdReadln(bird, httpW) { } @@ -99,10 +96,16 @@ func bird6Handler(httpW http.ResponseWriter, httpR *http.Request) { if query == "" { invalidHandler(httpW, httpR) } else { - bird6Mutex.Lock() - defer bird6Mutex.Unlock() + // Initialize BIRDv6 socket + 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) for birdReadln(bird6, httpW) { } diff --git a/proxy/main.go b/proxy/main.go index 192f1fd..cfdb10e 100644 --- a/proxy/main.go +++ b/proxy/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "net" "net/http" "os" ) @@ -28,8 +27,6 @@ var setting settingType // Wrapper of tracer func main() { - var err error - // Prepare default socket paths, use environment variable if possible var settingDefault = settingType{ "/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") flag.Parse() - // Initialize BIRDv4 socket - bird, err = net.Dial("unix", *birdParam) - if err != nil { - 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) + setting.birdSocket = *birdParam + setting.bird6Socket = *bird6Param + setting.listen = *listenParam // Start HTTP server http.HandleFunc("/", invalidHandler)