| 246 | } |
| 247 | |
| 248 | func WebConsoleHTTPHandler(proxy *pp.ProxyClient) func(w http.ResponseWriter, r *http.Request) { |
| 249 | return func(w http.ResponseWriter, r *http.Request) { |
| 250 | if r.Method == "GET" { |
| 251 | |
| 252 | if strings.HasPrefix(r.RequestURI, "/traffic.svg") { |
| 253 | w.Header().Add("Content-Type", "image/svg+xml") |
| 254 | w.Write(proxy.IO.Tr.SVG(300, 50, r.FormValue("log") == "1").Bytes()) |
| 255 | return |
| 256 | } |
| 257 | |
| 258 | payload := struct { |
| 259 | Global bool |
| 260 | Entries int |
| 261 | EntriesRatio int |
| 262 | DNS string |
| 263 | I18N map[string]string |
| 264 | }{} |
| 265 | |
| 266 | buf, count := &bytes.Buffer{}, 0 |
| 267 | |
| 268 | proxy.DNSCache.Info(func(k lru.Key, v interface{}, h int64, w int64) { |
| 269 | count++ |
| 270 | rule := v.(*pp.Rule) |
| 271 | ip, r := rule.IP, rule.R |
| 272 | |
| 273 | if aclrouter.IPv4ToInt(ip) > 0 { |
| 274 | ips := make([]string, 4) |
| 275 | for i, s := range strings.Split(ip, ".") { |
| 276 | if ips[i] = s; len(s) < 3 { |
| 277 | ips[i] = "<span class=p" + strconv.Itoa(len(s)) + ">" + s + "</span>" |
| 278 | } |
| 279 | } |
| 280 | ip = fmt.Sprintf("<a href='http://freeapi.ipip.net/%v' target=_blank>%v</a>", ip, strings.Join(ips, ".")) |
| 281 | } else { |
| 282 | ip = "<a><span class=p1>-</span>.<span class=p1>-</span>.<span class=p1>-</span>.<span class=p1>-</span></a>" |
| 283 | } |
| 284 | |
| 285 | buf.WriteString(fmt.Sprintf(`<tr class=citem><td class="fit host" host="%v">%v (%d)</td><td class="fit ip">%s</td>%s%s%s<!--%s--></tr>`, |
| 286 | k, k, h, ip, ruleMappingLeft[r], ruleMapping[r], ruleMappingRight[r], toString(rule.OldAns))) |
| 287 | }) |
| 288 | |
| 289 | buf.WriteString(fmt.Sprintf("<tr class=last-tr><td></td><td></td>%s</tr>", strings.Repeat("<td class=side-rule></td>", 13))) |
| 290 | |
| 291 | payload.DNS = buf.String() |
| 292 | payload.Global = proxy.Policy.IsSet(pp.PolicyGlobal) |
| 293 | payload.Entries = count |
| 294 | payload.EntriesRatio = count * 100 / int(proxy.DNSCache.MaxWeight()) |
| 295 | |
| 296 | // use lang=en to force english display |
| 297 | if strings.Contains(r.Header.Get("Accept-Language"), "zh") && r.FormValue("lang") != "en" { |
| 298 | payload.I18N = _i18n["zh"] |
| 299 | } else { |
| 300 | payload.I18N = _i18n["en"] |
| 301 | } |
| 302 | |
| 303 | webConsoleHTML.Execute(w, payload) |
| 304 | } else if r.Method == "POST" { |
| 305 | if r.FormValue("cleardns") != "" { |