|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | const callbackify = require('callbackify')
|
| 4 | +const getDefaultConfig = require('../runtime/config-nodejs.js') |
| 5 | +const log = require('debug')('ipfs:core:config') |
4 | 6 |
|
5 | 7 | module.exports = function config (self) {
|
6 | 8 | return {
|
7 | 9 | get: callbackify.variadic(self._repo.config.get),
|
8 | 10 | set: callbackify(self._repo.config.set),
|
9 |
| - replace: callbackify.variadic(self._repo.config.set) |
| 11 | + replace: callbackify.variadic(self._repo.config.set), |
| 12 | + profiles: { |
| 13 | + apply: callbackify.variadic(applyProfile), |
| 14 | + list: callbackify.variadic(listProfiles) |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + async function applyProfile (profileName, opts) { |
| 19 | + opts = opts || {} |
| 20 | + const { dryRun } = opts |
| 21 | + |
| 22 | + const profile = profiles[profileName] |
| 23 | + |
| 24 | + if (!profile) { |
| 25 | + throw new Error(`No profile with name '${profileName}' exists`) |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + const oldCfg = await self.config.get() |
| 30 | + let newCfg = JSON.parse(JSON.stringify(oldCfg)) // clone |
| 31 | + newCfg = profile.transform(newCfg) |
| 32 | + |
| 33 | + if (!dryRun) { |
| 34 | + await self.config.replace(newCfg) |
| 35 | + } |
| 36 | + |
| 37 | + // Scrub private key from output |
| 38 | + delete oldCfg.Identity.PrivKey |
| 39 | + delete newCfg.Identity.PrivKey |
| 40 | + |
| 41 | + return { original: oldCfg, updated: newCfg } |
| 42 | + } catch (err) { |
| 43 | + log(err) |
| 44 | + |
| 45 | + throw new Error(`Could not apply profile '${profileName}' to config: ${err.message}`) |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +async function listProfiles (options) { // eslint-disable-line require-await |
| 51 | + return Object.keys(profiles).map(name => ({ |
| 52 | + name, |
| 53 | + description: profiles[name].description |
| 54 | + })) |
| 55 | +} |
| 56 | + |
| 57 | +const profiles = { |
| 58 | + server: { |
| 59 | + description: 'Disables local host discovery - recommended when running IPFS on machines with public IPv4 addresses.', |
| 60 | + transform: (config) => { |
| 61 | + config.Discovery.MDNS.Enabled = false |
| 62 | + config.Discovery.webRTCStar.Enabled = false |
| 63 | + |
| 64 | + return config |
| 65 | + } |
| 66 | + }, |
| 67 | + 'local-discovery': { |
| 68 | + description: 'Enables local host discovery - inverse of "server" profile.', |
| 69 | + transform: (config) => { |
| 70 | + config.Discovery.MDNS.Enabled = true |
| 71 | + config.Discovery.webRTCStar.Enabled = true |
| 72 | + |
| 73 | + return config |
| 74 | + } |
| 75 | + }, |
| 76 | + lowpower: { |
| 77 | + description: 'Reduces daemon overhead on the system - recommended for low power systems.', |
| 78 | + transform: (config) => { |
| 79 | + config.Swarm = config.Swarm || {} |
| 80 | + config.Swarm.ConnMgr = config.Swarm.ConnMgr || {} |
| 81 | + config.Swarm.ConnMgr.LowWater = 20 |
| 82 | + config.Swarm.ConnMgr.HighWater = 40 |
| 83 | + |
| 84 | + return config |
| 85 | + } |
| 86 | + }, |
| 87 | + 'default-power': { |
| 88 | + description: 'Inverse of "lowpower" profile.', |
| 89 | + transform: (config) => { |
| 90 | + const defaultConfig = getDefaultConfig() |
| 91 | + |
| 92 | + config.Swarm = defaultConfig.Swarm |
| 93 | + |
| 94 | + return config |
| 95 | + } |
| 96 | + }, |
| 97 | + test: { |
| 98 | + description: 'Reduces external interference of IPFS daemon - for running the daemon in test environments.', |
| 99 | + transform: (config) => { |
| 100 | + const defaultConfig = getDefaultConfig() |
| 101 | + |
| 102 | + config.Addresses.API = defaultConfig.Addresses.API ? '/ip4/127.0.0.1/tcp/0' : '' |
| 103 | + config.Addresses.Gateway = defaultConfig.Addresses.Gateway ? '/ip4/127.0.0.1/tcp/0' : '' |
| 104 | + config.Addresses.Swarm = defaultConfig.Addresses.Swarm.length ? ['/ip4/127.0.0.1/tcp/0'] : [] |
| 105 | + config.Bootstrap = [] |
| 106 | + config.Discovery.MDNS.Enabled = false |
| 107 | + config.Discovery.webRTCStar.Enabled = false |
| 108 | + |
| 109 | + return config |
| 110 | + } |
| 111 | + }, |
| 112 | + 'default-networking': { |
| 113 | + description: 'Restores default network settings - inverse of "test" profile.', |
| 114 | + transform: (config) => { |
| 115 | + const defaultConfig = getDefaultConfig() |
| 116 | + |
| 117 | + config.Addresses.API = defaultConfig.Addresses.API |
| 118 | + config.Addresses.Gateway = defaultConfig.Addresses.Gateway |
| 119 | + config.Addresses.Swarm = defaultConfig.Addresses.Swarm |
| 120 | + config.Bootstrap = defaultConfig.Bootstrap |
| 121 | + config.Discovery.MDNS.Enabled = defaultConfig.Discovery.MDNS.Enabled |
| 122 | + config.Discovery.webRTCStar.Enabled = defaultConfig.Discovery.webRTCStar.Enabled |
| 123 | + |
| 124 | + return config |
| 125 | + } |
10 | 126 | }
|
11 | 127 | }
|
| 128 | + |
| 129 | +module.exports.profiles = profiles |
0 commit comments