Pull request: 2509 type-safety vol.2
Merge in DNS/adguard-home from 2509-type-safety-vol2 to master Updates #2509. Squashed commit of the following: commit c944e4e0a9949fc894c90b4bc1f739148a67fd9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Jan 21 19:36:20 2021 +0300 all: imp docs commit e8ac1815c492b0a9434596e35a48755cac2b9f3b Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 20 12:38:48 2021 +0300 all: imp JSON encoding, decoding
This commit is contained in:
@@ -16,10 +16,6 @@ import (
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
)
|
||||
|
||||
type getVersionJSONRequest struct {
|
||||
RecheckNow bool `json:"recheck_now"`
|
||||
}
|
||||
|
||||
// temporaryError is the interface for temporary errors from the Go standard
|
||||
// library.
|
||||
type temporaryError interface {
|
||||
@@ -29,31 +25,34 @@ type temporaryError interface {
|
||||
|
||||
// Get the latest available version from the Internet
|
||||
func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
|
||||
resp := &versionResponse{}
|
||||
if Context.disableUpdate {
|
||||
resp := make(map[string]interface{})
|
||||
resp["disabled"] = true
|
||||
d, _ := json.Marshal(resp)
|
||||
_, _ = w.Write(d)
|
||||
// w.Header().Set("Content-Type", "application/json")
|
||||
resp.Disabled = true
|
||||
_ = json.NewEncoder(w).Encode(resp)
|
||||
// TODO(e.burkov): Add error handling and deal with headers.
|
||||
return
|
||||
}
|
||||
|
||||
req := getVersionJSONRequest{}
|
||||
req := &struct {
|
||||
Recheck bool `json:"recheck_now"`
|
||||
}{}
|
||||
|
||||
var err error
|
||||
if r.ContentLength != 0 {
|
||||
err = json.NewDecoder(r.Body).Decode(&req)
|
||||
err = json.NewDecoder(r.Body).Decode(req)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusBadRequest, "JSON parse: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var info updater.VersionInfo
|
||||
for i := 0; i != 3; i++ {
|
||||
func() {
|
||||
Context.controlLock.Lock()
|
||||
defer Context.controlLock.Unlock()
|
||||
|
||||
info, err = Context.updater.VersionInfo(req.RecheckNow)
|
||||
resp.VersionInfo, err = Context.updater.VersionInfo(req.Recheck)
|
||||
}()
|
||||
|
||||
if err != nil {
|
||||
@@ -76,13 +75,16 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if err != nil {
|
||||
vcu := Context.updater.VersionCheckURL()
|
||||
// TODO(a.garipov): Figure out the purpose of %T verb.
|
||||
httpError(w, http.StatusBadGateway, "Couldn't get version check json from %s: %T %s\n", vcu, err, err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
resp.confirmAutoUpdate()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = w.Write(getVersionResp(info))
|
||||
err = json.NewEncoder(w).Encode(resp)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "Couldn't write body: %s", err)
|
||||
}
|
||||
@@ -109,21 +111,24 @@ func handleUpdate(w http.ResponseWriter, _ *http.Request) {
|
||||
go finishUpdate()
|
||||
}
|
||||
|
||||
// Convert version.json data to our JSON response
|
||||
func getVersionResp(info updater.VersionInfo) []byte {
|
||||
ret := make(map[string]interface{})
|
||||
ret["can_autoupdate"] = false
|
||||
ret["new_version"] = info.NewVersion
|
||||
ret["announcement"] = info.Announcement
|
||||
ret["announcement_url"] = info.AnnouncementURL
|
||||
// versionResponse is the response for /control/version.json endpoint.
|
||||
type versionResponse struct {
|
||||
Disabled bool `json:"disabled"`
|
||||
updater.VersionInfo
|
||||
}
|
||||
|
||||
if info.CanAutoUpdate {
|
||||
// confirmAutoUpdate checks the real possibility of auto update.
|
||||
func (vr *versionResponse) confirmAutoUpdate() {
|
||||
if vr.CanAutoUpdate != nil && *vr.CanAutoUpdate {
|
||||
canUpdate := true
|
||||
|
||||
tlsConf := tlsConfigSettings{}
|
||||
Context.tls.WriteDiskConfig(&tlsConf)
|
||||
var tlsConf *tlsConfigSettings
|
||||
if runtime.GOOS != "windows" {
|
||||
tlsConf = &tlsConfigSettings{}
|
||||
Context.tls.WriteDiskConfig(tlsConf)
|
||||
}
|
||||
|
||||
if runtime.GOOS != "windows" &&
|
||||
if tlsConf != nil &&
|
||||
((tlsConf.Enabled && (tlsConf.PortHTTPS < 1024 ||
|
||||
tlsConf.PortDNSOverTLS < 1024 ||
|
||||
tlsConf.PortDNSOverQUIC < 1024)) ||
|
||||
@@ -131,11 +136,8 @@ func getVersionResp(info updater.VersionInfo) []byte {
|
||||
config.DNS.Port < 1024) {
|
||||
canUpdate, _ = sysutil.CanBindPrivilegedPorts()
|
||||
}
|
||||
ret["can_autoupdate"] = canUpdate
|
||||
vr.CanAutoUpdate = &canUpdate
|
||||
}
|
||||
|
||||
d, _ := json.Marshal(ret)
|
||||
return d
|
||||
}
|
||||
|
||||
// Complete an update procedure
|
||||
|
||||
Reference in New Issue
Block a user