]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/node.go
e903567bcdbff6befad0842cf61252938ff13b9d
[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, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package nncp
20
21 import (
22         "crypto/rand"
23         "errors"
24         "sync"
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         Freq           *string
47         FreqChunked    int64
48         FreqMinSize    int64
49         Via            []*NodeId
50         Addrs          map[string]string
51         RxRate         int
52         TxRate         int
53         OnlineDeadline uint
54         MaxOnlineTime  uint
55         Calls          []*Call
56
57         Busy bool
58         sync.Mutex
59 }
60
61 type NodeOur struct {
62         Id       *NodeId
63         ExchPub  *[32]byte
64         ExchPrv  *[32]byte
65         SignPub  ed25519.PublicKey
66         SignPrv  ed25519.PrivateKey
67         NoisePub *[32]byte
68         NoisePrv *[32]byte
69 }
70
71 func NewNodeGenerate() (*NodeOur, error) {
72         exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
73         if err != nil {
74                 return nil, err
75         }
76         signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
77         if err != nil {
78                 return nil, err
79         }
80         noiseKey, err := noise.DH25519.GenerateKeypair(rand.Reader)
81         if err != nil {
82                 return nil, err
83         }
84         noisePub := new([32]byte)
85         noisePrv := new([32]byte)
86         copy(noisePrv[:], noiseKey.Private)
87         copy(noisePub[:], noiseKey.Public)
88
89         id := NodeId(blake2b.Sum256([]byte(signPub)))
90         node := NodeOur{
91                 Id:       &id,
92                 ExchPub:  exchPub,
93                 ExchPrv:  exchPrv,
94                 SignPub:  signPub,
95                 SignPrv:  signPrv,
96                 NoisePub: noisePub,
97                 NoisePrv: noisePrv,
98         }
99         return &node, nil
100 }
101
102 func (nodeOur *NodeOur) Their() *Node {
103         return &Node{
104                 Name:    "self",
105                 Id:      nodeOur.Id,
106                 ExchPub: nodeOur.ExchPub,
107                 SignPub: nodeOur.SignPub,
108         }
109 }
110
111 func NodeIdFromString(raw string) (*NodeId, error) {
112         decoded, err := FromBase32(raw)
113         if err != nil {
114                 return nil, err
115         }
116         if len(decoded) != blake2b.Size256 {
117                 return nil, errors.New("Invalid node id size")
118         }
119         buf := new([blake2b.Size256]byte)
120         copy(buf[:], decoded)
121         nodeId := NodeId(*buf)
122         return &nodeId, nil
123 }