-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
108 lines (96 loc) · 3.1 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package trace
import (
"net/http"
"github.com/zxfonline/gotrace/expvar"
"github.com/zxfonline/gotrace/golangtrace"
"github.com/zxfonline/gotrace/pprof"
)
func Init(handler *http.ServeMux) {
golangtrace.AuthRequest = func(req *http.Request) (any bool) {
//TODO iptable init
// RemoteAddr is commonly in the form "IP" or "IP:port".
// If it is in the form "IP:port", split off the port.
//host, _, err := net.SplitHostPort(req.RemoteAddr)
//if err != nil {
// host = req.RemoteAddr
//}
//switch host {
//case "localhost", "127.0.0.1", "::1":
// return true
//default:
// return false
//}
return true
}
handler.HandleFunc("/debug/pprof/", func(w http.ResponseWriter, req *http.Request) {
if !golangtrace.AuthRequest(req) {
http.Error(w, "not allowed", http.StatusUnauthorized)
return
}
pprof.Index(w, req)
})
handler.HandleFunc("/debug/pprof/cmdline", func(w http.ResponseWriter, req *http.Request) {
if !golangtrace.AuthRequest(req) {
http.Error(w, "not allowed", http.StatusUnauthorized)
return
}
pprof.Cmdline(w, req)
})
handler.HandleFunc("/debug/pprof/profile", func(w http.ResponseWriter, req *http.Request) {
if !golangtrace.AuthRequest(req) {
http.Error(w, "not allowed", http.StatusUnauthorized)
return
}
pprof.Profile(w, req)
})
handler.HandleFunc("/debug/pprof/symbol", func(w http.ResponseWriter, req *http.Request) {
if !golangtrace.AuthRequest(req) {
http.Error(w, "not allowed", http.StatusUnauthorized)
return
}
pprof.Symbol(w, req)
})
handler.HandleFunc("/debug/pprof/trace", func(w http.ResponseWriter, req *http.Request) {
if !golangtrace.AuthRequest(req) {
http.Error(w, "not allowed", http.StatusUnauthorized)
return
}
pprof.Trace(w, req)
})
handler.HandleFunc("/debug/pprof/requests", func(w http.ResponseWriter, req *http.Request) {
any := golangtrace.AuthRequest(req)
if !any {
http.Error(w, "not allowed", http.StatusUnauthorized)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
golangtrace.Render(w, req, any)
})
handler.HandleFunc("/debug/pprof/events", func(w http.ResponseWriter, req *http.Request) {
any := golangtrace.AuthRequest(req)
if !any {
http.Error(w, "not allowed", http.StatusUnauthorized)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
golangtrace.RenderEvents(w, req, any)
})
//==============================
handler.HandleFunc("/debug/pprof/vars", func(w http.ResponseWriter, req *http.Request) {
if !golangtrace.AuthRequest(req) {
http.Error(w, "not allowed", http.StatusUnauthorized)
return
}
expvar.ExpvarHandler(w, req)
})
//==============================
expvar.Publish("cmdline", expvar.Func(expvar.Cmdline))
expvar.Publish("memstats", expvar.Func(expvar.Memstats))
expvar.Publish("Goroutines", expvar.Func(goroutines))
expvar.Publish("Uptime", expvar.Func(uptime))
expvar.Publish("tracetotal", expvar.Func(traceTotal))
expvar.Publish("tracehour", expvar.Func(traceHour))
expvar.Publish("traceminute", expvar.Func(traceMinute))
expvar.Publish("chanstats", expvar.Func(chanStats))
//==============================
}