generated from labi-le/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
329 lines (277 loc) · 8.87 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package node
import (
"crypto/rand"
"crypto/rsa"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/labi-le/belphegor/internal/node/data"
"github.com/labi-le/belphegor/internal/types"
"github.com/labi-le/belphegor/pkg/clipboard"
"github.com/labi-le/belphegor/pkg/encrypter"
"github.com/rs/zerolog/log"
"net"
"os"
"os/user"
"runtime"
"time"
)
var (
ErrAlreadyConnected = errors.New("already connected")
)
type UniqueID = string
type Node struct {
clipboard clipboard.Manager
peers *Storage
localClipboard Channel
publicPort int
bitSize int
keepAliveDelay time.Duration
clipboardScanDelay time.Duration
lastMessage *data.LastMessage
writeTimeout time.Duration
// represents the current device
metadata *types.Device
}
// New creates a new instance of Node with the specified settings.
func New(
clipboard clipboard.Manager,
peers *Storage,
localClipboard Channel,
publicPort int,
bitSize int,
keepAliveDelay time.Duration,
clipboardScanDelay time.Duration,
writeTimeout time.Duration,
lastMessage *data.LastMessage,
) *Node {
return &Node{
clipboard: clipboard,
peers: peers,
localClipboard: localClipboard,
publicPort: publicPort,
bitSize: bitSize,
keepAliveDelay: keepAliveDelay,
clipboardScanDelay: clipboardScanDelay,
writeTimeout: writeTimeout,
lastMessage: lastMessage,
metadata: &types.Device{
Name: deviceName(),
Arch: runtime.GOARCH,
UniqueID: uuid.New().String(),
},
}
}
// ConnectTo establishes a TCP connection to a remote clipboard at the specified address.
// It adds the connection to the node's storage and starts handling the connection using 'handleConnection'.
// The 'addr' parameter should be in the format "host:port" to specify the remote clipboard's address.
// If the connection is successfully established, it returns nil; otherwise, it returns an error.
func (n *Node) ConnectTo(addr string) error {
conn, err := net.Dial("tcp4", addr)
if err != nil {
log.Error().AnErr("net.Dial", err).Msg("failed to handle connection")
return err
}
connErr := n.handleConnection(conn)
if connErr != nil {
log.Error().AnErr("node.handleConnection", connErr).Msg("failed to handle connection")
}
return connErr
}
func (n *Node) addPeer(hisHand *types.GreetMessage, cipher *encrypter.Cipher, conn net.Conn) (*Peer, error) {
if n.peers.Exist(hisHand.Device.UniqueID) {
log.Trace().Msgf("%s already connected, ignoring", prettyDevice(hisHand.Device))
return nil, ErrAlreadyConnected
}
if aliveErr := conn.(*net.TCPConn).SetKeepAlive(true); aliveErr != nil {
return nil, aliveErr
}
if err := conn.(*net.TCPConn).SetKeepAlivePeriod(n.keepAliveDelay); err != nil {
return nil, err
}
peer := AcquirePeer(
conn,
castAddrPort(conn),
hisHand.Device,
n.localClipboard,
cipher,
)
n.peers.Add(
hisHand.Device.UniqueID,
peer,
)
return peer, nil
}
// Start starts the node by listening for incoming connections on the specified public port.
// It also starts a clipboard monitor to periodically scan and update the local clipboard.
// When a new connection is accepted, it invokes the 'handleConnection' method to handle the connection.
// The 'scanDelay' parameter determines the interval at which the clipboard is scanned and updated.
// The method returns an error if it fails to start listening.
func (n *Node) Start() error {
const op = "node.Start"
l, err := net.Listen("tcp4", fmt.Sprintf(":%d", n.publicPort))
if err != nil {
return err
}
log.Info().Str(op, "listen").Msgf("on %s", l.Addr().String())
log.Info().Str(op, "metadata").Msg(prettyDevice(n.Metadata()))
defer l.Close()
go n.MonitorBuffer()
go n.lastMessage.ListenUpdates()
for {
conn, netErr := l.Accept()
if netErr != nil {
return err
}
log.Trace().Str(op, "accept connection").Msgf("from %s", conn.RemoteAddr().String())
go func() {
connErr := n.handleConnection(conn)
if connErr != nil {
log.Error().AnErr("node.handleConnection", connErr).Msg("failed to handle connection")
}
}()
}
}
func (n *Node) handleConnection(conn net.Conn) error {
privateKey, cipherErr := rsa.GenerateKey(rand.Reader, n.bitSize)
if cipherErr != nil {
log.Error().AnErr("rsa.GenerateKey", cipherErr).Send()
return cipherErr
}
myHand := data.NewGreet(n.metadata)
defer myHand.Release()
myHand.PublicKey = encrypter.PublicKey2Bytes(privateKey.Public())
hisHand, greetErr := n.greet(myHand, conn)
if greetErr != nil {
log.Error().AnErr("node.greet", greetErr).Send()
return greetErr
}
peer, addErr := n.addPeer(
hisHand,
encrypter.NewCipher(privateKey, encrypter.Bytes2PublicKey(hisHand.PublicKey)),
conn,
)
if addErr != nil {
if errors.Is(addErr, ErrAlreadyConnected) {
return nil
}
log.Error().AnErr("node.addPeer", addErr).Send()
return addErr
}
defer n.peers.Delete(peer.Device().GetUniqueID())
log.Info().Msgf("connected to %s", peer.String())
peer.Receive(n.lastMessage)
return nil
}
// Broadcast sends a message to all connected nodes except those specified in the 'ignore' list.
// It first checks if the message is a duplicate of the last sent message by comparing their IDs and hashes.
// If the message is a duplicate, it is not sent.
// For each connection in the storage, it writes the message to the connection's writer.
// The method logs the sent messages and their hashes for debugging purposes.
// The 'msg' parameter is the message to be broadcast.
// The 'ignore' parameter is a variadic list of AddrPort to exclude from the broadcast.
func (n *Node) Broadcast(msg *data.Message, ignore UniqueID) {
const op = "node.Broadcast"
defer msg.Release()
n.peers.Tap(func(id UniqueID, peer *Peer) {
if id == ignore {
log.Trace().Str(op, "exclude sending to creator node").Msg(peer.String())
return
}
if n.lastMessage.Duplicate(msg) {
return
}
log.Debug().Msgf(
"sent %s to %s",
msg.Header.ID,
peer.String(),
)
// Set write timeout if the writer implements net.Conn
err := peer.Conn().SetWriteDeadline(time.Now().Add(n.writeTimeout))
if err != nil {
log.Error().AnErr("net.Conn.SetWriteDeadline", err).Send()
return
}
defer peer.Conn().SetWriteDeadline(time.Time{}) // Reset the deadline when done
encData, encErr := peer.cipher.Sign(rand.Reader, encode(msg), nil)
if encErr != nil {
log.Error().AnErr("peer.cipher.Sign", encErr).Send()
}
if _, writeErr := encodeWriter(
&types.EncryptedMessage{Message: encData},
peer.Conn(),
); writeErr != nil {
log.Error().AnErr("encodeWriter", writeErr).Send()
n.peers.Delete(peer.Device().GetUniqueID())
}
})
}
func (n *Node) greet(my *data.Greet, conn net.Conn) (*types.GreetMessage, error) {
var incoming types.GreetMessage
log.Trace().Msgf("sending greeting to %s -> %s", prettyDevice(my.Device), conn.RemoteAddr().String())
if _, err := encodeWriter(my, conn); err != nil {
return &incoming, err
}
if decodeErr := decodeReader(conn, &incoming); decodeErr != nil {
return &incoming, decodeErr
}
log.Trace().Msgf("received greeting from %s -> %s", prettyDevice(incoming.Device), conn.RemoteAddr().String())
if my.Version != incoming.Version {
log.Warn().Msgf("version mismatch: %s != %s", my.Version, incoming.Version)
}
return &incoming, nil
}
// stats periodically log information about the nodes in the storage.
// It retrieves the list of nodes from the provided storage and logs the count of nodes
// as well as information about each node, including its Address and port.
func stats(storage *Storage) {
for range time.Tick(time.Minute) {
storage.Tap(func(metadata UniqueID, peer *Peer) {
log.Trace().Msgf("%s is alive", peer.String())
})
}
}
// MonitorBuffer starts monitoring the clipboard and subsequently sending data to other nodes
func (n *Node) MonitorBuffer() {
const op = "node.MonitorBuffer"
var (
currentClipboard = n.fetchClipboardData()
)
go func() {
for range time.Tick(n.clipboardScanDelay) {
newClipboard := n.fetchClipboardData()
if !newClipboard.Duplicate(currentClipboard) {
log.Trace().Str(op, "local clipboard data changed").Send()
currentClipboard = newClipboard
n.localClipboard <- currentClipboard
}
}
}()
for msg := range n.localClipboard {
n.setClipboardData(msg)
n.Broadcast(msg, msg.Header.From)
}
}
func (n *Node) fetchClipboardData() *data.Message {
clip, _ := n.clipboard.Get()
return data.MessageFrom(clip, n.Metadata())
}
func (n *Node) setClipboardData(m *data.Message) {
_ = n.clipboard.Set(m.GetData().GetRaw())
}
func (n *Node) Metadata() *types.Device {
return n.metadata
}
func deviceName() string {
hostname, hostErr := os.Hostname()
if hostErr != nil {
log.Error().AnErr("deviceName:hostname", hostErr)
return "unknown@unknown"
}
current, userErr := user.Current()
if userErr != nil {
log.Error().AnErr("deviceName:username", userErr)
return "unknown@unknown"
}
return fmt.Sprintf("%s@%s", current.Username, hostname)
}