-
Notifications
You must be signed in to change notification settings - Fork 869
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cleanup repository before starting up IPFS (#722)
* feat: cleanup repository before starting up ipfs License: MIT Signed-off-by: Henrique Dias <[email protected]> * feat: ask to clean if they differ License: MIT Signed-off-by: Henrique Dias <[email protected]> * feat: run ipfs rpeo fsck * feat: handle econnrefused License: MIT Signed-off-by: Henrique Dias <[email protected]> * revert: package-lock License: MIT Signed-off-by: Henrique Dias <[email protected]> * fix: typo Co-Authored-By: hacdias <[email protected]> * fix: trim addr Co-Authored-By: hacdias <[email protected]> * feat: better func names * feat: update config before starting License: MIT Signed-off-by: Henrique Dias <[email protected]> * feat: initialize default config on store * fix: reduce default keysize Co-Authored-By: hacdias <[email protected]> * feat: change ipfsCOnfig object name * fix: change store v * fix: do not need to change origins on IPFS config * revert prev commit * dialog as soon as econnrefused * revert pkg lock * revert pkg lock * fix: better error msg License: MIT Signed-off-by: Henrique Dias <[email protected]> * fix: add quotes
- Loading branch information
Showing
5 changed files
with
156 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,104 @@ | ||
import IPFSFactory from 'ipfsd-ctl' | ||
import logger from './logger' | ||
import { showConnFailureErrorMessage } from './errors' | ||
import { join } from 'path' | ||
import fs from 'fs-extra' | ||
import { spawnSync } from 'child_process' | ||
import findExecutable from 'ipfsd-ctl/src/utils/find-ipfs-executable' | ||
|
||
export default async function createDaemon (opts) { | ||
opts.type = opts.type || 'go' | ||
opts.path = opts.path || '' | ||
opts.flags = opts.flags || ['--migrate=true', '--routing=dhtclient'] | ||
opts.keysize = opts.keysize || 4096 | ||
function repoFsck (path) { | ||
const exec = findExecutable('go', join(__dirname, '..')) | ||
spawnSync(exec, ['repo', 'fsck'], { | ||
env: { | ||
...process.env, | ||
IPFS_PATH: path | ||
} | ||
}) | ||
} | ||
|
||
if (opts.type !== 'go') { | ||
throw new Error(`${opts.type} connection is not supported yet`) | ||
async function configure (ipfsd) { | ||
const cfgFile = join(ipfsd.repoPath, 'config') | ||
const cfg = await fs.readJSON(cfgFile) | ||
|
||
let origins = [] | ||
try { | ||
origins = cfg.API.HTTPHeaders['Access-Control-Allow-Origin'] | ||
} catch (e) { | ||
logger.warn(e) | ||
} | ||
|
||
const factory = IPFSFactory.create({ type: opts.type }) | ||
if (!Array.isArray(origins)) { | ||
origins = [] | ||
} | ||
|
||
if (!origins.includes('webui://-')) origins.push('webui://-') | ||
if (!origins.includes('https://webui.ipfs.io')) origins.push('https://webui.ipfs.io') | ||
|
||
cfg.API.HTTPHeaders['Access-Control-Allow-Origin'] = origins | ||
cfg.API.HTTPHeaders['Access-Control-Allow-Methods'] = ['PUT', 'GET', 'POST'] | ||
|
||
await fs.writeJSON(cfgFile, cfg) | ||
} | ||
|
||
async function spawn ({ type, path, keysize }) { | ||
const factory = IPFSFactory.create({ type: type }) | ||
|
||
const ipfsd = await new Promise((resolve, reject) => { | ||
return new Promise((resolve, reject) => { | ||
factory.spawn({ | ||
disposable: false, | ||
defaultAddrs: true, | ||
repoPath: opts.path | ||
repoPath: path | ||
}, (e, ipfsd) => { | ||
if (e) return reject(e) | ||
if (ipfsd.initialized) { | ||
return resolve(ipfsd) | ||
} | ||
|
||
ipfsd.init({ | ||
directory: opts.path, | ||
keysize: opts.keysize | ||
directory: path, | ||
keysize: keysize | ||
}, e => { | ||
if (e) return reject(e) | ||
resolve(ipfsd) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
if (!ipfsd.started) { | ||
await new Promise((resolve, reject) => { | ||
ipfsd.start(opts.flags, err => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
async function start (ipfsd, { flags }) { | ||
await new Promise((resolve, reject) => { | ||
ipfsd.start(flags, err => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
|
||
resolve() | ||
}) | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
export default async function (opts) { | ||
const ipfsd = await spawn(opts) | ||
await configure(ipfsd) | ||
|
||
if (!ipfsd.started) { | ||
await start(ipfsd, opts) | ||
} | ||
|
||
let origins = [] | ||
try { | ||
origins = await ipfsd.api.config.get('API.HTTPHeaders.Access-Control-Allow-Origin') | ||
await ipfsd.api.id() | ||
} catch (e) { | ||
logger.warn(e) | ||
} | ||
if (!e.message.includes('ECONNREFUSED')) { | ||
throw e | ||
} | ||
|
||
if (!origins.includes('webui://-')) origins.push('webui://-') | ||
if (!origins.includes('https://webui.ipfs.io')) origins.push('https://webui.ipfs.io') | ||
if (!showConnFailureErrorMessage(ipfsd.repoPath, ipfsd.apiAddr)) { | ||
throw new Error('exit') | ||
} | ||
|
||
await ipfsd.api.config.set('API.HTTPHeaders.Access-Control-Allow-Origin', origins) | ||
await ipfsd.api.config.set('API.HTTPHeaders.Access-Control-Allow-Methods', ['PUT', 'GET', 'POST']) | ||
repoFsck(ipfsd.repoPath) | ||
await start(ipfsd, opts) | ||
} | ||
|
||
return ipfsd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { app, dialog, shell } from 'electron' | ||
|
||
const issueTemplate = (e) => `Please describe what you were doing when this error happened. | ||
**Specifications** | ||
- **OS**: ${process.platform} | ||
- **IPFS Desktop Version**: ${app.getVersion()} | ||
- **Electron Version**: ${process.versions.electron} | ||
- **Chrome Version**: ${process.versions.chrome} | ||
**Error** | ||
\`\`\` | ||
${e.stack} | ||
\`\`\` | ||
` | ||
|
||
export function showErrorMessage (e) { | ||
const option = dialog.showMessageBox({ | ||
type: 'error', | ||
title: 'IPFS Desktop has shutdown', | ||
message: 'IPFS Desktop has shutdown because of an error. You can restart the app or report the error to the developers, which requires a GitHub account.', | ||
buttons: [ | ||
'Close', | ||
'Report the error to the developers', | ||
'Restart the app' | ||
], | ||
cancelId: 0 | ||
}) | ||
|
||
if (option === 1) { | ||
shell.openExternal(`https://github.com/ipfs-shipyard/ipfs-desktop/issues/new?body=${encodeURI(issueTemplate(e))}`) | ||
} else if (option === 2) { | ||
app.relaunch() | ||
} | ||
|
||
app.exit(1) | ||
} | ||
|
||
export function showConnFailureErrorMessage (path, addr) { | ||
const option = dialog.showMessageBox({ | ||
type: 'warning', | ||
title: 'IPFS Desktop', | ||
message: `IPFS Desktop failed to connect to an existing ipfs api at ${addr}. This can happen if you run 'ipfs daemon' manually and it has not shutdown cleanly. Would you like to try running 'ipfs repo fsck' to remove the lock and api files from ${path} and try again?`, | ||
buttons: [ | ||
'No, just quit', | ||
'Yes, run "ipfs repo fsck"' | ||
], | ||
cancelId: 0 | ||
}) | ||
|
||
return option === 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters