/
node.go
176 lines (159 loc) · 3.24 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package SealP2P
import (
"crypto/rsa"
"sync"
"github.com/SealSC/SealP2P/tools/grsa"
"crypto/x509"
"github.com/SealSC/SealP2P/conn/msg"
"github.com/SealSC/SealP2P/tools/gio"
"errors"
"github.com/SealSC/SealP2P/tools/ip"
"github.com/SealSC/SealP2P/conf"
)
type Node struct {
conf conf.Config
key *rsa.PrivateKey
network *Network
h Handler
status NodeStatus
}
func (n *Node) SetMessenger(m Messenger) {
if n.h != nil {
n.h.SetMessenger(m)
}
}
func (n *Node) GetPubKey() []byte {
return x509.MarshalPKCS1PublicKey(&n.key.PublicKey)
}
func (n *Node) GetNodeID() string {
return n.conf.ID
}
func (n *Node) GetNodeStatus() NodeStatus {
n.status.Dis = n.network.Discoverer.Started()
n.status.Ser = n.network.Connector.Started()
return n.status
}
func (n *Node) GetNodeList() (list []NodeInfo) {
nodeList := n.network.NodeList()
for _, node := range nodeList {
list = append(list, NodeInfo{
ID: node.NodeID,
Addr: node.Addr,
Type: node.conn.Type(),
})
}
return list
}
func (n *Node) Join() error {
if !n.conf.ClientOnly {
err := n.network.Connector.Listen()
if err != nil {
return err
}
}
err := n.network.Discoverer.Listen()
if err != nil {
return err
}
err = n.network.Online(n.status.IP)
if err != nil {
return err
}
return nil
}
func (n *Node) Leave() error {
if err := n.network.Discoverer.Offline(); err != nil {
return err
}
n.network.Discoverer.Stop()
n.network.Connector.Stop()
return nil
}
func (n *Node) SendMsg(data *msg.Message) error {
if data == nil {
return nil
}
if len(data.ToID) < 1 {
return errors.New("invalid send destination")
}
data.Action = msg.ActionDail
conn, ok := n.network.Connector.GetConn(data.ToID[0])
if ok {
conn.Write(data)
}
return nil
}
func (n *Node) MulticastMsg(data *msg.Message) {
if data == nil {
return
}
idSet := map[string]struct{}{}
for i := range data.ToID {
idSet[data.ToID[i]] = struct{}{}
}
data.Action = msg.ActionMulticast
for s := range idSet {
conn, ok := n.network.Connector.GetConn(s)
if ok {
conn.Write(data)
}
}
}
func (n *Node) BroadcastMsg(data *msg.Message) error {
if data == nil {
return nil
}
data.Action = msg.ActionBroadcast
return n.network.Discoverer.SendMsg(data)
}
func (n *Node) MsgProcessorRegister(router string, f func(req *msg.Message) *msg.Message) {
n.h.RegisterHandler(router, f)
}
var localNode *Node
var newLock sync.Mutex
func LocalNode() NetNode {
return localNode
}
func InitLocalNode(conf conf.Config) error {
newLock.Lock()
newLock.Unlock()
if localNode != nil {
return nil
}
n := &Node{h: NewDefaultHandler(), conf: conf}
key, err := readRSA(conf.PKFile)
if err != nil {
return err
}
n.key = key
available, err := ip.Available()
if err != nil {
return err
}
if conf.ID == "" {
conf.ID = grsa.PubSha1(key)
}
n.status = NodeStatus{
ID: conf.ID,
IP: available,
}
network, err := NewNetwork(conf, n.h)
if err != nil {
return err
}
n.network = network
localNode = n
return nil
}
func readRSA(pkFile string) (pk *rsa.PrivateKey, err error) {
//Read the configuration if it exists
if gio.FileExist(pkFile) {
return grsa.LoadFile(pkFile)
}
pk, err = grsa.RandKey()
if err != nil {
return nil, err
}
err = grsa.SaveFile(pkFile, pk)
return pk, err
}