]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/node.go
nncp-caller command
[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 int
51         Calls          []*Call
52
53         Busy bool
54         sync.Mutex
55 }
56
57 type NodeOur struct {
58         Id       *NodeId
59         ExchPub  *[32]byte
60         ExchPrv  *[32]byte
61         SignPub  ed25519.PublicKey
62         SignPrv  ed25519.PrivateKey
63         NoisePub *[32]byte
64         NoisePrv *[32]byte
65 }
66
67 type Call struct {
68         Cron           *cronexpr.Expression
69         Nice           uint8
70         Xx             *TRxTx
71         Addr           *string
72         OnlineDeadline int
73 }
74
75 func NewNodeGenerate() (*NodeOur, error) {
76         exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
77         if err != nil {
78                 return nil, err
79         }
80         signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
81         if err != nil {
82                 return nil, err
83         }
84         noiseKey := noise.DH25519.GenerateKeypair(rand.Reader)
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         }
110 }
111
112 func NodeIdFromString(raw string) (*NodeId, error) {
113         decoded, err := FromBase32(raw)
114         if err != nil {
115                 return nil, err
116         }
117         if len(decoded) != blake2b.Size256 {
118                 return nil, errors.New("Invalid node id size")
119         }
120         buf := new([blake2b.Size256]byte)
121         copy(buf[:], decoded)
122         nodeId := NodeId(*buf)
123         return &nodeId, nil
124 }