]> Cypherpunks.ru repositories - nncp.git/blob - src/node.go
aabf8477535f831cd7da681f2f7ea3dd01f65820
[nncp.git] / src / node.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package nncp
19
20 import (
21         "crypto/rand"
22         "errors"
23         "fmt"
24         "sync"
25         "time"
26
27         "github.com/flynn/noise"
28         "golang.org/x/crypto/blake2b"
29         "golang.org/x/crypto/ed25519"
30         "golang.org/x/crypto/nacl/box"
31 )
32
33 const DummyB32Id = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
34
35 type NodeId [blake2b.Size256]byte
36
37 func (id NodeId) String() string {
38         return Base32Codec.EncodeToString(id[:])
39 }
40
41 type Node struct {
42         Name           string
43         Id             *NodeId
44         ExchPub        *[32]byte
45         SignPub        ed25519.PublicKey
46         NoisePub       *[32]byte
47         Exec           map[string][]string
48         Incoming       *string
49         FreqPath       *string
50         FreqChunked    int64
51         FreqMinSize    int64
52         FreqMaxSize    int64
53         Via            []*NodeId
54         Addrs          map[string]string
55         RxRate         int
56         TxRate         int
57         OnlineDeadline time.Duration
58         MaxOnlineTime  time.Duration
59         Calls          []*Call
60
61         Busy bool
62         sync.Mutex
63 }
64
65 type NodeOur struct {
66         Id       *NodeId
67         ExchPub  *[32]byte
68         ExchPrv  *[32]byte
69         SignPub  ed25519.PublicKey
70         SignPrv  ed25519.PrivateKey
71         NoisePub *[32]byte
72         NoisePrv *[32]byte
73 }
74
75 func NewNodeGenerate() (*NodeOur, error) {
76         exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
77         if err != nil {
78                 return nil, err
79         }
80         signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
81         if err != nil {
82                 return nil, err
83         }
84         noiseKey, err := noise.DH25519.GenerateKeypair(rand.Reader)
85         if err != nil {
86                 return nil, err
87         }
88         noisePub := new([32]byte)
89         noisePrv := new([32]byte)
90         copy(noisePrv[:], noiseKey.Private)
91         copy(noisePub[:], noiseKey.Public)
92
93         id := NodeId(blake2b.Sum256([]byte(signPub)))
94         node := NodeOur{
95                 Id:       &id,
96                 ExchPub:  exchPub,
97                 ExchPrv:  exchPrv,
98                 SignPub:  signPub,
99                 SignPrv:  signPrv,
100                 NoisePub: noisePub,
101                 NoisePrv: noisePrv,
102         }
103         return &node, nil
104 }
105
106 func (nodeOur *NodeOur) Their() *Node {
107         return &Node{
108                 Name:        "self",
109                 Id:          nodeOur.Id,
110                 ExchPub:     nodeOur.ExchPub,
111                 SignPub:     nodeOur.SignPub,
112                 FreqChunked: MaxFileSize,
113                 FreqMaxSize: MaxFileSize,
114         }
115 }
116
117 func NodeIdFromString(raw string) (*NodeId, error) {
118         decoded, err := Base32Codec.DecodeString(raw)
119         if err != nil {
120                 return nil, fmt.Errorf("Can not parse node: %s: %s", raw, err)
121         }
122         if len(decoded) != blake2b.Size256 {
123                 return nil, errors.New("Invalid node id size")
124         }
125         nodeId := new(NodeId)
126         copy(nodeId[:], decoded)
127         return nodeId, nil
128 }
129
130 func (ctx *Ctx) NodeName(id *NodeId) string {
131         idS := id.String()
132         node, err := ctx.FindNode(idS)
133         if err == nil {
134                 return node.Name
135         }
136         return idS
137 }