multi: make profile IP configurable

This commit is contained in:
Oliver Gugger 2022-09-23 09:44:46 +02:00
parent a12246c47a
commit 819670fa82
No known key found for this signature in database
GPG key ID: 8E4256593F177720
3 changed files with 43 additions and 21 deletions

View file

@ -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.

View file

@ -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
}

12
run.go
View file

@ -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 {