Skip to content

Commit 377bc66

Browse files
committed
The AllowedPublicKeys option should not apply to multicast listeners
Another fix for #1141.
1 parent d1b8495 commit 377bc66

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

src/core/api.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ func (c *Core) GetSessions() []SessionInfo {
150150
// parsed from a string of the form e.g. "tcp://a.b.c.d:e". In the case of a
151151
// link-local address, the interface should be provided as the second argument.
152152
func (c *Core) Listen(u *url.URL, sintf string) (*Listener, error) {
153-
return c.links.listen(u, sintf)
153+
return c.links.listen(u, sintf, false)
154+
}
155+
156+
// ListenLocal starts a listener, like the Listen function, but is used for
157+
// more trustworthy situations where you want to ignore AllowedPublicKeys, i.e.
158+
// with multicast listeners.
159+
func (c *Core) ListenLocal(u *url.URL, sintf string) (*Listener, error) {
160+
return c.links.listen(u, sintf, true)
154161
}
155162

156163
// Address gets the IPv6 address of the Yggdrasil node. This is always a /128

src/core/core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func New(cert *tls.Certificate, logger Logger, opts ...SetupOption) (*Core, erro
127127
c.log.Errorf("Invalid listener URI %q specified, ignoring\n", listenaddr)
128128
continue
129129
}
130-
if _, err = c.links.listen(u, ""); err != nil {
130+
if _, err = c.links.listen(u, "", false); err != nil {
131131
c.log.Errorf("Failed to start listener %q: %s\n", listenaddr, err)
132132
}
133133
}

src/core/link.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func (l *links) add(u *url.URL, sintf string, linkType linkType) error {
336336

337337
// Give the connection to the handler. The handler will block
338338
// for the lifetime of the connection.
339-
if err = l.handler(linkType, options, lc, resetBackoff); err != nil && err != io.EOF {
339+
if err = l.handler(linkType, options, lc, resetBackoff, false); err != nil && err != io.EOF {
340340
l.core.log.Debugf("Link %s error: %s\n", info.uri, err)
341341
}
342342

@@ -395,7 +395,7 @@ func (l *links) remove(u *url.URL, sintf string, _ linkType) error {
395395
return retErr
396396
}
397397

398-
func (l *links) listen(u *url.URL, sintf string) (*Listener, error) {
398+
func (l *links) listen(u *url.URL, sintf string, local bool) (*Listener, error) {
399399
ctx, cancel := context.WithCancel(l.core.ctx)
400400
var protocol linkProtocol
401401
switch strings.ToLower(u.Scheme) {
@@ -522,7 +522,7 @@ func (l *links) listen(u *url.URL, sintf string) (*Listener, error) {
522522

523523
// Give the connection to the handler. The handler will block
524524
// for the lifetime of the connection.
525-
switch err = l.handler(linkTypeIncoming, options, lc, nil); {
525+
switch err = l.handler(linkTypeIncoming, options, lc, nil, local); {
526526
case err == nil:
527527
case errors.Is(err, io.EOF):
528528
case errors.Is(err, net.ErrClosed):
@@ -563,7 +563,7 @@ func (l *links) connect(ctx context.Context, u *url.URL, info linkInfo, options
563563
return dialer.dial(ctx, u, info, options)
564564
}
565565

566-
func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, success func()) error {
566+
func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, success func(), local bool) error {
567567
meta := version_getBaseMetadata()
568568
meta.publicKey = l.core.public
569569
meta.priority = options.priority
@@ -606,19 +606,21 @@ func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, s
606606
}
607607
}
608608
// Check if we're authorized to connect to this key / IP
609-
var allowed map[[32]byte]struct{}
610-
phony.Block(l.core, func() {
611-
allowed = l.core.config._allowedPublicKeys
612-
})
613-
isallowed := len(allowed) == 0
614-
for k := range allowed {
615-
if bytes.Equal(k[:], meta.publicKey) {
616-
isallowed = true
617-
break
609+
if !local {
610+
var allowed map[[32]byte]struct{}
611+
phony.Block(l.core, func() {
612+
allowed = l.core.config._allowedPublicKeys
613+
})
614+
isallowed := len(allowed) == 0
615+
for k := range allowed {
616+
if bytes.Equal(k[:], meta.publicKey) {
617+
isallowed = true
618+
break
619+
}
620+
}
621+
if linkType == linkTypeIncoming && !isallowed {
622+
return fmt.Errorf("node public key %q is not in AllowedPublicKeys", hex.EncodeToString(meta.publicKey))
618623
}
619-
}
620-
if linkType == linkTypeIncoming && !isallowed {
621-
return fmt.Errorf("node public key %q is not in AllowedPublicKeys", hex.EncodeToString(meta.publicKey))
622624
}
623625

624626
dir := "outbound"

src/multicast/multicast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ func (m *Multicast) _announce() {
327327
Host: net.JoinHostPort(addrIP.String(), fmt.Sprintf("%d", info.port)),
328328
RawQuery: v.Encode(),
329329
}
330-
if li, err := m.core.Listen(u, iface.Name); err == nil {
330+
if li, err := m.core.ListenLocal(u, iface.Name); err == nil {
331331
m.log.Debugln("Started multicasting on", iface.Name)
332332
// Store the listener so that we can stop it later if needed
333333
linfo = &listenerInfo{listener: li, time: time.Now(), port: info.port}

0 commit comments

Comments
 (0)