Skip to content

Commit

Permalink
fix: ascii-only identify versions
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Dec 6, 2022
1 parent a3913d9 commit 6357a06
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
4 changes: 2 additions & 2 deletions core/commands/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ func printPeer(keyEnc ke.KeyEncoder, ps pstore.Peerstore, p peer.ID) (interface{

if v, err := ps.Get(p, "ProtocolVersion"); err == nil {
if vs, ok := v.(string); ok {
info.ProtocolVersion = vs
info.ProtocolVersion = version.TrimVersion(vs)
}
}
if v, err := ps.Get(p, "AgentVersion"); err == nil {
if vs, ok := v.(string); ok {
info.AgentVersion = vs
info.AgentVersion = version.TrimVersion(vs)
}
}

Expand Down
9 changes: 7 additions & 2 deletions core/commands/stat_dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"text/tabwriter"
"time"

version "github.com/ipfs/kubo"
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"

cmds "github.com/ipfs/go-ipfs-cmds"
Expand Down Expand Up @@ -92,7 +93,9 @@ This interface is not stable and may change from release to release.
info := dhtPeerInfo{ID: p.String()}

if ver, err := nd.Peerstore.Get(p, "AgentVersion"); err == nil {
info.AgentVersion, _ = ver.(string)
if vs, ok := ver.(string); ok {
info.AgentVersion = version.TrimVersion(vs)
}
} else if err == pstore.ErrNotFound {
// ignore
} else {
Expand Down Expand Up @@ -143,7 +146,9 @@ This interface is not stable and may change from release to release.
info := dhtPeerInfo{ID: pi.Id.String()}

if ver, err := nd.Peerstore.Get(pi.Id, "AgentVersion"); err == nil {
info.AgentVersion, _ = ver.(string)
if vs, ok := ver.(string); ok {
info.AgentVersion = version.TrimVersion(vs)
}
} else if err == pstore.ErrNotFound {
// ignore
} else {
Expand Down
20 changes: 11 additions & 9 deletions test/sharness/t0026-id.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,22 @@ test_expect_success "checking and converting ID of a random peer while offline"
'

# agent-version-suffix (local, offline)
test_launch_ipfs_daemon --agent-version-suffix=test-suffix
test_expect_success "checking AgentVersion with suffix (local)" '
test_id_compute_agent test-suffix > expected-agent-version &&
ipfs id -f "<aver>\n" > actual-agent-version &&
test_cmp expected-agent-version actual-agent-version
test_launch_ipfs_daemon --agent-version-suffix=test-suffix-ąę-123456789012345678901234567890
test_expect_success "checking AgentVersion with suffix (local, only ascii chars)" '
test_id_compute_agent "test-suffix--1234567890123456789" > expected &&
ipfs id -f "<aver>\n" > actual &&
test_should_not_contain "ą" actual &&
test_cmp expected actual
'

# agent-version-suffix (over libp2p identify protocol)
iptb testbed create -type localipfs -count 2 -init
startup_cluster 2 --agent-version-suffix=test-suffix-identify
startup_cluster 2 --agent-version-suffix=test-suffix-identify-óź-123456789012345678901234567890
test_expect_success "checking AgentVersion with suffix (fetched via libp2p identify protocol)" '
ipfsi 0 id -f "<aver>\n" > expected-identify-agent-version &&
ipfsi 1 id "$(ipfsi 0 config Identity.PeerID)" -f "<aver>\n" > actual-libp2p-identify-agent-version &&
test_cmp expected-identify-agent-version actual-libp2p-identify-agent-version
test_id_compute_agent "test-suffix-identify--1234567890" > expected &&
ipfsi 1 id "$(ipfsi 0 config Identity.PeerID)" -f "<aver>\n" > actual &&
test_should_not_contain "ó" actual &&
test_cmp expected actual
'
test_kill_ipfs_daemon

Expand Down
21 changes: 19 additions & 2 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ipfs

import (
"fmt"
"regexp"
"runtime"

"github.com/ipfs/kubo/repo/fsrepo"
Expand All @@ -15,6 +16,8 @@ const CurrentVersionNumber = "0.18.0-dev"

const ApiVersion = "/kubo/" + CurrentVersionNumber + "/" //nolint

const maxVersionLen = 64

// GetUserAgentVersion is the libp2p user agent used by go-ipfs.
//
// Note: This will end in `/` when no commit is available. This is expected.
Expand All @@ -26,13 +29,27 @@ func GetUserAgentVersion() string {
}
userAgent += userAgentSuffix
}
return userAgent
return TrimVersion(userAgent)
}

var userAgentSuffix string
var onlyASCII = regexp.MustCompile("[[:^ascii:]]")

func SetUserAgentSuffix(suffix string) {
userAgentSuffix = suffix
userAgentSuffix = TrimVersion(suffix)
}

func TrimVersion(version string) string {
ascii := onlyASCII.ReplaceAllLiteralString(version, "")
chars := 0
for i := range ascii {
if chars >= maxVersionLen {
ascii = ascii[:i]
break
}
chars++
}
return ascii
}

type VersionInfo struct {
Expand Down

0 comments on commit 6357a06

Please sign in to comment.