diff --git a/cmd/cmd.go b/cmd/cmd.go index 7f4276ea8..7aad0a63b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -373,7 +373,7 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string, // The passwordFetcher will be used to fetch the password for the default_wallet if it is encrypted. // It returns an error if the genesis doc or default_wallet can't be found inside the working directory. // TODO: write test for me. -func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool), enableDebugger bool) ( +func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool)) ( *node.Node, *wallet.Wallet, error, ) { conf, gen, err := MakeConfig(workingDir) @@ -381,8 +381,6 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, return nil, nil, err } - conf.HTTP.EnableDebugger = enableDebugger - defaultWalletPath := PactusDefaultWalletPath(workingDir) walletInstance, err := wallet.Open(defaultWalletPath, true, wallet.WithCustomServers([]string{conf.GRPC.Listen})) diff --git a/cmd/daemon/start.go b/cmd/daemon/start.go index baec62a38..7565aa607 100644 --- a/cmd/daemon/start.go +++ b/cmd/daemon/start.go @@ -24,8 +24,6 @@ func buildStartCmd(parentCmd *cobra.Command) { passwordOpt := startCmd.Flags().StringP("password", "p", "", "the wallet password") - debuggerOpt := startCmd.Flags().BoolP("debug", "d", false, "enable pprof debugger") - startCmd.Run = func(_ *cobra.Command, _ []string) { workingDir, _ := filepath.Abs(*workingDirOpt) // change working directory @@ -60,7 +58,7 @@ func buildStartCmd(parentCmd *cobra.Command) { return password, true } node, _, err := cmd.StartNode( - workingDir, passwordFetcher, *debuggerOpt) + workingDir, passwordFetcher) cmd.FatalErrorCheck(err) cmd.TrapSignal(func() { diff --git a/cmd/gtk/main.go b/cmd/gtk/main.go index 382b48247..d2d880bcd 100644 --- a/cmd/gtk/main.go +++ b/cmd/gtk/main.go @@ -25,7 +25,6 @@ const appID = "com.github.pactus-project.pactus.pactus-gui" var ( workingDirOpt *string - debuggerOpt *bool passwordOpt *string testnetOpt *bool ) @@ -34,7 +33,6 @@ func init() { workingDirOpt = flag.String("working-dir", cmd.PactusDefaultHomeDir(), "working directory path") passwordOpt = flag.String("password", "", "wallet password") testnetOpt = flag.Bool("testnet", false, "initializing for the testnet") - debuggerOpt = flag.Bool("debug", false, "enable pprof debugger") version.NodeAgent.AppType = "gui" // the gtk on macos should run on main thread. if runtime.GOOS == "darwin" { @@ -157,7 +155,7 @@ func newNode(workingDir string) (*node.Node, *wallet.Wallet, error) { return getWalletPassword(wlt) } - n, wlt, err := cmd.StartNode(workingDir, passwordFetcher, *debuggerOpt) + n, wlt, err := cmd.StartNode(workingDir, passwordFetcher) if err != nil { return nil, nil, err } diff --git a/config/config.go b/config/config.go index e7f8b8259..f61d070af 100644 --- a/config/config.go +++ b/config/config.go @@ -152,6 +152,7 @@ func DefaultConfigMainnet() *Config { conf.JSONRPC.Listen = "127.0.0.1:8545" conf.HTTP.Enable = false conf.HTTP.Listen = "127.0.0.1:80" + conf.HTTP.EnablePprof = false conf.Nanomsg.Enable = false conf.Nanomsg.Listen = "tcp://127.0.0.1:40899" @@ -187,6 +188,7 @@ func DefaultConfigTestnet() *Config { conf.JSONRPC.Listen = "127.0.0.1:8545" conf.HTTP.Enable = false conf.HTTP.Listen = "[::]:80" + conf.HTTP.EnablePprof = false conf.Nanomsg.Enable = false conf.Nanomsg.Listen = "tcp://[::]:40799" @@ -214,6 +216,7 @@ func DefaultConfigLocalnet() *Config { conf.JSONRPC.Listen = "127.0.0.1:8545" conf.HTTP.Enable = true conf.HTTP.Listen = "[::]:0" + conf.HTTP.EnablePprof = true conf.Nanomsg.Enable = true conf.Nanomsg.Listen = "tcp://[::]:40799" diff --git a/config/example_config.toml b/config/example_config.toml index f5e313de7..dbf492274 100644 --- a/config/example_config.toml +++ b/config/example_config.toml @@ -213,6 +213,11 @@ # `listen` is the address to listen for incoming connections for HTTP server. listen = "127.0.0.1:80" + # `enable_pprof` Enables Golang's pprof debugger for profiling CPU, memory, and goroutines, + # providing key performance insights. Be cautious as it exposes sensitive + # data, so enable only in secure environments with restricted access. + enable_pprof = false + # Nanomsg configuration. [nanomsg] diff --git a/www/http/config.go b/www/http/config.go index 065e66687..26112746d 100644 --- a/www/http/config.go +++ b/www/http/config.go @@ -1,16 +1,16 @@ package http type Config struct { - Enable bool `toml:"enable"` - Listen string `toml:"listen"` - EnableDebugger bool `toml:"-"` // EnableDebugger is private configs + Enable bool `toml:"enable"` + Listen string `toml:"listen"` + EnablePprof bool `toml:"enable_pprof"` } func DefaultConfig() *Config { return &Config{ - Enable: false, - Listen: "", - EnableDebugger: false, + Enable: false, + Listen: "", + EnablePprof: false, } } diff --git a/www/http/server.go b/www/http/server.go index fd5992515..32fb85259 100644 --- a/www/http/server.go +++ b/www/http/server.go @@ -37,6 +37,12 @@ type Server struct { logger *logger.SubLogger } +// init disables default pprof handlers registered by importing net/http/pprof. +// Your pprof is showing (https://mmcloughlin.com/posts/your-pprof-is-showing) +func init() { + http.DefaultServeMux = http.NewServeMux() +} + func NewServer(conf *Config, enableAuth bool) *Server { ctx, cancel := context.WithCancel(context.Background()) @@ -87,8 +93,11 @@ func (s *Server) StartServer(grpcServer string) error { s.router.HandleFunc("/validator/number/{number}", s.GetValidatorByNumberHandler) s.router.HandleFunc("/metrics/prometheus", promhttp.Handler().ServeHTTP) - if s.config.EnableDebugger { - s.router.HandleFunc("/debug/pprof/", pprof.Index) + if s.config.EnablePprof { + http.HandleFunc("/debug/pprof/", pprof.Index) + http.HandleFunc("/debug/pprof/profile", pprof.Profile) + http.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + http.HandleFunc("/debug/pprof/trace", pprof.Trace) } if s.enableAuth {