|
| 1 | +/* eslint-disable spellcheck/spell-checker */ |
| 2 | +const shell = require('shelljs'); |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const argv = require('yargs') |
| 7 | + .usage('Usage: $0 [options]') |
| 8 | + // Allow the user to force IPFS config override |
| 9 | + .option('force', { |
| 10 | + alias: 'f', |
| 11 | + describe: 'Force the private IPFS config even if a config already exists', |
| 12 | + type: 'boolean', |
| 13 | + }) |
| 14 | + // Get IPFS path from cli argument, environment variable or expected default path |
| 15 | + .option('path', { |
| 16 | + alias: 'p', |
| 17 | + describe: |
| 18 | + 'The path to the IPFS config directory (the default path is usually already set on IPFS_PATH environment variable)', |
| 19 | + nargs: 1, |
| 20 | + type: 'string', |
| 21 | + default: process.env.IPFS_PATH || path.join(require('os').homedir(), '.ipfs'), |
| 22 | + }).argv; |
| 23 | + |
| 24 | +// Swarm key content |
| 25 | +const swarmKeyContent = `/key/swarm/psk/1.0.0/ |
| 26 | +/base16/ |
| 27 | +5f3af0599d991e5eb4c37da2472aa299759ee3350ba26c125d0c7579dd04dd52 |
| 28 | +`; |
| 29 | + |
| 30 | +// Exit if IPFS is not installed |
| 31 | +if (!shell.which('ipfs')) { |
| 32 | + shell.echo('IPFS is not installed or not in the PATH'); |
| 33 | + shell.exit(1); |
| 34 | +} |
| 35 | + |
| 36 | +const ipfsPath = argv.path; |
| 37 | +shell.echo('IPFS Path:'); |
| 38 | +shell.echo(ipfsPath); |
| 39 | + |
| 40 | +// Initialize the IPFS node |
| 41 | +initializeNode(ipfsPath); |
| 42 | + |
| 43 | +// Setup the swarm key file |
| 44 | +setupSwarmKey(ipfsPath, swarmKeyContent); |
| 45 | + |
| 46 | +shell.echo('Done'); |
| 47 | +shell.exit(0); |
| 48 | + |
| 49 | +/** |
| 50 | + * Initialize the IPFS node with private network settings |
| 51 | + */ |
| 52 | +function initializeNode(ipfsPath) { |
| 53 | + // Check if the ipfs folder exists and if it is writable. |
| 54 | + try { |
| 55 | + fs.accessSync(ipfsPath, fs.constants.F_OK | fs.constants.W_OK); |
| 56 | + // If --force argument is set, the config will be overwritten |
| 57 | + if (argv.force) { |
| 58 | + shell.echo('IPFS config already exists. Force argument set, overriding it.'); |
| 59 | + } else { |
| 60 | + shell.echo('IPFS config already exists (use --force if you want to override it)'); |
| 61 | + shell.exit(0); |
| 62 | + } |
| 63 | + } catch (err) { |
| 64 | + if (err.code !== 'ENOENT') { |
| 65 | + shell.echo(`No read access to ipfs config folder: ${ipfsPath}`); |
| 66 | + shell.exit(1); |
| 67 | + } |
| 68 | + |
| 69 | + // Initializing ipfs repo |
| 70 | + shell.echo('No IPFS repo found in $IPFS_PATH. Initializing...'); |
| 71 | + shell.exec('ipfs init'); |
| 72 | + } |
| 73 | + |
| 74 | + // Reinitialize bootstrap nodes |
| 75 | + shell.echo('Removing all bootstrap nodes...'); |
| 76 | + shell.echo( |
| 77 | + '(see https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#private-networks)', |
| 78 | + ); |
| 79 | + shell.exec('ipfs bootstrap rm --all'); |
| 80 | + |
| 81 | + shell.echo('Adding private swarm bootstrap nodes...'); |
| 82 | + shell.exec( |
| 83 | + `ipfs bootstrap add /dns4/ipfs-bootstrap.request.network/tcp/4001/ipfs/QmPBPgTDVjveRu6KjGVMYixkCSgGtVyV8aUe6wGQeLZFVd /dns4/ipfs-bootstrap-2.request.network/tcp/4001/ipfs/QmYdcSoVNU1axgSnkRAyHtwsKiSvFHXeVvRonGCAV9LVEj /dns4/ipfs-2.request.network/tcp/4001/ipfs/QmPBPgTDVjveRu6KjGVMYixkCSgGtVyV8aUe6wGQeLZFVd /dns4/ipfs-survival.request.network/tcp/4001/ipfs/Qmb6a5DH45k8JwLdLVZUhRhv1rnANpsbXjtsH41esGhNCh`, |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Setup the IPFS private swarm key |
| 89 | + */ |
| 90 | +function setupSwarmKey(ipfsPath, swarmKeyContent) { |
| 91 | + const swarmKeyPath = path.join(ipfsPath, 'swarm.key'); |
| 92 | + // Check if the swarm key file exists |
| 93 | + try { |
| 94 | + fs.accessSync(swarmKeyPath, fs.constants.F_OK | fs.constants.W_OK); |
| 95 | + return; |
| 96 | + } catch (err) { |
| 97 | + shell.echo('No swarm.key found, creating a new key.'); |
| 98 | + } |
| 99 | + |
| 100 | + // Create a new swarm key file |
| 101 | + try { |
| 102 | + fs.writeFileSync(swarmKeyPath, swarmKeyContent, { |
| 103 | + mode: 0o600, |
| 104 | + }); |
| 105 | + } catch (err) { |
| 106 | + throw new Error(`Error creating swarm key file: ${err.message}`); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + shell.echo('Swarm key file created.'); |
| 111 | +} |
0 commit comments