-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfsControl.ts
346 lines (312 loc) · 13.8 KB
/
ipfsControl.ts
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
import * as IPFSFactory from 'ipfsd-ctl';
import { promisify } from 'util';
import * as path from 'path';
import * as fs from 'fs';
import { app } from 'electron';
import { createReadStream, createWriteStream } from 'fs';
import { createCipheriv, createDecipheriv } from 'crypto';
import * as assert from 'assert';
let ipfsd = null;
let ipfsApi = null;
let initPromise = null;
function getBasePathToSharu(): string {
return path.resolve(app.getPath('userData'));
}
export async function init(forcedPath?: string) {
if (initPromise !== null) {
return initPromise;
} else {
initPromise = new Promise(async (resolve, reject) => {
const sharuPath = ((forcedPath) ? forcedPath : getBasePathToSharu()) + path.sep + 'ipfs';
const promUnlink = promisify(fs.unlink);
let ipfsAlreadyInited = false;
const flags = [];
console.log('path to sharu: ' + sharuPath);
if (fs.existsSync(sharuPath)) {
ipfsAlreadyInited = true;
}
console.log('ipfs is already inited (because path exists): ' + ipfsAlreadyInited);
if (ipfsAlreadyInited) {
try {
await promUnlink(path.resolve(sharuPath, 'api'));
} catch (e) {
console.log('no api file found in sharu-folder, no problemo');
}
try {
await promUnlink(path.resolve(sharuPath, 'repo.lock'));
} catch (e) {
console.log('no repo.lock file found in sharu-folder, no problemo');
}
}
const ipfsFactory = IPFSFactory.create({
type: 'jsdep'
});
const options = {
repoPath: sharuPath,
init: false,
start: false,
disposable: false,
defaultAddrs: true,
config: generateIpfsConfig(forcedPath)
};
ipfsFactory.spawn(options, async (err, _ipfsd) => {
if (err) {
return reject(err);
}
ipfsd = _ipfsd;
assert.ok(ipfsd, 'deamon did not spawn');
console.log('ipfs is inited (says ipfsd): ' + ipfsd.initialized);
if (!ipfsAlreadyInited) {
console.log('since ipfs is not inited yet, we do this now');
try {
ipfsd = await (() => {
return new Promise((res, rej) => {
ipfsd.init((error, _i) => {
if (error) {
console.log('init error');
console.log(error);
rej(error);
}
console.log('inited');
res(_i);
});
});
})();
} catch (initError) {
console.log('whoopsi? err!');
console.log(initError);
reject(initError);
return;
}
console.log('init seems to be done okayishly');
}
assert.ok(ipfsd.initialized);
console.log('lets start this thing');
let api = null;
try {
api = await (() => {
return new Promise((res, rej) => {
ipfsd.start(flags, (error, _a) => {
if (error) {
console.log(error);
rej(error);
}
console.log('started');
res(_a);
});
});
})();
} catch (startError) {
return reject(startError);
}
assert.ok(api, 'api does not exist after start');
console.log('starting seems to be done okayishly');
ipfsApi = api;
console.log('API: ' + api.apiHost + ':' + api.apiPort);
console.log('Gateway: ' + api.gatewayHost + ':' + api.gatewayPort);
let currentConfig = null;
try {
// currentConfig = await promIpfsdGetConfig(['Bootstrap']);
currentConfig = await (() => {
return new Promise((res, rej) => {
ipfsd.getConfig(['Bootstrap'], (error, _a) => {
if (error) {
console.log(error);
rej(error);
}
console.log('got config');
res(_a);
});
});
})();
} catch (getConfigError) {
console.error('Error getting IPFS config');
}
const leConfig = JSON.parse(currentConfig);
let dirty = false;
const customBootstrapAdditions = getCustomBootStrapAdditions(forcedPath);
customBootstrapAdditions.Bootstrap.forEach(customPeer => {
if (!leConfig.includes(customPeer)) {
leConfig.push(customPeer);
dirty = true;
}
});
if (dirty) {
console.log('setting new bootstrap-config');
try {
await (() => {
return new Promise((res, rej) => {
ipfsd.setConfig(['Bootstrap'], JSON.stringify(leConfig), (error) => {
if (error) {
console.log(error);
rej(error);
}
console.log('set config');
res();
});
});
})();
} catch (e) {
console.log('error setting new config: ' + e);
}
try {
const conf = await (() => {
return new Promise((res, rej) => {
ipfsd.getConfig(['Bootstrap'], (error, _a) => {
if (error) {
console.log(error);
rej(error);
}
console.log('got config');
res(_a);
});
});
})();
console.log('the new bootstrap: ');
console.log(conf);
} catch (e) {
console.error('Error getting current config' + e);
}
}
resolve(ipfsApi);
});
});
return initPromise;
}
}
export async function stop() {
ipfsd.stop = promisify(ipfsd.stop);
await ipfsd.stop();
}
export async function upload(filePath, symKey, iv, nodelocalPath) {
return new Promise(async (resolve, reject) => {
try {
const fileName = path.basename(filePath);
const rst = createReadStream(filePath);
const cipher = createCipheriv('aes-256-gcm', Buffer.from(symKey, 'latin1'), Buffer.from(iv, 'latin1'), { authTagLength: 16 });
cipher.on('error', (err) => {
console.log(`cipher stream error ${err}`);
reject(err);
});
let authTag = '';
cipher.on('finish', (err) => {
authTag = cipher.getAuthTag().toString('latin1');
});
const p = nodelocalPath + '/' + fileName;
const prom = ipfsApi.files.write(p, cipher, { create: true });
rst.pipe(cipher);
await prom;
resolve(authTag);
} catch (e) {
reject(e);
}
});
}
export async function download(filePath, symKey, iv, authTag, hash) {
return new Promise(async (resolve, reject) => {
try {
const stream = ipfsApi.catReadableStream(hash);
stream.on('error', (err) => {
console.log(`IPFS download error ${err}`);
reject(err);
});
const decipher = createDecipheriv(
'aes-256-gcm',
Buffer.from(symKey, 'latin1'),
Buffer.from(iv, 'latin1'),
{ authTagLength: 16 }
);
decipher.setAuthTag(Buffer.from(authTag, 'latin1'));
decipher.on('error', (err) => {
console.log(`Download decipher error ${err}`);
reject(err);
});
const output = createWriteStream(filePath);
output.on('error', (err) => {
console.log(`File system writing error ${err}`);
reject(err);
});
output.on('finish', () => {
resolve();
});
stream.pipe(decipher).pipe(output);
} catch (e) {
reject(e);
}
});
}
const CUSTOM_BOOTSTRAP_FILE = 'customBootstrapAdditions.json';
export function generateIpfsConfig(forcedPath?: string) {
const stdConfig = {
'Addresses': {
'Swarm': [
'/ip4/0.0.0.0/tcp/4002',
'/ip4/127.0.0.1/tcp/4003/ws'
],
'API': '/ip4/127.0.0.1/tcp/5002',
'Gateway': '/ip4/127.0.0.1/tcp/9090'
},
'Bootstrap': [
'/ip4/213.165.80.135/tcp/4001/ipfs/QmXPiBKHQ31s33n6F9cUWYjdGSFs5nAzxBPoq6NqnrR1uj',
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
'/ip6/2604:a880:1:20::1f9:9001/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
'/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/ip6/2604:a880:0:1010::23:d001/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/ip6/2a03:b0c0:1:d0::e7:1/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/ip6/2604:a880:1:20::1d9:6001/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
// '/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
// '/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
],
'API': {
'HTTPHeaders':
{
'Access-Control-Allow-Credentials': [
'true'
],
'Access-Control-Allow-Methods': [
'PUT', 'POST', 'GET'
],
'Access-Control-Allow-Origin': [
'*'
]
}
},
};
const custom = getCustomBootStrapAdditions(forcedPath);
custom.Bootstrap.forEach(peer => {
if (!stdConfig.Bootstrap.includes(peer)){
stdConfig.Bootstrap.push(peer);
}
});
return stdConfig;
}
export function getCustomBootStrapAdditions(forcedPath?: string) {
const pathToCustomBootstrapAdditions = ((forcedPath) ? forcedPath : getBasePathToSharu()) + path.sep + CUSTOM_BOOTSTRAP_FILE;
let bootStrap = { 'Bootstrap': [] };
if (fs.existsSync(pathToCustomBootstrapAdditions)) {
try {
bootStrap = JSON.parse(fs.readFileSync(pathToCustomBootstrapAdditions).toString());
} catch (e) {
console.log('something went wrong while processing your custom bootstrap, falling back to default');
console.log(e);
}
} else {
fs.writeFileSync(pathToCustomBootstrapAdditions, JSON.stringify(bootStrap));
console.log('created default custom bootstrap config: ' + pathToCustomBootstrapAdditions);
}
// these are the nodes provided by sharu
bootStrap.Bootstrap.push('/ip4/159.89.6.248/tcp/4004/ipfs/QmSmCMn5Mq5sLSa6mehmUtFLmqezu7mXHHPepvXiYTX1vR');
bootStrap.Bootstrap.push('/ip4/159.89.6.248/tcp/4001/ipfs/Qmci7GnY4WvEJr1UFBAfMqji7Vch1cEpAcGgfVGHVKxeJp');
return bootStrap;
}