ethstats: fix URL parser for '@' or ':' in node name/password (#21640)

Fixes the case (example below) where the value passed
to --ethstats flag would be parsed wrongly because the
node name and/or password value contained the special
characters '@' or ':'

    --ethstats "ETC Labs Metrics @meowsbits":mypass@ws://mordor.dash.fault.dev:3000
This commit is contained in:
meowsbits
2021-05-25 16:22:46 -05:00
committed by GitHub
parent 49bde05a55
commit 10962b685e
2 changed files with 97 additions and 9 deletions

View File

@ -24,7 +24,6 @@ import (
"fmt"
"math/big"
"net/http"
"regexp"
"runtime"
"strconv"
"strings"
@ -144,21 +143,43 @@ func (w *connWrapper) Close() error {
return w.conn.Close()
}
// parseEthstatsURL parses the netstats connection url.
// URL argument should be of the form <nodename:secret@host:port>
// If non-erroring, the returned slice contains 3 elements: [nodename, pass, host]
func parseEthstatsURL(url string) (parts []string, err error) {
err = fmt.Errorf("invalid netstats url: \"%s\", should be nodename:secret@host:port", url)
hostIndex := strings.LastIndex(url, "@")
if hostIndex == -1 || hostIndex == len(url)-1 {
return nil, err
}
preHost, host := url[:hostIndex], url[hostIndex+1:]
passIndex := strings.LastIndex(preHost, ":")
if passIndex == -1 {
return []string{preHost, "", host}, nil
}
nodename, pass := preHost[:passIndex], ""
if passIndex != len(preHost)-1 {
pass = preHost[passIndex+1:]
}
return []string{nodename, pass, host}, nil
}
// New returns a monitoring service ready for stats reporting.
func New(node *node.Node, backend backend, engine consensus.Engine, url string) error {
// Parse the netstats connection url
re := regexp.MustCompile("([^:@]*)(:([^@]*))?@(.+)")
parts := re.FindStringSubmatch(url)
if len(parts) != 5 {
return fmt.Errorf("invalid netstats url: \"%s\", should be nodename:secret@host:port", url)
parts, err := parseEthstatsURL(url)
if err != nil {
return err
}
ethstats := &Service{
backend: backend,
engine: engine,
server: node.Server(),
node: parts[1],
pass: parts[3],
host: parts[4],
node: parts[0],
pass: parts[1],
host: parts[2],
pongCh: make(chan struct{}),
histCh: make(chan []uint64, 1),
}