|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "expvar" |
4 | 6 | "flag" |
5 | 7 | "fmt" |
6 | 8 | "log" |
@@ -61,6 +63,31 @@ func main() { |
61 | 63 | if os.Getenv("ANDROID_ROOT") != "" { |
62 | 64 | net.DefaultResolver = daze.ResolverDns("1.1.1.1:53") |
63 | 65 | } |
| 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 | + }) |
64 | 91 | resExec := filepath.Dir(doa.Try(os.Executable())) |
65 | 92 | subCommand := os.Args[1] |
66 | 93 | os.Args = os.Args[1:len(os.Args)] |
@@ -121,7 +148,7 @@ func main() { |
121 | 148 | if *flGpprof != "" { |
122 | 149 | _ = pprof.Handler |
123 | 150 | 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)) }() |
125 | 152 | } |
126 | 153 | // Hang prevent program from exiting. |
127 | 154 | gracefulexit.Wait() |
@@ -199,7 +226,7 @@ func main() { |
199 | 226 | if *flGpprof != "" { |
200 | 227 | _ = pprof.Handler |
201 | 228 | 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)) }() |
203 | 230 | } |
204 | 231 | // Hang prevent program from exiting. |
205 | 232 | gracefulexit.Wait() |
|
0 commit comments