-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_signer.go
162 lines (140 loc) · 4.14 KB
/
remote_signer.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
package signer
import (
"io"
"net"
"time"
cometcryptoed25519 "github.com/cometbft/cometbft/crypto/ed25519"
cometlog "github.com/cometbft/cometbft/libs/log"
cometnet "github.com/cometbft/cometbft/libs/net"
"github.com/cometbft/cometbft/libs/protoio"
cometservice "github.com/cometbft/cometbft/libs/service"
cometp2pconn "github.com/cometbft/cometbft/p2p/conn"
cometprotoprivval "github.com/cometbft/cometbft/proto/tendermint/privval"
)
const sleep = 1
type HorcruxConnection interface {
SendRequest(request cometprotoprivval.Message) (*cometprotoprivval.Message, error)
}
// ReconnRemoteSigner dials using its dialer and responds to any
// signature requests using its privVal.
type ReconnRemoteSigner struct {
cometservice.BaseService
address string
privKey cometcryptoed25519.PrivKey
horcruxConnection HorcruxConnection
dialer net.Dialer
}
// NewReconnRemoteSigner return a ReconnRemoteSigner that will dial using the given
// dialer and respond to any signature requests over the connection
// using the given privVal.
//
// If the connection is broken, the ReconnRemoteSigner will attempt to reconnect.
func NewReconnRemoteSigner(
address string,
logger cometlog.Logger,
horcruxConnection HorcruxConnection,
dialer net.Dialer,
) *ReconnRemoteSigner {
rs := &ReconnRemoteSigner{
address: address,
dialer: dialer,
horcruxConnection: horcruxConnection,
privKey: cometcryptoed25519.GenPrivKey(),
}
rs.BaseService = *cometservice.NewBaseService(logger, "RemoteSigner", rs)
return rs
}
// OnStart implements cmn.Service.
func (rs *ReconnRemoteSigner) OnStart() error {
go rs.loop()
return nil
}
// OnStop implements cmn.Service.
func (rs *ReconnRemoteSigner) OnStop() {
}
// main loop for ReconnRemoteSigner
func (rs *ReconnRemoteSigner) loop() {
var conn net.Conn
for {
if !rs.IsRunning() {
if conn != nil {
if err := conn.Close(); err != nil {
rs.Logger.Error("Close", "err", err.Error()+"closing listener failed")
}
}
return
}
for conn == nil {
if !rs.IsRunning() {
return
}
proto, address := cometnet.ProtocolAndAddress(rs.address)
netConn, err := rs.dialer.Dial(proto, address)
if err != nil {
rs.Logger.Error("Dialing", "err", err)
rs.Logger.Info("Retrying", "sleep (s)", sleep, "address", rs.address)
time.Sleep(time.Second * time.Duration(sleep))
continue
}
rs.Logger.Info("Connected to Sentry", "address", rs.address)
conn, err = cometp2pconn.MakeSecretConnection(netConn, rs.privKey)
if err != nil {
if err := netConn.Close(); err != nil {
rs.Logger.Error("Error closing netConn", "err", err)
}
conn = nil
rs.Logger.Error("Secret Conn", "err", err)
rs.Logger.Info("Retrying", "sleep (s)", sleep, "address", rs.address)
time.Sleep(time.Second * time.Duration(sleep))
continue
}
}
// since dialing can take time, we check running again
if !rs.IsRunning() {
if err := conn.Close(); err != nil {
rs.Logger.Error("Close", "err", err.Error()+"closing listener failed")
}
return
}
req, err := ReadMsg(conn)
if err != nil {
rs.Logger.Error("readMsg", "err", err)
conn.Close()
conn = nil
continue
}
// handleRequest handles request errors. We always send back a response
res, err := rs.horcruxConnection.SendRequest(req)
if err != nil {
rs.Logger.Error("handleRequest", "err", err)
conn.Close()
conn = nil
continue
}
if res == nil {
rs.Logger.Error("handleRequest", "err", "nil response")
conn.Close()
conn = nil
continue
}
err = WriteMsg(conn, *res)
if err != nil {
rs.Logger.Error("writeMsg", "err", err)
conn.Close()
conn = nil
}
}
}
// ReadMsg reads a message from an io.Reader
func ReadMsg(reader io.Reader) (msg cometprotoprivval.Message, err error) {
const maxRemoteSignerMsgSize = 1024 * 10
protoReader := protoio.NewDelimitedReader(reader, maxRemoteSignerMsgSize)
_, err = protoReader.ReadMsg(&msg)
return msg, err
}
// WriteMsg writes a message to an io.Writer
func WriteMsg(writer io.Writer, msg cometprotoprivval.Message) (err error) {
protoWriter := protoio.NewDelimitedWriter(writer)
_, err = protoWriter.WriteMsg(&msg)
return err
}