]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/yggdrasil/yggdrasil.go
Update dependencies
[nncp.git] / src / yggdrasil / yggdrasil.go
index 490136143bfbb0a7598c342c74ec37ac3c8e7959..010e4159ba2f2609540e0cc7c2c31fcd5fa0d088 100644 (file)
@@ -3,7 +3,7 @@
 
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2022 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -26,6 +26,7 @@ import (
        "log"
        "net"
        "net/url"
+       "regexp"
        "strconv"
        "strings"
        "sync"
@@ -41,14 +42,6 @@ import (
 
 const DefaultPort = 5400
 
-// Copy-pasted from yggdrasil-go/src/ipv6rwc/ipv6rwc.go,
-// because they are non-exportable.
-const (
-       typeKeyDummy = iota
-       typeKeyLookup
-       typeKeyResponse
-)
-
 var (
        glog *gologme.Logger
 
@@ -85,17 +78,51 @@ func ycoreStart(cfg *ycfg.NodeConfig, port int, mcasts []string) (*ycore.Core, e
                        },
                )
        }
-       core := &ycore.Core{}
-       if err := core.Start(cfg, glog); err != nil {
+
+       options := []ycore.SetupOption{
+               ycore.NodeInfo(cfg.NodeInfo),
+               ycore.NodeInfoPrivacy(cfg.NodeInfoPrivacy),
+       }
+       for _, addr := range cfg.Listen {
+               options = append(options, ycore.ListenAddress(addr))
+       }
+       for _, peer := range cfg.Peers {
+               options = append(options, ycore.Peer{URI: peer})
+       }
+       for intf, peers := range cfg.InterfacePeers {
+               for _, peer := range peers {
+                       options = append(options, ycore.Peer{URI: peer, SourceInterface: intf})
+               }
+       }
+       for _, allowed := range cfg.AllowedPublicKeys {
+               k, err := hex.DecodeString(allowed)
+               if err != nil {
+                       panic(err)
+               }
+               options = append(options, ycore.AllowedPublicKey(k[:]))
+       }
+
+       err = cfg.GenerateSelfSignedCertificate()
+       if err != nil {
+               return nil, err
+       }
+       core, err := ycore.New(cfg.Certificate, glog, options...)
+       if err != nil {
                return nil, err
        }
        if len(mcasts) > 0 {
-               mc := &ymcast.Multicast{}
-               if err := mc.Init(core, cfg, glog, nil); err != nil {
-                       core.Stop()
-                       return nil, err
+
+               options := []ymcast.SetupOption{}
+               for _, intf := range cfg.MulticastInterfaces {
+                       options = append(options, ymcast.MulticastInterface{
+                               Regex:    regexp.MustCompile(intf.Regex),
+                               Beacon:   intf.Beacon,
+                               Listen:   intf.Listen,
+                               Port:     intf.Port,
+                               Priority: uint8(intf.Priority),
+                       })
                }
-               if err := mc.Start(); err != nil {
+               if _, err = ymcast.New(core, glog, options...); err != nil {
                        core.Stop()
                        return nil, err
                }
@@ -180,7 +207,7 @@ func NewConn(aliases map[string]string, in string) (net.Conn, error) {
                return e.DialTCP(&net.TCPAddr{IP: ipTheir, Port: port})
        }
        cfg := ycfg.NodeConfig{
-               PrivateKey:      prvHex,
+               PrivateKey:      prvRaw,
                Peers:           peers,
                NodeInfo:        map[string]interface{}{"name": "NNCP"},
                NodeInfoPrivacy: true,
@@ -198,27 +225,6 @@ func NewConn(aliases map[string]string, in string) (net.Conn, error) {
        return e.DialTCP(&net.TCPAddr{IP: ipTheir, Port: port})
 }
 
-type OOBState struct {
-       c      *ycore.Core
-       subnet yaddr.Subnet
-}
-
-func (state *OOBState) Handler(fromKey, toKey ed25519.PublicKey, data []byte) {
-       if len(data) != 1+ed25519.SignatureSize {
-               return
-       }
-       if data[0] == typeKeyLookup {
-               snet := *yaddr.SubnetForKey(toKey)
-               sig := data[1:]
-               if snet == state.subnet && ed25519.Verify(fromKey, toKey[:], sig) {
-                       state.c.SendOutOfBand(fromKey, append(
-                               []byte{typeKeyResponse},
-                               ed25519.Sign(state.c.PrivateKey(), fromKey[:])...,
-                       ))
-               }
-       }
-}
-
 func NewListener(aliases map[string]string, in string) (net.Listener, error) {
        // yggdrasils://PRV[:PORT]?[bind=BIND][&pub=PUB][&peer=PEER][&mcast=REGEX[:PORT]]
        u, err := url.Parse(in)
@@ -291,7 +297,7 @@ func NewListener(aliases map[string]string, in string) (net.Listener, error) {
                return e.ListenTCP(&net.TCPAddr{IP: ipOur, Port: port})
        }
        cfg := ycfg.NodeConfig{
-               PrivateKey:        prvHex,
+               PrivateKey:        ycfg.KeyBytes(prvRaw),
                Listen:            binds,
                AllowedPublicKeys: pubs,
                Peers:             peers,
@@ -302,11 +308,6 @@ func NewListener(aliases map[string]string, in string) (net.Listener, error) {
        if err != nil {
                return nil, err
        }
-       oobState := OOBState{core, *yaddr.SubnetForKey(core.PublicKey())}
-       if err := core.SetOutOfBandHandler(oobState.Handler); err != nil {
-               core.Stop()
-               return nil, err
-       }
        e, err = NewTCPIPEndpoint(core, ipOur, uint32(core.MTU()))
        if err != nil {
                core.Stop()