This repository was archived by the owner on Jan 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
211 lines (177 loc) · 6.68 KB
/
server.js
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
'use strict';
var fs = require('fs');
var Server = require('./lib/server.js');
var utils = require('./lib/utils.js');
var version = require('./package.json').version;
var opts = utils.getopts({
"certificate": '',
"contract-config": '',
"faucet-config": '',
"intermediate-certificate": [],
"rpc": "http://localhost:8545",
"pid-file": '',
"port": 5000,
"private-key": '',
}, {
debug: false,
help: false,
version: false
});
function ensureInteger(key, min, max) {
var value = opts.options[key];
if (parseInt(value) != value) {
throw new Error(key + ' must be an integer');
}
value = parseInt(value);
if (typeof(min) === 'number' && min > value) {
throw new Error(key + ' must be ' + min + ' or greater');
} else if (typeof(max) === 'number' && max < value) {
throw new Error(key + ' must be ' + max + ' or less');
}
return value;
}
function loadFile(filename) {
try {
return fs.readFileSync(filename);
} catch (error) {
throw new Error('could not open file: ' + filename);
}
}
function checkOptions() {
if (opts.error) {
throw new Error(opts.error);
}
if (opts.args.length) {
throw new Error('invalid argument: ' + opts.args[0]);
}
var values = {
port: ensureInteger('port'),
// @TODO: Do some tests on these?
rpc: (opts.options['rpc'] || null),
pidFile: (opts.options['pid-file'] || null),
}
// Intermediate certificates require a SSL
var intermediateCertificateCount = opts.options['intermediate-certificate'].length
if (values.privateKey && intermediateCertificatesCount) {
throw new Error('intermediate certificates require --private-key and --certificate');
}
// Using SSL
if (opts.options['certificate'] && opts.options['private-key']) {
// Load the private key and certificate
values.privateKey = loadFile(opts.options['private-key']);
values.certificate = loadFile(opts.options['certificate']);
// Load the intermediate certificates
values.intermediateCertificate = [];
for (var i = 0; i < intermediateCertificateCount; i++) {
values.intermediateCertificate.push(loadFile(opts.options['intermediate-certificate'][i]));
}
} else if (opts.options['certificate'] || opts.options['private-key']) {
throw new Error('SSL requires both --certificate and --private-key');
}
// Load the contract storage configuration
if (opts.options['contract-config']) {
var contractConfig = loadFile(opts.options['contract-config']);
try {
values.contractConfig = JSON.parse(contractConfig);
} catch (error) {
throw new Error('invalid --contract-config JSON: ' + error);
}
}
if (opts.options['faucet-config']) {
try {
values.faucetPrivateKey = loadFile(opts.options['faucet-config']).toString();
if (!utils.isHexString(values.faucetPrivateKey, 32)) {
throw new Error('invalid faucet private key');
}
} catch (error) {
values.faucetPrivateKey = '0x' + require('crypto').randomBytes(32).toString('hex');
fs.writeFileSync(opts.options['faucet-config'], values.faucetPrivateKey);
}
}
return values;
}
var values, errorMessage;
try {
values = checkOptions();
} catch (error) {
errorMessage = error.message;
}
// Show the help (-h or the input parameters had problems)
if (errorMessage || opts.flags.help) {
console.log('');
console.log("Command Line Interface - ethers.io/" + version);
console.log('');
console.log("Usage:");
console.log(" node server.js [--help] [--version] [--debug] [--pid-file PATH]");
console.log(" [--port PORT] [--rpc URL] [--contract-config PATH]");
console.log(" [--faucet-config PATH]");
console.log(" [--certificate CERT --private-key KEY");
console.log(' [ --intermediate-certificate CERT ] ... ]');
console.log('');
console.log(' --help show this help screen');
console.log(' --version show the software version');
console.log(' --debug enable debug options');
console.log(' --pid-file PATH path to store PID file');
console.log('');
console.log('Service Options');
console.log(' --rpc URL url to rpc service (http://localhost:8545');
console.log(' --contract-config PATH');
console.log(' config JSON for contract storage');
console.log('');
console.log('HTTP Options');
console.log(' --port PORT port to bind the server to (default: 5000)');
console.log('');
console.log('HTTPS Options');
console.log(' --certificate CERT SSL certificate (PEM format)');
console.log(' --private-key KEY SSL private key (PEM format)');
console.log(' --intermediate-certificate CERT');
console.log(' SSL intermediate certificate(s) (PEM format)');
console.log('');
console.log('NOTES:');
console.log(' - RPC URLs may be of the forms http://localhost:8545 (default) or');
console.log(' ipc:/User/ricmoo/Library/Ethereum.geth.ipc');
console.log('');
if (errorMessage) {
console.log('Error:', errorMessage);
console.log('');
}
// Show the version
} else if (opts.flags.version) {
console.log("ethers.io/" + version);
// Start up the server :o)
} else {
var server = new Server({
// Service options
rpc: values.rpc,
port: values.port,
contractConfig: values.contractConfig,
debug: opts.flags.debug,
faucetPrivateKey: values.faucetPrivateKey,
// SSL Options
certificate: values.certificate,
privateKey: values.privateKey,
intermediateCertificate: values.intermediateCertificate,
});
server.on('error', function(error) {
/*
if (error.message === "CONNECTION ERROR: Couldn't connect to node on IPC.") {
console.log("ERROR: Could not connect to IPC " + values.ipcPath);
process.exit();
} else {
*/
console.log('ERROR: ' + error.message);
//}
});
server.start(function() {
console.log('ethers.io is running on port', server.port);
});
if (values.pidFile) {
fs.writeFile(values.pidFile, String(process.pid), function (error) {
if (error) {
process.exit("Could not write PID: " + error.message);
}
});
}
}
//server = new Server();
//server.start();