1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 "github.com/flynn/noise"
26 "golang.org/x/crypto/blake2b"
27 "golang.org/x/crypto/ed25519"
28 "golang.org/x/crypto/nacl/box"
31 const DummyB32Id = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
33 type NodeId [blake2b.Size256]byte
35 func (id NodeId) String() string {
36 return Base32Codec.EncodeToString(id[:])
43 SignPub ed25519.PublicKey
45 Exec map[string][]string
54 Addrs map[string]string
57 OnlineDeadline time.Duration
58 MaxOnlineTime time.Duration
69 SignPub ed25519.PublicKey
70 SignPrv ed25519.PrivateKey
75 func NewNodeGenerate() (*NodeOur, error) {
76 exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
80 signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
84 noiseKey, err := noise.DH25519.GenerateKeypair(rand.Reader)
88 noisePub := new([32]byte)
89 noisePrv := new([32]byte)
90 copy(noisePrv[:], noiseKey.Private)
91 copy(noisePub[:], noiseKey.Public)
93 id := NodeId(blake2b.Sum256([]byte(signPub)))
106 func (nodeOur *NodeOur) Their() *Node {
110 ExchPub: nodeOur.ExchPub,
111 SignPub: nodeOur.SignPub,
112 FreqChunked: MaxFileSize,
113 FreqMaxSize: MaxFileSize,
117 func NodeIdFromString(raw string) (*NodeId, error) {
118 decoded, err := Base32Codec.DecodeString(raw)
120 return nil, fmt.Errorf("Can not parse node: %s: %s", raw, err)
122 if len(decoded) != blake2b.Size256 {
123 return nil, errors.New("Invalid node id size")
125 nodeId := new(NodeId)
126 copy(nodeId[:], decoded)
130 func (ctx *Ctx) NodeName(id *NodeId) string {
132 node, err := ctx.FindNode(idS)