From 892a7bee22a1bb02d3b4da6d270c65b6e4e1321a Mon Sep 17 00:00:00 2001 From: Yuhui Xu Date: Fri, 8 Jul 2022 21:14:11 -0400 Subject: [PATCH] frontend: support listening on unix socket (#54) --- frontend/main.go | 42 +++++++++++++++++++++++++++++------------- frontend/webserver.go | 5 +++-- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/frontend/main.go b/frontend/main.go index 979605f..f91c618 100644 --- a/frontend/main.go +++ b/frontend/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "net" "os" "strconv" "strings" @@ -101,18 +102,18 @@ func main() { if env := os.Getenv("BIRDLG_NAME_FILTER"); env != "" { settingDefault.nameFilter = env } - if env := os.Getenv("BIRDLG_TIMEOUT"); env != "" { - var err error - if settingDefault.timeOut, err = strconv.Atoi(env); err != nil { - panic(err) - } - } + if env := os.Getenv("BIRDLG_TIMEOUT"); env != "" { + var err error + if settingDefault.timeOut, err = strconv.Atoi(env); err != nil { + panic(err) + } + } serversPtr := flag.String("servers", strings.Join(settingDefault.servers, ","), "server name prefixes, separated by comma") domainPtr := flag.String("domain", settingDefault.domain, "server name domain suffixes") proxyPortPtr := flag.Int("proxy-port", settingDefault.proxyPort, "port bird-lgproxy is running on") whoisPtr := flag.String("whois", settingDefault.whoisServer, "whois server for queries") - listenPtr := flag.String("listen", settingDefault.listen, "address bird-lg is listening on") + listenPtr := flag.String("listen", settingDefault.listen, "address or unix socket bird-lg is listening on") dnsInterfacePtr := flag.String("dns-interface", settingDefault.dnsInterface, "dns zone to query ASN information") netSpecificModePtr := flag.String("net-specific-mode", settingDefault.netSpecificMode, "network specific operation mode, [(none)|dn42]") titleBrandPtr := flag.String("title-brand", settingDefault.titleBrand, "prefix of page titles in browser tabs") @@ -132,11 +133,6 @@ func main() { panic("no server set") } - if !strings.Contains(*listenPtr, ":") { - listenHost := ":" + (*listenPtr) - listenPtr = &listenHost - } - servers := strings.Split(*serversPtr, ",") serversDisplay := strings.Split(*serversPtr, ",") @@ -177,5 +173,25 @@ func main() { } ImportTemplates() - webServerStart() + + var l net.Listener + var err error + + if strings.HasPrefix(setting.listen, "/") { + // Delete existing socket file, ignore errors (will fail later anyway) + os.Remove(setting.listen) + l, err = net.Listen("unix", setting.listen) + } else { + listenAddr := setting.listen + if !strings.Contains(listenAddr, ":") { + listenAddr = ":" + listenAddr + } + l, err = net.Listen("tcp", listenAddr) + } + + if err != nil { + panic(err) + } + + webServerStart(l) } diff --git a/frontend/webserver.go b/frontend/webserver.go index 926978d..9bd589a 100644 --- a/frontend/webserver.go +++ b/frontend/webserver.go @@ -5,6 +5,7 @@ import ( "fmt" "html" "io/fs" + "net" "net/http" "net/url" "os" @@ -176,7 +177,7 @@ func webHandlerBGPMap(endpoint string, command string) func(w http.ResponseWrite } // set up routing paths and start webserver -func webServerStart() { +func webServerStart(l net.Listener) { // redirect main page to all server summary http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -211,5 +212,5 @@ func webServerStart() { http.HandleFunc("/telegram/", webHandlerTelegramBot) // Start HTTP server - http.ListenAndServe(setting.listen, handlers.LoggingHandler(os.Stdout, http.DefaultServeMux)) + http.Serve(l, handlers.LoggingHandler(os.Stdout, http.DefaultServeMux)) }