From ecd9ef47b0b742ea74561b7e89a3bce431b23d05 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Fri, 22 Jan 2021 15:00:45 +0300 Subject: [PATCH] Pull request: 2509 type-safety vol.3 Merge in DNS/adguard-home from 2509-type-safety-vol2-1 to master Squashed commit of the following: commit d58efb32396328247e1bb044f2b01145530cd84c Author: Eugene Burkov Date: Fri Jan 22 14:27:54 2021 +0300 home: imp JSON encoding --- internal/home/clientshttp.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/internal/home/clientshttp.go b/internal/home/clientshttp.go index aecc68e5..e39f4767 100644 --- a/internal/home/clientshttp.go +++ b/internal/home/clientshttp.go @@ -22,7 +22,7 @@ type clientJSON struct { Upstreams []string `json:"upstreams"` - WhoisInfo map[string]interface{} `json:"whois_info"` + WhoisInfo map[string]string `json:"whois_info"` // Disallowed - if true -- client's IP is not disallowed // Otherwise, it is blocked. @@ -39,7 +39,7 @@ type clientHostJSON struct { Name string `json:"name"` Source string `json:"source"` - WhoisInfo map[string]interface{} `json:"whois_info"` + WhoisInfo map[string]string `json:"whois_info"` } type clientListJSON struct { @@ -75,7 +75,7 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, _ *http cj.Source = "WHOIS" } - cj.WhoisInfo = make(map[string]interface{}) + cj.WhoisInfo = map[string]string{} for _, wi := range ch.WhoisInfo { cj.WhoisInfo[wi[0]] = wi[1] } @@ -140,7 +140,7 @@ func clientHostToJSON(ip string, ch ClientHost) clientJSON { IDs: []string{ip}, } - cj.WhoisInfo = make(map[string]interface{}) + cj.WhoisInfo = map[string]string{} for _, wi := range ch.WhoisInfo { cj.WhoisInfo[wi[0]] = wi[1] } @@ -228,7 +228,7 @@ func (clients *clientsContainer) handleUpdateClient(w http.ResponseWriter, r *ht // Get the list of clients by IP address list func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http.Request) { q := r.URL.Query() - data := []map[string]interface{}{} + data := []map[string]clientJSON{} for i := 0; ; i++ { ipStr := q.Get(fmt.Sprintf("ip%d", i)) ip := net.ParseIP(ipStr) @@ -236,7 +236,6 @@ func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http break } - el := map[string]interface{}{} c, ok := clients.Find(ip) var cj clientJSON if !ok { @@ -250,18 +249,13 @@ func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http cj.Disallowed, cj.DisallowedRule = clients.dnsServer.IsBlockedIP(ip) } - el[ipStr] = cj - data = append(data, el) - } - - js, err := json.Marshal(data) - if err != nil { - httpError(w, http.StatusInternalServerError, "json.Marshal: %s", err) - return + data = append(data, map[string]clientJSON{ + ipStr: cj, + }) } w.Header().Set("Content-Type", "application/json") - _, err = w.Write(js) + err := json.NewEncoder(w).Encode(data) if err != nil { httpError(w, http.StatusInternalServerError, "Couldn't write response: %s", err) }