]> Cypherpunks.ru repositories - nncp.git/blob - src/node.go
Remove unnecessary Duration*Second calculation
[nncp.git] / src / node.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2020 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         "sync"
24         "time"
25
26         "github.com/flynn/noise"
27         "golang.org/x/crypto/blake2b"
28         "golang.org/x/crypto/ed25519"
29         "golang.org/x/crypto/nacl/box"
30 )
31
32 type NodeId [blake2b.Size256]byte
33
34 func (id NodeId) String() string {
35         return ToBase32(id[:])
36 }
37
38 type Node struct {
39         Name           string
40         Id             *NodeId
41         ExchPub        *[32]byte
42         SignPub        ed25519.PublicKey
43         NoisePub       *[32]byte
44         Exec           map[string][]string
45         Incoming       *string
46         FreqPath       *string
47         FreqChunked    int64
48         FreqMinSize    int64
49         FreqMaxSize    int64
50         Via            []*NodeId
51         Addrs          map[string]string
52         RxRate         int
53         TxRate         int
54         OnlineDeadline time.Duration
55         MaxOnlineTime  time.Duration
56         Calls          []*Call
57
58         Busy bool
59         sync.Mutex
60 }
61
62 type NodeOur struct {
63         Id       *NodeId
64         ExchPub  *[32]byte
65         ExchPrv  *[32]byte
66         SignPub  ed25519.PublicKey
67         SignPrv  ed25519.PrivateKey
68         NoisePub *[32]byte
69         NoisePrv *[32]byte
70 }
71
72 func NewNodeGenerate() (*NodeOur, error) {
73         exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
74         if err != nil {
75                 return nil, err
76         }
77         signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
78         if err != nil {
79                 return nil, err
80         }
81         noiseKey, err := noise.DH25519.GenerateKeypair(rand.Reader)
82         if err != nil {
83                 return nil, err
84         }
85         noisePub := new([32]byte)
86         noisePrv := new([32]byte)
87         copy(noisePrv[:], noiseKey.Private)
88         copy(noisePub[:], noiseKey.Public)
89
90         id := NodeId(blake2b.Sum256([]byte(signPub)))
91         node := NodeOur{
92                 Id:       &id,
93                 ExchPub:  exchPub,
94                 ExchPrv:  exchPrv,
95                 SignPub:  signPub,
96                 SignPrv:  signPrv,
97                 NoisePub: noisePub,
98                 NoisePrv: noisePrv,
99         }
100         return &node, nil
101 }
102
103 func (nodeOur *NodeOur) Their() *Node {
104         return &Node{
105                 Name:        "self",
106                 Id:          nodeOur.Id,
107                 ExchPub:     nodeOur.ExchPub,
108                 SignPub:     nodeOur.SignPub,
109                 FreqChunked: MaxFileSize,
110                 FreqMaxSize: MaxFileSize,
111         }
112 }
113
114 func NodeIdFromString(raw string) (*NodeId, error) {
115         decoded, err := FromBase32(raw)
116         if err != nil {
117                 return nil, err
118         }
119         if len(decoded) != blake2b.Size256 {
120                 return nil, errors.New("Invalid node id size")
121         }
122         buf := new([blake2b.Size256]byte)
123         copy(buf[:], decoded)
124         nodeId := NodeId(*buf)
125         return &nodeId, nil
126 }