]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/node.go
22fa3fb2e03663f81f47c7ba06633d2209ea9934
[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-2017 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         "github.com/gorhill/cronexpr"
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 ToBase32(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         Sendmail       []string
46         Incoming       *string
47         Freq           *string
48         FreqChunked    int64
49         FreqMinSize    int64
50         Via            []*NodeId
51         Addrs          map[string]string
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 type Call struct {
71         Cron           *cronexpr.Expression
72         Nice           uint8
73         Xx             *TRxTx
74         Addr           *string
75         OnlineDeadline uint
76         MaxOnlineTime  uint
77 }
78
79 func NewNodeGenerate() (*NodeOur, error) {
80         exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
81         if err != nil {
82                 return nil, err
83         }
84         signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
85         if err != nil {
86                 return nil, err
87         }
88         noiseKey := noise.DH25519.GenerateKeypair(rand.Reader)
89         noisePub := new([32]byte)
90         noisePrv := new([32]byte)
91         copy(noisePrv[:], noiseKey.Private)
92         copy(noisePub[:], noiseKey.Public)
93
94         id := NodeId(blake2b.Sum256([]byte(signPub)))
95         node := NodeOur{
96                 Id:       &id,
97                 ExchPub:  exchPub,
98                 ExchPrv:  exchPrv,
99                 SignPub:  signPub,
100                 SignPrv:  signPrv,
101                 NoisePub: noisePub,
102                 NoisePrv: noisePrv,
103         }
104         return &node, nil
105 }
106
107 func (nodeOur *NodeOur) Their() *Node {
108         return &Node{
109                 Name:    "self",
110                 Id:      nodeOur.Id,
111                 ExchPub: nodeOur.ExchPub,
112                 SignPub: nodeOur.SignPub,
113         }
114 }
115
116 func NodeIdFromString(raw string) (*NodeId, error) {
117         decoded, err := FromBase32(raw)
118         if err != nil {
119                 return nil, err
120         }
121         if len(decoded) != blake2b.Size256 {
122                 return nil, errors.New("Invalid node id size")
123         }
124         buf := new([blake2b.Size256]byte)
125         copy(buf[:], decoded)
126         nodeId := NodeId(*buf)
127         return &nodeId, nil
128 }