From ad17c52bb9238189852aa4b95ce7ad3e14855b36 Mon Sep 17 00:00:00 2001 From: Lan Tian Date: Fri, 27 Mar 2020 12:21:30 +0800 Subject: [PATCH] Improve parameter handling --- frontend/lgproxy.go | 4 +- frontend/main.go | 103 ++++++++++++++++++++++-------------------- frontend/template.go | 8 ++-- frontend/webserver.go | 4 +- frontend/whois.go | 10 ++-- proxy/main.go | 28 ++++++++---- 6 files changed, 85 insertions(+), 72 deletions(-) diff --git a/frontend/lgproxy.go b/frontend/lgproxy.go index 06ec203..3e1142b 100644 --- a/frontend/lgproxy.go +++ b/frontend/lgproxy.go @@ -21,7 +21,7 @@ func batchRequest(servers []string, endpoint string, command string) []string { for i, server := range servers { // Check if the server is in the valid server list passed at startup var isValidServer bool = false - for _, validServer := range settingServers { + for _, validServer := range setting.servers { if validServer == server { isValidServer = true break @@ -35,7 +35,7 @@ func batchRequest(servers []string, endpoint string, command string) []string { }(i) } else { // Compose URL and send the request - url := "http://" + server + "." + settingServersDomain + ":" + strconv.Itoa(settingServersPort) + "/" + url.PathEscape(endpoint) + "?q=" + url.QueryEscape(command) + url := "http://" + server + "." + setting.domain + ":" + strconv.Itoa(setting.proxyPort) + "/" + url.PathEscape(endpoint) + "?q=" + url.QueryEscape(command) go func(url string, i int) { response, err := http.Get(url) if err != nil { diff --git a/frontend/main.go b/frontend/main.go index fc25adf..a793663 100644 --- a/frontend/main.go +++ b/frontend/main.go @@ -1,63 +1,66 @@ package main import ( - "os" - "flag" - "strings" - "strconv" + "flag" + "os" + "strconv" + "strings" ) -var settingServers []string -var settingServersDomain string -var settingServersPort int -var settingWhoisServer string -var settingListen string +type settingType struct { + servers []string + domain string + proxyPort int + whoisServer string + listen string +} + +var setting settingType func main() { - serversDefault := "" - domainDefault := "" - proxyPortDefault := 8000 - whoisDefault := "whois.verisign-grs.com" - listenDefault := ":5000" + var settingDefault = settingType{ + []string{""}, "", 8000, "whois.verisign-grs.com", ":5000", + } - if serversEnv := os.Getenv("BIRDLG_SERVERS"); serversEnv != "" { - serversDefault = serversEnv - } - if domainEnv := os.Getenv("BIRDLG_DOMAIN"); domainEnv != "" { - domainDefault = domainEnv - } - if proxyPortEnv := os.Getenv("BIRDLG_PROXY_PORT"); proxyPortEnv != "" { - var err error - proxyPortDefault, err = strconv.Atoi(proxyPortEnv) - if err != nil{ - panic(err) - } - } - if whoisEnv := os.Getenv("BIRDLG_WHOIS"); whoisEnv != "" { - whoisDefault = whoisEnv - } - if listenEnv := os.Getenv("BIRDLG_LISTEN"); listenEnv != "" { - listenDefault = listenEnv - } + if serversEnv := os.Getenv("BIRDLG_SERVERS"); serversEnv != "" { + settingDefault.servers = strings.Split(serversEnv, ",") + } + if domainEnv := os.Getenv("BIRDLG_DOMAIN"); domainEnv != "" { + settingDefault.domain = domainEnv + } + if proxyPortEnv := os.Getenv("BIRDLG_PROXY_PORT"); proxyPortEnv != "" { + var err error + if settingDefault.proxyPort, err = strconv.Atoi(proxyPortEnv); err != nil { + panic(err) + } + } + if whoisEnv := os.Getenv("BIRDLG_WHOIS"); whoisEnv != "" { + settingDefault.whoisServer = whoisEnv + } + if listenEnv := os.Getenv("BIRDLG_LISTEN"); listenEnv != "" { + settingDefault.listen = listenEnv + } - serversPtr := flag.String("servers", serversDefault, "server name prefixes, separated by comma") - domainPtr := flag.String("domain", domainDefault, "server name domain suffixes") - proxyPortPtr := flag.Int("proxy-port", proxyPortDefault, "port bird-lgproxy is running on") - whoisPtr := flag.String("whois", whoisDefault, "whois server for queries") - listenPtr := flag.String("listen", listenDefault, "address bird-lg is listening on") - flag.Parse() + 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") + flag.Parse() - if *serversPtr == "" { - panic("no server set") - } else if *domainPtr == "" { - panic("no base domain set") - } + if *serversPtr == "" { + panic("no server set") + } else if *domainPtr == "" { + panic("no base domain set") + } - settingServers = strings.Split(*serversPtr, ",") - settingServersDomain = *domainPtr - settingServersPort = *proxyPortPtr - settingWhoisServer = *whoisPtr - settingListen = *listenPtr + setting = settingType{ + strings.Split(*serversPtr, ","), + *domainPtr, + *proxyPortPtr, + *whoisPtr, + *listenPtr, + } - webServerStart() + webServerStart() } diff --git a/frontend/template.go b/frontend/template.go index 9f7f21a..b54c9f5 100644 --- a/frontend/template.go +++ b/frontend/template.go @@ -30,7 +30,7 @@ func templateHeader(w http.ResponseWriter, r *http.Request, title string) { // Use a default URL if the request URL is too short // The URL is for return to IPv4 summary page if len(split) < 3 { - path = "ipv4/summary/" + strings.Join(settingServers[:], "+") + "/" + path = "ipv4/summary/" + strings.Join(setting.servers, "+") + "/" } else if len(split) == 3 { path += "/" } @@ -40,10 +40,10 @@ func templateHeader(w http.ResponseWriter, r *http.Request, title string) { // Compose URLs for link in navbar ipv4URL := "/" + strings.Join([]string{"ipv4", split[1], split[2], strings.Join(split[3:], "/")}, "/") ipv6URL := "/" + strings.Join([]string{"ipv6", split[1], split[2], strings.Join(split[3:], "/")}, "/") - allURL := "/" + strings.Join([]string{split[0], split[1], strings.Join(settingServers[:], "+"), strings.Join(split[3:], "/")}, "/") + allURL := "/" + strings.Join([]string{split[0], split[1], strings.Join(setting.servers, "+"), strings.Join(split[3:], "/")}, "/") // Check if the "All Server" link should be marked as active - var serverAllActive bool = strings.ToLower(split[2]) == strings.ToLower(strings.Join(settingServers[:], "+")) + var serverAllActive bool = strings.ToLower(split[2]) == strings.ToLower(strings.Join(setting.servers, "+")) // Print the IPv4, IPv6, All Servers link in navbar var serverNavigation string = ` @@ -55,7 +55,7 @@ func templateHeader(w http.ResponseWriter, r *http.Request, title string) { ` // Add a link for each of the servers - for _, server := range settingServers { + for _, server := range setting.servers { var serverActive string if split[2] == server { serverActive = " active" diff --git a/frontend/webserver.go b/frontend/webserver.go index 417b5b6..8bd50b5 100644 --- a/frontend/webserver.go +++ b/frontend/webserver.go @@ -119,7 +119,7 @@ func webHandlerNavbarFormRedirect(w http.ResponseWriter, r *http.Request) { func webServerStart() { // Start HTTP server http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/ipv4/summary/"+strings.Join(settingServers[:], "+"), 302) + http.Redirect(w, r, "/ipv4/summary/"+strings.Join(setting.servers, "+"), 302) }) http.HandleFunc("/ipv4/summary/", webBackendCommunicator("bird", "summary")) http.HandleFunc("/ipv6/summary/", webBackendCommunicator("bird6", "summary")) @@ -141,5 +141,5 @@ func webServerStart() { http.HandleFunc("/ipv6/traceroute/", webBackendCommunicator("traceroute6", "traceroute")) http.HandleFunc("/whois/", webHandlerWhois) http.HandleFunc("/redir/", webHandlerNavbarFormRedirect) - http.ListenAndServe(settingListen, nil) + http.ListenAndServe(setting.listen, nil) } diff --git a/frontend/whois.go b/frontend/whois.go index bc8f0ba..8592858 100644 --- a/frontend/whois.go +++ b/frontend/whois.go @@ -1,22 +1,22 @@ package main import ( - "net" - "io/ioutil" + "io/ioutil" + "net" ) // Send a whois request func whois(s string) string { - conn, err := net.Dial("tcp", settingWhoisServer + ":43") + conn, err := net.Dial("tcp", setting.whoisServer+":43") if err != nil { return err.Error() } defer conn.Close() - conn.Write([]byte(s + "\r\n")) + conn.Write([]byte(s + "\r\n")) result, err := ioutil.ReadAll(conn) if err != nil { return err.Error() } - return string(result) + return string(result) } diff --git a/proxy/main.go b/proxy/main.go index e44c9aa..192f1fd 100644 --- a/proxy/main.go +++ b/proxy/main.go @@ -18,29 +18,39 @@ func invalidHandler(httpW http.ResponseWriter, httpR *http.Request) { httpW.Write([]byte("Invalid Request\n")) } +type settingType struct { + birdSocket string + bird6Socket string + listen string +} + +var setting settingType + // Wrapper of tracer func main() { var err error // Prepare default socket paths, use environment variable if possible - birdSocketDefault := "/var/run/bird/bird.ctl" - bird6SocketDefault := "/var/run/bird/bird6.ctl" - listenDefault := ":8000" + var settingDefault = settingType{ + "/var/run/bird/bird.ctl", + "/var/run/bird/bird6.ctl", + ":8000", + } if birdSocketEnv := os.Getenv("BIRD_SOCKET"); birdSocketEnv != "" { - birdSocketDefault = birdSocketEnv + settingDefault.birdSocket = birdSocketEnv } if bird6SocketEnv := os.Getenv("BIRD6_SOCKET"); bird6SocketEnv != "" { - bird6SocketDefault = bird6SocketEnv + settingDefault.bird6Socket = bird6SocketEnv } if listenEnv := os.Getenv("BIRDLG_LISTEN"); listenEnv != "" { - listenDefault = listenEnv + settingDefault.listen = listenEnv } // Allow parameters to override environment variables - birdParam := flag.String("bird", birdSocketDefault, "socket file for bird, set either in parameter or environment variable BIRD_SOCKET") - bird6Param := flag.String("bird6", bird6SocketDefault, "socket file for bird6, set either in parameter or environment variable BIRD6_SOCKET") - listenParam := flag.String("listen", listenDefault, "listen address, set either in parameter or environment variable BIRDLG_LISTEN") + birdParam := flag.String("bird", settingDefault.birdSocket, "socket file for bird, set either in parameter or environment variable BIRD_SOCKET") + bird6Param := flag.String("bird6", settingDefault.bird6Socket, "socket file for bird6, set either in parameter or environment variable BIRD6_SOCKET") + listenParam := flag.String("listen", settingDefault.listen, "listen address, set either in parameter or environment variable BIRDLG_LISTEN") flag.Parse() // Initialize BIRDv4 socket