New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persist DHT nodes #1431
Open
bookmoons
wants to merge
9
commits into
webtorrent:master
from
bookmoons:bookmoons/persist
base: master
+221
−7
Open
Persist DHT nodes #1431
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
03b7ecc
Persist DHT nodes
bookmoons a7ed3a2
Move DHT persistence logic into lib/ module
bookmoons c2f5943
Add save DHT state test
bookmoons 2852b67
Distinguish addresses in DHT persistence test
bookmoons ff755bc
Correct DHT state loading
bookmoons d6e090f
Allow DHT state loading when bootstrap disabled
bookmoons 2a3e7ee
Add load DHT state test
bookmoons 02879a0
Exclude dhtpersist on browser
bookmoons 7f26a8c
Split DHT persistence options
bookmoons File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
Move DHT persistence logic into lib/ module
Increases cleanliness of index.js.
- Loading branch information
bookmoons
committed
Jul 30, 2018
commit a7ed3a26867806759cf2b702cc6be7519a19d9d4
Verified
This commit was signed with a verified signature.
GPG key ID: 29D2A92F3145E7A7
Learn about signing commits
| @@ -10,10 +10,8 @@ var debug = require('debug')('webtorrent') | ||
| var DHT = require('bittorrent-dht/client') // browser exclude | ||
| var EventEmitter = require('events').EventEmitter | ||
| var extend = require('xtend') | ||
| var fs = require('fs') | ||
| var inherits = require('inherits') | ||
| var loadIPSet = require('load-ip-set') // browser exclude | ||
| var mkdirp = require('mkdirp') | ||
| var parallel = require('run-parallel') | ||
| var parseTorrent = require('parse-torrent') | ||
| var path = require('path') | ||
| @@ -22,6 +20,7 @@ var randombytes = require('randombytes') | ||
| var speedometer = require('speedometer') | ||
| var zeroFill = require('zero-fill') | ||
|
|
||
| var dhtPersist = require('./lib/dhtpersist') // browser exclude | ||
bookmoons
Author
|
||
| var TCPPool = require('./lib/tcp-pool') // browser exclude | ||
| var Torrent = require('./lib/torrent') | ||
|
|
||
| @@ -128,90 +127,19 @@ function WebTorrent (opts) { | ||
| self._downloadSpeed = speedometer() | ||
| self._uploadSpeed = speedometer() | ||
|
|
||
| // DHT state save location | ||
| var dhtSaveFile | ||
|
|
||
| var savingDhtState = false | ||
| function saveDhtState () { | ||
| if (savingDhtState) return | ||
| if (!self.dht) return // Quell after destroy | ||
| savingDhtState = true | ||
| var dhtState = self.dht.toJSON() | ||
| var dhtStateJson = JSON.stringify(dhtState) | ||
| mkdirp( | ||
| path.dirname(dhtSaveFile), | ||
| function handleDhtSaveDirCreated (err) { | ||
| if (err) { | ||
| savingDhtState = false | ||
| return | ||
| } | ||
| fs.writeFile( | ||
| dhtSaveFile, | ||
| dhtStateJson, | ||
| function handleDhtStateWritten () { | ||
| savingDhtState = false | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| function readDhtState (file) { | ||
| try { | ||
| return fs.readFileSync(file) | ||
| } catch (e) { | ||
| switch (e.code) { | ||
| case 'EACCES': | ||
| case 'EISDIR': | ||
| case 'ENOENT': | ||
| case 'EPERM': | ||
| return null | ||
| default: | ||
| throw e | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function parseDhtState (dhtStateJson) { | ||
| try { | ||
| return JSON.parse(dhtStateJson) | ||
| } catch (e) { | ||
| if (e instanceof SyntaxError) return null | ||
| else throw e | ||
| } | ||
| } | ||
|
|
||
| function loadDhtState (file) { | ||
| var dhtStateJson = readDhtState(file) | ||
| if (!dhtStateJson) return null | ||
| var dhtState = parseDhtState(dhtStateJson) | ||
| if (!dhtState) return null | ||
| return dhtState | ||
| } | ||
|
|
||
| function loadDhtNodes (file) { | ||
| var dhtState = loadDhtState(file) | ||
| if (!dhtState) return null | ||
| if (!('nodes' in dhtState)) return null | ||
| var nodes = dhtState.nodes | ||
| if (!Array.isArray(nodes)) return null | ||
| if (nodes.length === 0) return null // Don't load an empty nodes list | ||
| return nodes | ||
| } | ||
|
|
||
| if (opts.dht !== false && typeof DHT === 'function' /* browser exclude */) { | ||
| var dhtOpts = extend({ nodeId: self.nodeId }, opts.dht) | ||
|
|
||
| if (opts.dhtState) { | ||
| // Construct state save location | ||
| dhtSaveFile = | ||
| self.dhtSaveFile = | ||
| opts.dhtState === true | ||
| ? path.join(appDataFolder('webtorrent'), 'dht.json') | ||
| : opts.dhtState | ||
|
|
||
| if (!('bootstrap' in dhtOpts)) { | ||
| // Load persisted state | ||
| var nodes = loadDhtNodes(dhtSaveFile) | ||
| var nodes = dhtPersist.loadNodes(self.dhtSaveFile) | ||
| if (nodes) dhtOpts.bootstrap = nodes | ||
| } | ||
| } | ||
| @@ -222,7 +150,9 @@ function WebTorrent (opts) { | ||
| if (opts.dhtState) { | ||
| // Persist state periodically | ||
| var saveInterval = 15 * 60 * 1000 // 15 minutes | ||
| self.saveDhtStateTimer = setInterval(saveDhtState, saveInterval) | ||
| self.saveDhtStateTimer = setInterval(function saveDhtState () { | ||
| dhtPersist.save(self.dht, self.dhtSaveFile) | ||
| }, saveInterval) | ||
| } | ||
|
|
||
| self.dht.once('error', function (err) { | ||
| @@ -499,6 +429,20 @@ WebTorrent.prototype.address = function () { | ||
| : { address: '0.0.0.0', family: 'IPv4', port: 0 } | ||
| } | ||
|
|
||
| /** | ||
| * Persist DHT state to disk. | ||
| * No effect if DHT is not loaded. | ||
| */ | ||
| WebTorrent.prototype.saveDhtState = function (cb) { | ||
| if ( | ||
| this.dht !== false && | ||
| this.dhtSaveFile && | ||
| typeof DHT === 'function' /* browser exclude */ | ||
| ) { | ||
| dhtPersist.save(this.dht, this.dhtSaveFile, cb) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Destroy the client, including all torrents and connections to peers. | ||
| * @param {function} cb | ||
| @@ -0,0 +1,78 @@ | ||
| var fs = require('fs') | ||
| var mkdirp = require('mkdirp') | ||
| var path = require('path') | ||
|
|
||
| var savingDhtState = false | ||
| function saveDhtState (dht, file, cb) { | ||
| if (savingDhtState) return | ||
| if (!dht) return // Quell after destroy | ||
| savingDhtState = true | ||
| var dhtState = dht.toJSON() | ||
| var dhtStateJson = JSON.stringify(dhtState) | ||
| mkdirp( | ||
| path.dirname(file), | ||
| function handleDhtSaveDirCreated (err) { | ||
| if (err) { | ||
| savingDhtState = false | ||
| if (cb) cb(err) | ||
| return | ||
| } | ||
| fs.writeFile( | ||
| file, | ||
| dhtStateJson, | ||
| function handleDhtStateWritten () { | ||
| savingDhtState = false | ||
| if (cb) cb(null) | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| function readDhtState (file) { | ||
| try { | ||
| return fs.readFileSync(file) | ||
| } catch (e) { | ||
| switch (e.code) { | ||
| case 'EACCES': | ||
| case 'EISDIR': | ||
| case 'ENOENT': | ||
| case 'EPERM': | ||
| return null | ||
| default: | ||
| throw e | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function parseDhtState (dhtStateJson) { | ||
| try { | ||
| return JSON.parse(dhtStateJson) | ||
| } catch (e) { | ||
| if (e instanceof SyntaxError) return null | ||
| else throw e | ||
| } | ||
| } | ||
|
|
||
| function loadDhtState (file) { | ||
| var dhtStateJson = readDhtState(file) | ||
| if (!dhtStateJson) return null | ||
| var dhtState = parseDhtState(dhtStateJson) | ||
| if (!dhtState) return null | ||
| return dhtState | ||
| } | ||
|
|
||
| function loadDhtNodes (file) { | ||
| var dhtState = loadDhtState(file) | ||
| if (!dhtState) return null | ||
| if (!('nodes' in dhtState)) return null | ||
| var nodes = dhtState.nodes | ||
| if (!Array.isArray(nodes)) return null | ||
| if (nodes.length === 0) return null // Don't load an empty nodes list | ||
| return nodes | ||
| } | ||
|
|
||
| module.exports = { | ||
| save: saveDhtState, | ||
| loadNodes: loadDhtNodes | ||
| } |
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
It says browser exclude, but it's not actually excluded in the browser field of package.json?
like this