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