Improve parameter handling

This commit is contained in:
Lan Tian 2020-03-27 12:21:30 +08:00
parent 8f581d378f
commit ad17c52bb9
No known key found for this signature in database
GPG Key ID: 27F31700E751EC22
6 changed files with 85 additions and 72 deletions

View File

@ -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 {

View File

@ -1,50 +1,51 @@
package main
import (
"os"
"flag"
"strings"
"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
settingDefault.servers = strings.Split(serversEnv, ",")
}
if domainEnv := os.Getenv("BIRDLG_DOMAIN"); domainEnv != "" {
domainDefault = domainEnv
settingDefault.domain = domainEnv
}
if proxyPortEnv := os.Getenv("BIRDLG_PROXY_PORT"); proxyPortEnv != "" {
var err error
proxyPortDefault, err = strconv.Atoi(proxyPortEnv)
if err != nil{
if settingDefault.proxyPort, err = strconv.Atoi(proxyPortEnv); err != nil {
panic(err)
}
}
if whoisEnv := os.Getenv("BIRDLG_WHOIS"); whoisEnv != "" {
whoisDefault = whoisEnv
settingDefault.whoisServer = whoisEnv
}
if listenEnv := os.Getenv("BIRDLG_LISTEN"); listenEnv != "" {
listenDefault = 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")
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 == "" {
@ -53,11 +54,13 @@ func main() {
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()
}

View File

@ -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) {
</li>`
// 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"

View File

@ -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)
}

View File

@ -1,13 +1,13 @@
package main
import (
"net"
"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()
}

View File

@ -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