-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandshake.go
More file actions
179 lines (147 loc) · 5.13 KB
/
Copy pathhandshake.go
File metadata and controls
179 lines (147 loc) · 5.13 KB
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
// SPDX-License-Identifier: AGPL-3.0-or-later
package runtime
import (
"crypto/ed25519"
"fmt"
"time"
"github.com/pilot-protocol/common/coreapi"
"github.com/pilot-protocol/common/daemonapi"
"github.com/pilot-protocol/common/protocol"
registryclient "github.com/pilot-protocol/common/registry/client"
"github.com/pilot-protocol/handshake"
)
// handshakeCloseDelay matches plugins/handshake.HandshakeCloseDelay.
const handshakeCloseDelay = 500 * time.Millisecond
// HandshakeRuntime adapts daemonapi.Daemon to the handshake.Runtime
// interface. Lives here (L12 composition root) so plugins/handshake
// stays free of pkg/daemon.
type HandshakeRuntime struct{ d daemonapi.Daemon }
// NewHandshakeRuntime returns a handshake.Runtime backed by d.
func NewHandshakeRuntime(d daemonapi.Daemon) handshake.Runtime {
return HandshakeRuntime{d: d}
}
func (r HandshakeRuntime) NodeID() uint32 { return r.d.NodeID() }
func (r HandshakeRuntime) HasIdentity() bool { return r.d.Identity() != nil }
func (r HandshakeRuntime) PublicKey() ed25519.PublicKey {
id := r.d.Identity()
if id == nil {
return nil
}
return id.PublicKey
}
func (r HandshakeRuntime) Sign(msg []byte) []byte {
id := r.d.Identity()
if id == nil {
return nil
}
return id.Sign(msg)
}
func (r HandshakeRuntime) IdentityPath() string { return r.d.IdentityPath() }
func (r HandshakeRuntime) TrustAutoApprove() bool { return r.d.TrustAutoApprove() }
func (r HandshakeRuntime) IsTrusted(nodeID uint32) (string, bool) {
tc := r.d.GetTrustChecker()
if tc == nil {
return "", false
}
return tc.IsTrusted(nodeID)
}
func (r HandshakeRuntime) PublishEvent(topic string, payload map[string]any) {
r.d.PublishEvent(topic, payload)
}
func (r HandshakeRuntime) PortListener(port uint16) (coreapi.Listener, error) {
return daemonStreams{d: r.d}.Listen(port)
}
func (r HandshakeRuntime) DialAndSend(peerNodeID uint32, port uint16, data []byte) error {
peerAddr := protocol.Addr{Network: 0, Node: peerNodeID}
conn, err := r.d.DialConnection(peerAddr, port)
if err != nil {
return err
}
if err := r.d.SendData(conn, data); err != nil {
r.d.CloseConnection(conn)
return fmt.Errorf("send handshake data: %w", err)
}
go func() {
time.Sleep(handshakeCloseDelay)
r.d.CloseConnection(conn)
}()
return nil
}
func (r HandshakeRuntime) RemoveTunnelPeer(nodeID uint32) {
r.d.Tunnels().RemovePeer(nodeID)
}
func (r HandshakeRuntime) Registry() handshake.RegistryClient {
c := r.d.RegistryClient()
if c == nil {
return nil
}
return c
}
// handshakeServiceAdapter converts between plugins/handshake types and
// the daemon-local daemonapi.HandshakeService mirror types.
type handshakeServiceAdapter struct{ m *handshake.Manager }
// NewHandshakeServiceAdapter wraps a *handshake.Service so it satisfies
// daemonapi.HandshakeService.
func NewHandshakeServiceAdapter(svc *handshake.Service) daemonapi.HandshakeService {
return handshakeServiceAdapter{m: svc.Manager()}
}
func (a handshakeServiceAdapter) IsTrusted(nodeID uint32) bool { return a.m.IsTrusted(nodeID) }
func (a handshakeServiceAdapter) TrustedPeers() []daemonapi.HandshakeTrustRecord {
src := a.m.TrustedPeers()
out := make([]daemonapi.HandshakeTrustRecord, len(src))
for i, r := range src {
out[i] = daemonapi.HandshakeTrustRecord{
NodeID: r.NodeID,
PublicKey: r.PublicKey,
ApprovedAt: r.ApprovedAt,
Mutual: r.Mutual,
Network: r.Network,
}
}
return out
}
func (a handshakeServiceAdapter) PendingRequests() []daemonapi.HandshakePendingRecord {
src := a.m.PendingRequests()
out := make([]daemonapi.HandshakePendingRecord, len(src))
for i, p := range src {
out[i] = daemonapi.HandshakePendingRecord{
NodeID: p.NodeID,
PublicKey: p.PublicKey,
Justification: p.Justification,
ReceivedAt: p.ReceivedAt,
}
}
return out
}
func (a handshakeServiceAdapter) PendingCount() int { return a.m.PendingCount() }
func (a handshakeServiceAdapter) SendRequest(peerNodeID uint32, justification string) error {
return a.m.SendRequest(peerNodeID, justification)
}
func (a handshakeServiceAdapter) ApproveHandshake(peerNodeID uint32) error {
return a.m.ApproveHandshake(peerNodeID)
}
func (a handshakeServiceAdapter) RejectHandshake(peerNodeID uint32, reason string) error {
return a.m.RejectHandshake(peerNodeID, reason)
}
func (a handshakeServiceAdapter) RevokeTrust(peerNodeID uint32) error {
return a.m.RevokeTrust(peerNodeID)
}
func (a handshakeServiceAdapter) WaitForTrust(peerNodeID uint32, timeout time.Duration) bool {
return a.m.WaitForTrust(peerNodeID, timeout)
}
func (a handshakeServiceAdapter) ProcessRelayedRequest(fromNodeID uint32, justification string) {
a.m.ProcessRelayedRequest(fromNodeID, justification)
}
func (a handshakeServiceAdapter) ProcessRelayedApproval(fromNodeID uint32) {
a.m.ProcessRelayedApproval(fromNodeID)
}
func (a handshakeServiceAdapter) ProcessRelayedRejection(fromNodeID uint32) {
a.m.ProcessRelayedRejection(fromNodeID)
}
func (a handshakeServiceAdapter) Stop() { a.m.Stop() }
// Compile-time guards.
var (
_ handshake.Runtime = HandshakeRuntime{}
_ daemonapi.HandshakeService = handshakeServiceAdapter{}
_ handshake.RegistryClient = (*registryclient.Client)(nil)
)