/
commands.go
353 lines (298 loc) · 11.3 KB
/
commands.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright (c) 2014-2016 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"crypto/rand"
"crypto/tls"
"fmt"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/zmqutil"
"github.com/bitmark-inc/exitwithstatus"
"github.com/bitmark-inc/logger"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
)
// setup command handler
// commands that run to create key and certificate files
// these commands cannot access any internal database or states
func processSetupCommand(log *logger.L, arguments []string, options *Configuration) bool {
command := "help"
if len(arguments) > 0 {
command = arguments[0]
arguments = arguments[1:]
}
switch command {
case "gen-peer-identity", "peer":
publicKeyFilename := options.Peering.PublicKey
privateKeyFilename := options.Peering.PrivateKey
if len(arguments) >= 1 && "" != arguments[0] {
publicKeyFilename = arguments[0] + ".public"
privateKeyFilename = arguments[0] + ".private"
}
err := zmqutil.MakeKeyPair(publicKeyFilename, privateKeyFilename)
if nil != err {
fmt.Printf("cannot generate private key: %q and public key: %q\n", privateKeyFilename, publicKeyFilename)
log.Criticalf("cannot generate private key: %q and public key: %q", privateKeyFilename, publicKeyFilename)
fmt.Printf("error generating server key pair: %v\n", err)
log.Criticalf("error generating server key pair: %v", err)
exitwithstatus.Exit(1)
}
fmt.Printf("generated private key: %q and public key: %q\n", privateKeyFilename, publicKeyFilename)
log.Infof("generated private key: %q and public key: %q", privateKeyFilename, publicKeyFilename)
case "gen-rpc-cert", "rpc":
certificateFilename := options.ClientRPC.Certificate
privateKeyFilename := options.ClientRPC.PrivateKey
addresses := []string{}
if len(arguments) >= 2 {
for _, a := range arguments[1:] {
if "" != a {
addresses = append(addresses, a)
}
}
}
if len(arguments) >= 1 && "" != arguments[0] {
certificateFilename = arguments[0] + ".crt"
privateKeyFilename = arguments[0] + ".key"
}
err := makeSelfSignedCertificate("rpc", certificateFilename, privateKeyFilename, 0 != len(addresses), addresses)
if nil != err {
fmt.Printf("cannot generate RPC key: %q and certificate: %q\n", privateKeyFilename, certificateFilename)
log.Criticalf("cannot generate RPC key: %q and certificate: %q", privateKeyFilename, certificateFilename)
fmt.Printf("error generating RPC key/certificate: %v\n", err)
log.Criticalf("error generating RPC key/certificate: %v", err)
exitwithstatus.Exit(1)
}
fmt.Printf("generated RPC key: %q and certificate: %q\n", privateKeyFilename, certificateFilename)
log.Infof("generated RPC key: %q and certificate: %q", privateKeyFilename, certificateFilename)
case "gen-proof-identity", "proof":
publicKeyFilename := options.Proofing.PublicKey
privateKeyFilename := options.Proofing.PrivateKey
signingKeyFilename := options.Proofing.SigningKey
if len(arguments) >= 1 && "" != arguments[0] {
publicKeyFilename = arguments[0] + ".public"
privateKeyFilename = arguments[0] + ".private"
signingKeyFilename = arguments[0] + ".sign"
}
err := zmqutil.MakeKeyPair(publicKeyFilename, privateKeyFilename)
if nil != err {
fmt.Printf("cannot generate private key: %q and public key: %q\n", privateKeyFilename, publicKeyFilename)
log.Criticalf("cannot generate private key: %q and public key: %q", privateKeyFilename, publicKeyFilename)
fmt.Printf("error generating server key pair: %v\n", err)
log.Criticalf("error generating server key pair: %v", err)
exitwithstatus.Exit(1)
}
// new random key for signing base record
signingKey := make([]byte, 32)
if _, err := rand.Read(signingKey); err != nil {
fmt.Printf("error generating signing key error: %v\n", err)
log.Criticalf("error generating signing key error: %v", err)
exitwithstatus.Exit(1)
}
if err = ioutil.WriteFile(signingKeyFilename, signingKey, 0600); err != nil {
fmt.Printf("error writing signing key file error: %v\n", err)
log.Criticalf("error writing signing key file error: %v", err)
exitwithstatus.Exit(1)
}
fmt.Printf("generated private key: %q and public key: %q\n", privateKeyFilename, publicKeyFilename)
log.Infof("generated private key: %q and public key: %q", privateKeyFilename, publicKeyFilename)
fmt.Printf("generated signing key: %q\n", signingKeyFilename)
log.Infof("generated signing key: %q", signingKeyFilename)
case "dns-txt", "txt":
dnsTXT(log, options)
case "start", "run":
return false // continue processing
case "block-times":
return false // defer processing until database is loaded
default:
switch command {
case "help", "h", "?":
case "", " ":
fmt.Printf("error: missing command\n")
default:
fmt.Printf("error: no such command: %v\n", command)
}
fmt.Printf("supported commands:\n\n")
fmt.Printf(" help (h) - display this message\n\n")
fmt.Printf(" gen-peer-identity (peer) - create private key in: %q\n", options.Peering.PrivateKey)
fmt.Printf(" and the public key in: %q\n", options.Peering.PublicKey)
fmt.Printf("\n")
fmt.Printf(" gen-rpc-cert (rpc) - create private key in: %q\n", options.ClientRPC.PrivateKey)
fmt.Printf(" and the certificate in: %q\n", options.ClientRPC.Certificate)
fmt.Printf("\n")
fmt.Printf(" gen-rpc-cert PREFIX IPs... - create private key in: '<PREFIX>.key'\n")
fmt.Printf(" and the certificate in '<PREFIX>.crt'\n")
fmt.Printf("\n")
fmt.Printf(" gen-proof-identity (proof) - create private key in: %q\n", options.Proofing.PrivateKey)
fmt.Printf(" the public key in: %q\n", options.Proofing.PublicKey)
fmt.Printf(" and signing key in: %q\n", options.Proofing.SigningKey)
fmt.Printf("\n")
fmt.Printf(" dns-txt (txt) - display the data to put in a dbs TXT record\n")
fmt.Printf("\n")
fmt.Printf(" start (run) - just run the program, same as no arguments\n")
fmt.Printf(" for convienience when passing script arguments\n")
fmt.Printf("\n")
//fmt.Printf(" block-times FILE BEGIN END - write time and difficulty to text file for a range of blocks\n")
exitwithstatus.Exit(1)
}
// indicate processing complete and prefor normal exit from main
return true
}
// data command handler
// the internal block and transaction pools are enabled so these commands can
// access and/or change these databases
func processDataCommand(log *logger.L, arguments []string, options *Configuration) bool {
command := "help"
if len(arguments) > 0 {
command = arguments[0]
arguments = arguments[1:]
}
switch command {
case "start", "run":
return false // continue processing
case "block-times":
if len(arguments) < 3 {
fmt.Printf("missing arguments arguments (use '' for stdout, and '0' for min/max)\n")
exitwithstatus.Exit(1)
}
begin, err := strconv.ParseUint(arguments[1], 10, 64)
if nil != err {
fmt.Printf("error in begin block number: %v\n", err)
exitwithstatus.Exit(1)
}
end, err := strconv.ParseUint(arguments[2], 10, 64)
if nil != err {
fmt.Printf("error in end block number: %v\n", err)
exitwithstatus.Exit(1)
}
fmt.Printf("*********** ERROR: %d %d\n", begin, end) // ***** FIX THIS: remove later when this code
switch filename := arguments[0]; filename {
case "": // use stdout
fallthrough
case "-": // use stdout
// block.PrintBlockTimes(os.Stdout, begin, end)
panic("HERE")
default:
fh, err := os.Create(filename)
if nil != err {
fmt.Printf("cannot create: %q error: %v\n", filename, err)
exitwithstatus.Exit(1)
}
defer fh.Close()
//block.PrintBlockTimes(fh, begin, end)
panic("HERE")
}
default:
fmt.Printf("error: no such command: %v\n", command)
exitwithstatus.Exit(1)
}
// indicate processing complete and perform normal exit from main
return true
}
// print out the DNS TXT record
func dnsTXT(log *logger.L, options *Configuration) {
// <TAG> a=<IPv4;IPv6> c=<PORT> s=<PORT> r=<PORT> f=<SHA3-256(cert)> p=<PUBLIC-KEY>
const txtRecord = "TXT \"bitmark=v2 a=%s c=%d s=%d r=%d f=%x p=%x\"\n"
rpc := options.ClientRPC
keypair, err := tls.LoadX509KeyPair(rpc.Certificate, rpc.PrivateKey)
if nil != err {
fmt.Printf("error: cannot certificate: %q error: %s\n", rpc.Certificate, err)
exitwithstatus.Exit(1)
}
fingerprint := CertificateFingerprint(keypair.Certificate[0])
rpcIP4, rpcIP6, rpcPort := getFirstConnections(rpc.Announce)
if 0 == rpcPort {
fmt.Printf("error: cannot determine rpc port\n")
exitwithstatus.Exit(1)
}
peering := options.Peering
publicKey, err := zmqutil.ReadPublicKeyFile(peering.PublicKey)
if nil != err {
fmt.Printf("error: cannot read public key: %q error: %s\n", peering.PublicKey, err)
exitwithstatus.Exit(1)
}
peeringAnnounce := options.Peering.Announce
broadcastIP4, broadcastIP6, broadcastPort := getFirstConnections(peeringAnnounce.Broadcast)
if 0 == broadcastPort {
fmt.Printf("error: cannot determine broadcast port\n")
exitwithstatus.Exit(1)
}
listenIP4, listenIP6, listenPort := getFirstConnections(peeringAnnounce.Listen)
if 0 == listenPort {
fmt.Printf("error: cannot determine listen port\n")
exitwithstatus.Exit(1)
}
IPs := ""
if "" != rpcIP4 && rpcIP4 == broadcastIP4 && rpcIP4 == listenIP4 {
IPs = rpcIP4
}
if "" != rpcIP6 && rpcIP6 == broadcastIP6 && rpcIP6 == listenIP6 {
if "" == IPs {
IPs = rpcIP6
} else {
IPs += ";" + rpcIP6
}
}
fmt.Printf("rpc fingerprint: %x\n", fingerprint)
fmt.Printf("rpc port: %d\n", rpcPort)
fmt.Printf("public key: %x\n", publicKey)
fmt.Printf("subscribe port: %d\n", broadcastPort)
fmt.Printf("connect port: %d\n", listenPort)
fmt.Printf("IP4 IP6: %s\n", IPs)
fmt.Printf(txtRecord, IPs, listenPort, broadcastPort, rpcPort, fingerprint, publicKey)
}
// extract first IP4 and/or IP6 connection
func getFirstConnections(connections []string) (string, string, int) {
initialPort := 0
IP4 := ""
IP6 := ""
for i, c := range connections {
v6, IP, port, err := splitConnection(c)
if nil != err {
fmt.Printf("error: cannot decode[%d]: %q error: %s\n", i, c, err)
exitwithstatus.Exit(1)
}
if v6 {
if "" == IP6 {
IP6 = IP
if 0 == initialPort || port == initialPort {
initialPort = port
}
}
} else {
if "" == IP4 {
IP4 = IP
if 0 == initialPort || port == initialPort {
initialPort = port
}
}
}
// fmt.Printf("scan[%d]: %v %d\n", i, IP, port)
}
return IP4, IP6, initialPort
}
// split connection into ip and port
func splitConnection(hostPort string) (bool, string, int, error) {
host, port, err := net.SplitHostPort(hostPort)
if nil != err {
return false, "", 0, fault.ErrInvalidIPAddress
}
IP := net.ParseIP(strings.Trim(host, " "))
if nil == IP {
return false, "", 0, fault.ErrInvalidIPAddress
}
numericPort, err := strconv.Atoi(strings.Trim(port, " "))
if nil != err {
return false, "", 0, err
}
if numericPort < 1 || numericPort > 65535 {
return false, "", 0, fault.ErrInvalidPortNumber
}
if nil != IP.To4() {
return false, IP.String(), numericPort, nil
}
return true, "[" + IP.String() + "]", numericPort, nil
}