diff --git a/cmd/poold/main.go b/cmd/poold/main.go index c68704c..e19a7b7 100644 --- a/cmd/poold/main.go +++ b/cmd/poold/main.go @@ -2,12 +2,9 @@ package main import ( "fmt" - "net" - "net/http" _ "net/http/pprof" // nolint:gosec "os" "path/filepath" - "strconv" "strings" "github.com/jessevdk/go-flags" @@ -66,23 +63,6 @@ func start() error { return err } - // Enable http profiling and Validate profile port number if reqeusted. - if config.Profile != "" { - profilePort, err := strconv.Atoi(config.Profile) - if err != nil || profilePort < 1024 || profilePort > 65535 { - return fmt.Errorf("the profile port must be between " + - "1024 and 65535") - } - - go func() { - listenAddr := net.JoinHostPort("", config.Profile) - profileRedirect := http.RedirectHandler("/debug/pprof", - http.StatusSeeOther) - http.Handle("/", profileRedirect) - fmt.Println(http.ListenAndServe(listenAddr, nil)) - }() - } - // Execute command. if parser.Active == nil { // Show the version and exit if the version flag was specified. diff --git a/config.go b/config.go index 2104ac8..2fc81c9 100644 --- a/config.go +++ b/config.go @@ -8,6 +8,7 @@ import ( "os" "path" "path/filepath" + "strconv" "time" "github.com/btcsuite/btcd/btcutil" @@ -145,7 +146,7 @@ type Config struct { LsatMaxRoutingFee btcutil.Amount `long:"lsatmaxroutingfee" description:"The maximum amount in satoshis we are willing to pay in routing fees when paying for the one-time LSAT auth token that is required to use the Pool service."` - Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65535"` + Profile string `long:"profile" description:"Enable HTTP profiling on given ip:port -- NOTE port must be between 1024 and 65535"` FakeAuth bool `long:"fakeauth" description:"Disable LSAT authentication and instead use a fake LSAT ID to identify. For testing only, cannot be set on mainnet."` TxLabelPrefix string `long:"txlabelprefix" description:"If set, then every transaction poold makes will be created with a label that has this string as a prefix."` @@ -334,6 +335,35 @@ func Validate(cfg *Config) error { ) } + // Enable http profiling and Validate profile port number if requested. + if cfg.Profile != "" { + portErr := fmt.Errorf("the profile port must be between 1024 " + + "and 65535") + inRange := func(port int) bool { + return port >= 1024 && port <= 65535 + } + + // Try to parse Profile as a host:port. + _, hostPort, err := net.SplitHostPort(cfg.Profile) + if err == nil { + // Determine if the port is valid. + profilePort, err := strconv.Atoi(hostPort) + if err != nil || !inRange(profilePort) { + return portErr + } + } else { + // Try to parse Profile as a port. + profilePort, err := strconv.Atoi(cfg.Profile) + if err != nil || !inRange(profilePort) { + return portErr + } + + // Since the user just set a port, we will serve + // debugging information over localhost. + cfg.Profile = net.JoinHostPort("127.0.0.1", cfg.Profile) + } + } + return nil } diff --git a/run.go b/run.go index 2f4d9d2..884c470 100644 --- a/run.go +++ b/run.go @@ -2,6 +2,7 @@ package pool import ( "fmt" + "net/http" "os" "path/filepath" @@ -41,6 +42,17 @@ func Run(cfg *Config) error { return err } + if cfg.Profile != "" { + go func() { + log.Infof("Pprof listening on %v", cfg.Profile) + profileRedirect := http.RedirectHandler( + "/debug/pprof", http.StatusSeeOther, + ) + http.Handle("/", profileRedirect) + fmt.Println(http.ListenAndServe(cfg.Profile, nil)) + }() + } + trader := NewServer(cfg) err = trader.Start() if err != nil {