frontend: support listening on unix socket (#54)

This commit is contained in:
Yuhui Xu 2022-07-08 21:14:11 -04:00 committed by GitHub
parent 348295b9aa
commit 892a7bee22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"flag" "flag"
"net"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -101,18 +102,18 @@ func main() {
if env := os.Getenv("BIRDLG_NAME_FILTER"); env != "" { if env := os.Getenv("BIRDLG_NAME_FILTER"); env != "" {
settingDefault.nameFilter = env settingDefault.nameFilter = env
} }
if env := os.Getenv("BIRDLG_TIMEOUT"); env != "" { if env := os.Getenv("BIRDLG_TIMEOUT"); env != "" {
var err error var err error
if settingDefault.timeOut, err = strconv.Atoi(env); err != nil { if settingDefault.timeOut, err = strconv.Atoi(env); err != nil {
panic(err) panic(err)
} }
} }
serversPtr := flag.String("servers", strings.Join(settingDefault.servers, ","), "server name prefixes, separated by comma") serversPtr := flag.String("servers", strings.Join(settingDefault.servers, ","), "server name prefixes, separated by comma")
domainPtr := flag.String("domain", settingDefault.domain, "server name domain suffixes") domainPtr := flag.String("domain", settingDefault.domain, "server name domain suffixes")
proxyPortPtr := flag.Int("proxy-port", settingDefault.proxyPort, "port bird-lgproxy is running on") proxyPortPtr := flag.Int("proxy-port", settingDefault.proxyPort, "port bird-lgproxy is running on")
whoisPtr := flag.String("whois", settingDefault.whoisServer, "whois server for queries") 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") 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]") 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") titleBrandPtr := flag.String("title-brand", settingDefault.titleBrand, "prefix of page titles in browser tabs")
@ -132,11 +133,6 @@ func main() {
panic("no server set") panic("no server set")
} }
if !strings.Contains(*listenPtr, ":") {
listenHost := ":" + (*listenPtr)
listenPtr = &listenHost
}
servers := strings.Split(*serversPtr, ",") servers := strings.Split(*serversPtr, ",")
serversDisplay := strings.Split(*serversPtr, ",") serversDisplay := strings.Split(*serversPtr, ",")
@ -177,5 +173,25 @@ func main() {
} }
ImportTemplates() 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)
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"html" "html"
"io/fs" "io/fs"
"net"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -176,7 +177,7 @@ func webHandlerBGPMap(endpoint string, command string) func(w http.ResponseWrite
} }
// set up routing paths and start webserver // set up routing paths and start webserver
func webServerStart() { func webServerStart(l net.Listener) {
// redirect main page to all server summary // redirect main page to all server summary
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@ -211,5 +212,5 @@ func webServerStart() {
http.HandleFunc("/telegram/", webHandlerTelegramBot) http.HandleFunc("/telegram/", webHandlerTelegramBot)
// Start HTTP server // Start HTTP server
http.ListenAndServe(setting.listen, handlers.LoggingHandler(os.Stdout, http.DefaultServeMux)) http.Serve(l, handlers.LoggingHandler(os.Stdout, http.DefaultServeMux))
} }