]> Cypherpunks.ru repositories - nncp.git/blob - src/node.go
b2a598546d54b72e41c1fbc05f601a227b6c964c
[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 type NodeId [blake2b.Size256]byte
34
35 func (id NodeId) String() string {
36         return Base32Codec.EncodeToString(id[:])
37 }
38
39 type Node struct {
40         Name           string
41         Id             *NodeId
42         ExchPub        *[32]byte
43         SignPub        ed25519.PublicKey
44         NoisePub       *[32]byte
45         Exec           map[string][]string
46         Incoming       *string
47         FreqPath       *string
48         FreqChunked    int64
49         FreqMinSize    int64
50         FreqMaxSize    int64
51         Via            []*NodeId
52         Addrs          map[string]string
53         RxRate         int
54         TxRate         int
55         OnlineDeadline time.Duration
56         MaxOnlineTime  time.Duration
57         Calls          []*Call
58
59         Busy bool
60         sync.Mutex
61 }
62
63 type NodeOur struct {
64         Id       *NodeId
65         ExchPub  *[32]byte
66         ExchPrv  *[32]byte
67         SignPub  ed25519.PublicKey
68         SignPrv  ed25519.PrivateKey
69         NoisePub *[32]byte
70         NoisePrv *[32]byte
71 }
72
73 func NewNodeGenerate() (*NodeOur, error) {
74         exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
75         if err != nil {
76                 return nil, err
77         }
78         signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
79         if err != nil {
80                 return nil, err
81         }
82         noiseKey, err := noise.DH25519.GenerateKeypair(rand.Reader)
83         if err != nil {
84                 return nil, err
85         }
86         noisePub := new([32]byte)
87         noisePrv := new([32]byte)
88         copy(noisePrv[:], noiseKey.Private)
89         copy(noisePub[:], noiseKey.Public)
90
91         id := NodeId(blake2b.Sum256([]byte(signPub)))
92         node := NodeOur{
93                 Id:       &id,
94                 ExchPub:  exchPub,
95                 ExchPrv:  exchPrv,
96                 SignPub:  signPub,
97                 SignPrv:  signPrv,
98                 NoisePub: noisePub,
99                 NoisePrv: noisePrv,
100         }
101         return &node, nil
102 }
103
104 func (nodeOur *NodeOur) Their() *Node {
105         return &Node{
106                 Name:        "self",
107                 Id:          nodeOur.Id,
108                 ExchPub:     nodeOur.ExchPub,
109                 SignPub:     nodeOur.SignPub,
110                 FreqChunked: MaxFileSize,
111                 FreqMaxSize: MaxFileSize,
112         }
113 }
114
115 func NodeIdFromString(raw string) (*NodeId, error) {
116         decoded, err := Base32Codec.DecodeString(raw)
117         if err != nil {
118                 return nil, fmt.Errorf("Can not parse node: %s: %s", raw, err)
119         }
120         if len(decoded) != blake2b.Size256 {
121                 return nil, errors.New("Invalid node id size")
122         }
123         nodeId := new(NodeId)
124         copy(nodeId[:], decoded)
125         return nodeId, nil
126 }
127
128 func (ctx *Ctx) NodeName(id *NodeId) string {
129         idS := id.String()
130         node, err := ctx.FindNode(idS)
131         if err == nil {
132                 return node.Name
133         }
134         return idS
135 }