Skip to content

Commit 4048bd3

Browse files
committed
Remove memstat and cmdline defaults
1 parent 5cf81a2 commit 4048bd3

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

cmd/daze/main.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
"expvar"
46
"flag"
57
"fmt"
68
"log"
@@ -61,6 +63,31 @@ func main() {
6163
if os.Getenv("ANDROID_ROOT") != "" {
6264
net.DefaultResolver = daze.ResolverDns("1.1.1.1:53")
6365
}
66+
// Remove cmdline and memstats from expvar default exports.
67+
// See: https://github.com/golang/go/issues/29105
68+
muxHttp := http.NewServeMux()
69+
muxHttp.HandleFunc("/debug/vars", func(w http.ResponseWriter, r *http.Request) {
70+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
71+
vars := new(expvar.Map).Init()
72+
expvar.Do(func(kv expvar.KeyValue) {
73+
vars.Set(kv.Key, kv.Value)
74+
})
75+
vars.Delete("cmdline")
76+
vars.Delete("memstats")
77+
msg := map[string]any{}
78+
err := json.Unmarshal([]byte(vars.String()), &msg)
79+
if err != nil {
80+
w.WriteHeader(http.StatusBadRequest)
81+
w.Write([]byte(err.Error()))
82+
return
83+
}
84+
enc := json.NewEncoder(w)
85+
enc.SetIndent("", " ")
86+
enc.Encode(msg)
87+
})
88+
muxHttp.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
89+
http.DefaultServeMux.ServeHTTP(w, r)
90+
})
6491
resExec := filepath.Dir(doa.Try(os.Executable()))
6592
subCommand := os.Args[1]
6693
os.Args = os.Args[1:len(os.Args)]
@@ -121,7 +148,7 @@ func main() {
121148
if *flGpprof != "" {
122149
_ = pprof.Handler
123150
log.Println("main: listen net/http/pprof on", *flGpprof)
124-
go func() { doa.Nil(http.ListenAndServe(*flGpprof, nil)) }()
151+
go func() { doa.Nil(http.ListenAndServe(*flGpprof, muxHttp)) }()
125152
}
126153
// Hang prevent program from exiting.
127154
gracefulexit.Wait()
@@ -199,7 +226,7 @@ func main() {
199226
if *flGpprof != "" {
200227
_ = pprof.Handler
201228
log.Println("main: listen net/http/pprof on", *flGpprof)
202-
go func() { doa.Nil(http.ListenAndServe(*flGpprof, nil)) }()
229+
go func() { doa.Nil(http.ListenAndServe(*flGpprof, muxHttp)) }()
203230
}
204231
// Hang prevent program from exiting.
205232
gracefulexit.Wait()

0 commit comments

Comments
 (0)