fix: exclude tor uris from lsps1 info response (#535)

This commit is contained in:
Roland 2024-06-28 21:21:08 +07:00 committed by GitHub
parent 11db49fd38
commit b54ed570bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 3 deletions

View file

@ -16,6 +16,7 @@ import (
"github.com/getAlby/nostr-wallet-connect/lnclient"
"github.com/getAlby/nostr-wallet-connect/logger"
"github.com/getAlby/nostr-wallet-connect/lsp"
"github.com/getAlby/nostr-wallet-connect/utils"
decodepay "github.com/nbd-wtf/ln-decodepay"
"github.com/sirupsen/logrus"
)
@ -174,20 +175,28 @@ func (api *api) getLSPS1LSPInfo(url string) (*lspInfo, error) {
return nil, fmt.Errorf("failed to deserialize json %s %s", url, string(body))
}
uri := lsps1LspInfo.URIs[0]
httpUris := utils.Filter(lsps1LspInfo.URIs, func(uri string) bool {
return !strings.Contains(uri, ".onion")
})
if len(httpUris) == 0 {
logger.Logger.WithField("uris", lsps1LspInfo.URIs).WithError(err).Error("Couldn't find HTTP URI")
return nil, err
}
uri := httpUris[0]
// make sure it's a valid IPv4 URI
regex := regexp.MustCompile(`^([0-9a-f]+)@([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):([0-9]+)$`)
parts := regex.FindStringSubmatch(uri)
logger.Logger.WithField("parts", parts).Info("Split URI")
if parts == nil || len(parts) != 4 {
logger.Logger.WithField("parts", parts).Info("Unsupported URI")
logger.Logger.WithField("parts", parts).Error("Unsupported URI")
return nil, errors.New("could not decode LSP URI")
}
port, err := strconv.Atoi(parts[3])
if err != nil {
logger.Logger.WithField("port", parts[3]).WithError(err).Info("Failed to decode port number")
logger.Logger.WithField("port", parts[3]).WithError(err).Error("Failed to decode port number")
return nil, err
}

View file

@ -44,3 +44,14 @@ func ReadFileTail(filePath string, maxLen int) (data []byte, err error) {
return data, nil
}
// filters values from a slice
func Filter[T any](s []T, f func(T) bool) []T {
var r []T
for _, v := range s {
if f(v) {
r = append(r, v)
}
}
return r
}