Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add functions to tell if an address contains a security protocol #160

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions multiaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,14 @@ func (m *multiaddr) ValueForProtocol(code int) (value string, err error) {
})
return
}

func ContainsSecurityProtocol(addr Multiaddr) bool {
var contains bool
ForEach(addr, func(c Component) bool {
if code := c.Protocol().Code; code == P_TLS || code == P_NOISE {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I would definitely put this in an extendable map.
  • This is also somewhat ambiguous. For example, what about wss? What about quic? This kind of check will likely fit better in the swarm itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I would definitely put this in an extendable map.

Makes sense.

  • This is also somewhat ambiguous. For example, what about wss? What about quic? This kind of check will likely fit better in the swarm itself.

True, this should probably be better documented.
I'm not sure if putting it in the swarm is the right solution though. We'll need a function like this (maybe a SecurityProtocol(Multiaddr) string is even better?) in quite a lot of packages, especially as we'll have a lengthy transition period.

contains = true
}
return true
})
return contains
}
30 changes: 30 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,33 @@ func TestComponentJSONMarshaler(t *testing.T) {
t.Error("expected equal components in circular marshaling test")
}
}

func TestContainsSecurity(t *testing.T) {
does := []string{
"/ip4/127.0.0.1/tcp/80/tls",
"/ip6/::1/tcp/4321/noise",
}
for _, s := range does {
addr, err := NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
if !ContainsSecurityProtocol(addr) {
t.Fatalf("expected %s to be recognized as a security protocol", s)
}
}
doesnt := []string{
"/ip4/127.0.0.1/tcp/80",
"/ip6/::1/tcp/4321",
"/ip4/127.0.0.1/udp/443/quic",
}
for _, s := range doesnt {
addr, err := NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
if ContainsSecurityProtocol(addr) {
t.Fatalf("didn't expect %s to be recognized as a security protocol", s)
}
}
}