]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/node.go
-maxonlinetime option
[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         Via            []*NodeId
49         Addrs          map[string]string
50         OnlineDeadline uint
51         MaxOnlineTime  uint
52         Calls          []*Call
53
54         Busy bool
55         sync.Mutex
56 }
57
58 type NodeOur struct {
59         Id       *NodeId
60         ExchPub  *[32]byte
61         ExchPrv  *[32]byte
62         SignPub  ed25519.PublicKey
63         SignPrv  ed25519.PrivateKey
64         NoisePub *[32]byte
65         NoisePrv *[32]byte
66 }
67
68 type Call struct {
69         Cron           *cronexpr.Expression
70         Nice           uint8
71         Xx             *TRxTx
72         Addr           *string
73         OnlineDeadline uint
74         MaxOnlineTime  uint
75 }
76
77 func NewNodeGenerate() (*NodeOur, error) {
78         exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
79         if err != nil {
80                 return nil, err
81         }
82         signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
83         if err != nil {
84                 return nil, err
85         }
86         noiseKey := noise.DH25519.GenerateKeypair(rand.Reader)
87         noisePub := new([32]byte)
88         noisePrv := new([32]byte)
89         copy(noisePrv[:], noiseKey.Private)
90         copy(noisePub[:], noiseKey.Public)
91
92         id := NodeId(blake2b.Sum256([]byte(signPub)))
93         node := NodeOur{
94                 Id:       &id,
95                 ExchPub:  exchPub,
96                 ExchPrv:  exchPrv,
97                 SignPub:  signPub,
98                 SignPrv:  signPrv,
99                 NoisePub: noisePub,
100                 NoisePrv: noisePrv,
101         }
102         return &node, nil
103 }
104
105 func (nodeOur *NodeOur) Their() *Node {
106         return &Node{
107                 Name:    "self",
108                 Id:      nodeOur.Id,
109                 ExchPub: nodeOur.ExchPub,
110                 SignPub: nodeOur.SignPub,
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 }