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 { for i, server := range servers {
// Check if the server is in the valid server list passed at startup // Check if the server is in the valid server list passed at startup
var isValidServer bool = false var isValidServer bool = false
for _, validServer := range settingServers { for _, validServer := range setting.servers {
if validServer == server { if validServer == server {
isValidServer = true isValidServer = true
break break
@ -35,7 +35,7 @@ func batchRequest(servers []string, endpoint string, command string) []string {
}(i) }(i)
} else { } else {
// Compose URL and send the request // 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) { go func(url string, i int) {
response, err := http.Get(url) response, err := http.Get(url)
if err != nil { if err != nil {

View File

@ -1,63 +1,66 @@
package main package main
import ( import (
"os" "flag"
"flag" "os"
"strings" "strconv"
"strconv" "strings"
) )
var settingServers []string type settingType struct {
var settingServersDomain string servers []string
var settingServersPort int domain string
var settingWhoisServer string proxyPort int
var settingListen string whoisServer string
listen string
}
var setting settingType
func main() { func main() {
serversDefault := "" var settingDefault = settingType{
domainDefault := "" []string{""}, "", 8000, "whois.verisign-grs.com", ":5000",
proxyPortDefault := 8000 }
whoisDefault := "whois.verisign-grs.com"
listenDefault := ":5000"
if serversEnv := os.Getenv("BIRDLG_SERVERS"); serversEnv != "" { if serversEnv := os.Getenv("BIRDLG_SERVERS"); serversEnv != "" {
serversDefault = serversEnv settingDefault.servers = strings.Split(serversEnv, ",")
} }
if domainEnv := os.Getenv("BIRDLG_DOMAIN"); domainEnv != "" { if domainEnv := os.Getenv("BIRDLG_DOMAIN"); domainEnv != "" {
domainDefault = domainEnv settingDefault.domain = domainEnv
} }
if proxyPortEnv := os.Getenv("BIRDLG_PROXY_PORT"); proxyPortEnv != "" { if proxyPortEnv := os.Getenv("BIRDLG_PROXY_PORT"); proxyPortEnv != "" {
var err error var err error
proxyPortDefault, err = strconv.Atoi(proxyPortEnv) if settingDefault.proxyPort, err = strconv.Atoi(proxyPortEnv); err != nil {
if err != nil{ panic(err)
panic(err) }
} }
} if whoisEnv := os.Getenv("BIRDLG_WHOIS"); whoisEnv != "" {
if whoisEnv := os.Getenv("BIRDLG_WHOIS"); whoisEnv != "" { settingDefault.whoisServer = whoisEnv
whoisDefault = whoisEnv }
} if listenEnv := os.Getenv("BIRDLG_LISTEN"); listenEnv != "" {
if listenEnv := os.Getenv("BIRDLG_LISTEN"); listenEnv != "" { settingDefault.listen = listenEnv
listenDefault = listenEnv }
}
serversPtr := flag.String("servers", serversDefault, "server name prefixes, separated by comma") serversPtr := flag.String("servers", strings.Join(settingDefault.servers, ","), "server name prefixes, separated by comma")
domainPtr := flag.String("domain", domainDefault, "server name domain suffixes") domainPtr := flag.String("domain", settingDefault.domain, "server name domain suffixes")
proxyPortPtr := flag.Int("proxy-port", proxyPortDefault, "port bird-lgproxy is running on") proxyPortPtr := flag.Int("proxy-port", settingDefault.proxyPort, "port bird-lgproxy is running on")
whoisPtr := flag.String("whois", whoisDefault, "whois server for queries") whoisPtr := flag.String("whois", settingDefault.whoisServer, "whois server for queries")
listenPtr := flag.String("listen", listenDefault, "address bird-lg is listening on") listenPtr := flag.String("listen", settingDefault.listen, "address bird-lg is listening on")
flag.Parse() flag.Parse()
if *serversPtr == "" { if *serversPtr == "" {
panic("no server set") panic("no server set")
} else if *domainPtr == "" { } else if *domainPtr == "" {
panic("no base domain set") panic("no base domain set")
} }
settingServers = strings.Split(*serversPtr, ",") setting = settingType{
settingServersDomain = *domainPtr strings.Split(*serversPtr, ","),
settingServersPort = *proxyPortPtr *domainPtr,
settingWhoisServer = *whoisPtr *proxyPortPtr,
settingListen = *listenPtr *whoisPtr,
*listenPtr,
}
webServerStart() 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 // Use a default URL if the request URL is too short
// The URL is for return to IPv4 summary page // The URL is for return to IPv4 summary page
if len(split) < 3 { if len(split) < 3 {
path = "ipv4/summary/" + strings.Join(settingServers[:], "+") + "/" path = "ipv4/summary/" + strings.Join(setting.servers, "+") + "/"
} else if len(split) == 3 { } else if len(split) == 3 {
path += "/" path += "/"
} }
@ -40,10 +40,10 @@ func templateHeader(w http.ResponseWriter, r *http.Request, title string) {
// Compose URLs for link in navbar // Compose URLs for link in navbar
ipv4URL := "/" + strings.Join([]string{"ipv4", split[1], split[2], strings.Join(split[3:], "/")}, "/") 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:], "/")}, "/") 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 // 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 // Print the IPv4, IPv6, All Servers link in navbar
var serverNavigation string = ` var serverNavigation string = `
@ -55,7 +55,7 @@ func templateHeader(w http.ResponseWriter, r *http.Request, title string) {
</li>` </li>`
// Add a link for each of the servers // Add a link for each of the servers
for _, server := range settingServers { for _, server := range setting.servers {
var serverActive string var serverActive string
if split[2] == server { if split[2] == server {
serverActive = " active" serverActive = " active"

View File

@ -119,7 +119,7 @@ func webHandlerNavbarFormRedirect(w http.ResponseWriter, r *http.Request) {
func webServerStart() { func webServerStart() {
// Start HTTP server // Start HTTP server
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 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("/ipv4/summary/", webBackendCommunicator("bird", "summary"))
http.HandleFunc("/ipv6/summary/", webBackendCommunicator("bird6", "summary")) http.HandleFunc("/ipv6/summary/", webBackendCommunicator("bird6", "summary"))
@ -141,5 +141,5 @@ func webServerStart() {
http.HandleFunc("/ipv6/traceroute/", webBackendCommunicator("traceroute6", "traceroute")) http.HandleFunc("/ipv6/traceroute/", webBackendCommunicator("traceroute6", "traceroute"))
http.HandleFunc("/whois/", webHandlerWhois) http.HandleFunc("/whois/", webHandlerWhois)
http.HandleFunc("/redir/", webHandlerNavbarFormRedirect) http.HandleFunc("/redir/", webHandlerNavbarFormRedirect)
http.ListenAndServe(settingListen, nil) http.ListenAndServe(setting.listen, nil)
} }

View File

@ -1,22 +1,22 @@
package main package main
import ( import (
"net" "io/ioutil"
"io/ioutil" "net"
) )
// Send a whois request // Send a whois request
func whois(s string) string { func whois(s string) string {
conn, err := net.Dial("tcp", settingWhoisServer + ":43") conn, err := net.Dial("tcp", setting.whoisServer+":43")
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
defer conn.Close() defer conn.Close()
conn.Write([]byte(s + "\r\n")) conn.Write([]byte(s + "\r\n"))
result, err := ioutil.ReadAll(conn) result, err := ioutil.ReadAll(conn)
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
return string(result) return string(result)
} }

View File

@ -18,29 +18,39 @@ func invalidHandler(httpW http.ResponseWriter, httpR *http.Request) {
httpW.Write([]byte("Invalid Request\n")) httpW.Write([]byte("Invalid Request\n"))
} }
type settingType struct {
birdSocket string
bird6Socket string
listen string
}
var setting settingType
// Wrapper of tracer // Wrapper of tracer
func main() { func main() {
var err error var err error
// Prepare default socket paths, use environment variable if possible // Prepare default socket paths, use environment variable if possible
birdSocketDefault := "/var/run/bird/bird.ctl" var settingDefault = settingType{
bird6SocketDefault := "/var/run/bird/bird6.ctl" "/var/run/bird/bird.ctl",
listenDefault := ":8000" "/var/run/bird/bird6.ctl",
":8000",
}
if birdSocketEnv := os.Getenv("BIRD_SOCKET"); birdSocketEnv != "" { if birdSocketEnv := os.Getenv("BIRD_SOCKET"); birdSocketEnv != "" {
birdSocketDefault = birdSocketEnv settingDefault.birdSocket = birdSocketEnv
} }
if bird6SocketEnv := os.Getenv("BIRD6_SOCKET"); bird6SocketEnv != "" { if bird6SocketEnv := os.Getenv("BIRD6_SOCKET"); bird6SocketEnv != "" {
bird6SocketDefault = bird6SocketEnv settingDefault.bird6Socket = bird6SocketEnv
} }
if listenEnv := os.Getenv("BIRDLG_LISTEN"); listenEnv != "" { if listenEnv := os.Getenv("BIRDLG_LISTEN"); listenEnv != "" {
listenDefault = listenEnv settingDefault.listen = listenEnv
} }
// Allow parameters to override environment variables // Allow parameters to override environment variables
birdParam := flag.String("bird", birdSocketDefault, "socket file for bird, set either in parameter or environment variable BIRD_SOCKET") birdParam := flag.String("bird", settingDefault.birdSocket, "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") bird6Param := flag.String("bird6", settingDefault.bird6Socket, "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") listenParam := flag.String("listen", settingDefault.listen, "listen address, set either in parameter or environment variable BIRDLG_LISTEN")
flag.Parse() flag.Parse()
// Initialize BIRDv4 socket // Initialize BIRDv4 socket