diff --git a/home/config.go b/home/config.go index ca576f45..41071c61 100644 --- a/home/config.go +++ b/home/config.go @@ -58,6 +58,10 @@ type configuration struct { transport *http.Transport client *http.Client + // cached version.json to avoid hammering github.io for each page reload + versionCheckJSON []byte + versionCheckLastTime time.Time + BindHost string `yaml:"bind_host"` // BindHost is the IP address of the HTTP server to bind to BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server AuthName string `yaml:"auth_name"` // AuthName is the basic auth username diff --git a/home/control.go b/home/control.go index e6c41d92..0a2c6f43 100644 --- a/home/control.go +++ b/home/control.go @@ -24,10 +24,6 @@ import ( const updatePeriod = time.Minute * 30 -// cached version.json to avoid hammering github.io for each page reload -var versionCheckJSON []byte -var versionCheckLastTime time.Time - var protocols = []string{"tls://", "https://", "tcp://", "sdns://"} // ---------------- diff --git a/home/control_update.go b/home/control_update.go index c6babbf1..3e02d71d 100644 --- a/home/control_update.go +++ b/home/control_update.go @@ -74,8 +74,8 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) { now := time.Now() if !req.RecheckNow { config.controlLock.Lock() - cached := now.Sub(versionCheckLastTime) <= versionCheckPeriod && len(versionCheckJSON) != 0 - data := versionCheckJSON + cached := now.Sub(config.versionCheckLastTime) <= versionCheckPeriod && len(config.versionCheckJSON) != 0 + data := config.versionCheckJSON config.controlLock.Unlock() if cached { @@ -104,8 +104,8 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) { } config.controlLock.Lock() - versionCheckLastTime = now - versionCheckJSON = body + config.versionCheckLastTime = now + config.versionCheckJSON = body config.controlLock.Unlock() w.Header().Set("Content-Type", "application/json") @@ -501,12 +501,12 @@ func finishUpdate(u *updateInfo) { func handleUpdate(w http.ResponseWriter, r *http.Request) { log.Tracef("%s %v", r.Method, r.URL) - if len(versionCheckJSON) == 0 { + if len(config.versionCheckJSON) == 0 { httpError(w, http.StatusBadRequest, "/update request isn't allowed now") return } - u, err := getUpdateInfo(versionCheckJSON) + u, err := getUpdateInfo(config.versionCheckJSON) if err != nil { httpError(w, http.StatusInternalServerError, "%s", err) return