-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5e22c45
Showing
14 changed files
with
574 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
secret.js | ||
secret/* | ||
!secret/index-sample.js | ||
|
||
main.css | ||
node_modules | ||
static/bundle.js | ||
static/main.css | ||
|
||
.DS_Store |
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,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) John Hiesey | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,10 @@ | ||
# Peercloud | ||
|
||
### Decentralized hosting over WebTorrent | ||
|
||
Description forthcoming. | ||
|
||
## License | ||
|
||
MIT. Copyright (c) John Hiesey. | ||
|
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,176 @@ | ||
var debug = require('debug')('peercloud.io') | ||
var mime = require('mime') | ||
var Peer = require('simple-peer') | ||
var thunky = require('thunky') | ||
var WebTorrent = require('webtorrent') | ||
var xhr = require('xhr') | ||
|
||
var TRACKER_URL = 'wss://tracker.webtorrent.io' | ||
|
||
global.WEBTORRENT_ANNOUNCE = [ TRACKER_URL ] | ||
|
||
if (!Peer.WEBRTC_SUPPORT || !navigator.serviceWorker) { | ||
alert('This browser is unsupported. Please use a browser with WebRTC support and ServiceWorker support.') | ||
return; | ||
} | ||
|
||
/* | ||
indexed by infoHash | ||
{ | ||
files: [ | ||
{ | ||
name: '', | ||
cb: //function | ||
} | ||
] | ||
} | ||
*/ | ||
var waitingTorrents = {}; | ||
|
||
function fetchFileFromTorrent(torrent, path, cb) { | ||
for (var i = 0; i < torrent.files.length; i++) { | ||
var file = torrent.files[i]; | ||
if (file.path === torrent.name + '/' + path) { | ||
file.getBuffer(function (err, buffer) { | ||
if (err) return cb(err); | ||
cb(null, buffer.toArrayBuffer()); | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
cb(new Error('file not found')) | ||
} | ||
|
||
function torrentReady (torrent) { | ||
var entry = waitingTorrents[torrent.infoHash]; | ||
entry.torrent = torrent; | ||
entry.files.forEach(function (file) { | ||
fetchFileFromTorrent(torrent, file.path, file.cb) | ||
}); | ||
|
||
} | ||
|
||
function fetchFile(infoHash, path, cb) { | ||
var entry = waitingTorrents[infoHash]; | ||
|
||
if (!entry) { | ||
getClient(function (err, client) { | ||
if (err) return console.error(err) | ||
client.add(infoHash, torrentReady) | ||
}) | ||
waitingTorrents[infoHash] = { | ||
files: [], | ||
torrent: null | ||
}; | ||
} | ||
|
||
if (entry && entry.torrent) { | ||
fetchFileFromTorrent(entry.torrent, path, cb) | ||
} else { | ||
waitingTorrents[infoHash].files.push({ | ||
path: path, | ||
cb: cb | ||
}); | ||
} | ||
} | ||
|
||
function messageWorker() { | ||
worker.postMessage.apply(worker, arguments); | ||
} | ||
|
||
var pageId = Math.random(1, 2); | ||
|
||
window.addEventListener('unload', function () { | ||
messageWorker({ | ||
type: 'unload', | ||
pageId: pageId | ||
}); | ||
}); | ||
|
||
var worker = null; | ||
navigator.serviceWorker.register('/service-worker.js').then(function (registration) { | ||
if (!registration.active) { | ||
// Refresh to activate the worker | ||
location.reload(); | ||
return; | ||
} | ||
worker = registration.active; | ||
|
||
var messageChannel = new MessageChannel(); | ||
messageChannel.port1.onmessage = function(event) { | ||
var data = event.data; | ||
switch(data.type) { | ||
case 'fetch': | ||
fetchFile(data.hash, data.path, function (err, buffer) { | ||
var msg = { | ||
type: 'response', | ||
hash: data.hash, | ||
path: data.path, | ||
err: null | ||
}; | ||
if (!err) { | ||
msg.response = { | ||
body: buffer, | ||
mime: mime.lookup(data.path) | ||
}; | ||
} else { | ||
msg.err = err.message; | ||
} | ||
messageWorker(msg); | ||
}); | ||
break; | ||
default: | ||
console.log('Unexpected message from service worker:', data); | ||
break; | ||
} | ||
}; | ||
|
||
messageWorker({ | ||
type: 'returnpipe', | ||
pageId: pageId | ||
}, [messageChannel.port2]); | ||
|
||
loadPage(); | ||
}).catch(function (err) { | ||
console.error('service worker failed'); | ||
console.error(err); | ||
}); | ||
|
||
var MATCH_PATH = /#\/?([a-fA-F0-9]{40})(?:\/(.*))?$/; | ||
|
||
function loadPage () { | ||
var matches = MATCH_PATH.exec(location.hash); | ||
if (matches) { | ||
var hash = matches[1]; | ||
var path = matches[2] || 'index.html'; | ||
var iframe = document.createElement('iframe'); | ||
iframe.src = '/sandbox/' + hash + '/' + path; | ||
iframe.width = iframe.height = '100%'; | ||
iframe.sandbox = 'allow-scripts allow-same-origin'; | ||
iframe.frameBorder = 0; | ||
document.body.appendChild(iframe); | ||
} else { | ||
document.querySelector('.hide-intro').classList.remove('hide-intro'); | ||
} | ||
} | ||
|
||
var getClient = thunky(function (cb) { | ||
xhr('/rtcConfig', function (err, res) { | ||
var rtcConfig | ||
if (err || res.statusCode !== 200) { | ||
console.error('Could not get WebRTC config from server. Using default (without TURN).') | ||
} else { | ||
try { | ||
rtcConfig = JSON.parse(res.body) | ||
} catch (err) { | ||
return cb(new Error('Expected JSON response from /rtcConfig: ' + res.body)) | ||
} | ||
debug('got rtc config: %o', rtcConfig) | ||
} | ||
var client = new WebTorrent({ rtcConfig: rtcConfig }) | ||
client.on('warning', console.warn) | ||
client.on('error', console.error) | ||
cb(null, client) | ||
}) | ||
}) |
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,3 @@ | ||
exports.ports = { | ||
http: 9100 | ||
} |
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,8 @@ | ||
html, body | ||
margin: 0 | ||
padding: 0 | ||
height: 100% | ||
overflow: hidden | ||
|
||
.hide-intro | ||
display: none |
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,58 @@ | ||
{ | ||
"name": "peercloud", | ||
"description": "Serverless websites via WebTorrent", | ||
"version": "1.0.0", | ||
"author": { | ||
"name": "John Hiesey", | ||
"email": "[email protected]", | ||
"url": "http://hiesey.com/" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/jhiesey/peercloud/issues" | ||
}, | ||
"dependencies": { | ||
"compression": "^1.0.9", | ||
"debug": "^2.0.0", | ||
"express": "^4.8.5", | ||
"jade": "^1.5.0", | ||
"mime": "^1.3.4", | ||
"simple-peer": "^5.3.0", | ||
"thunky": "^0.1.0", | ||
"twilio": "^2.1.0", | ||
"webtorrent": "0.x", | ||
"xhr": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^10.0.0", | ||
"nib": "^1.0.3", | ||
"nodemon": "^1.2.1", | ||
"standard": "^3.1.1", | ||
"stylus": "^0.51.0", | ||
"watchify": "^3.1.0" | ||
}, | ||
"homepage": "https://peercloud.io", | ||
"keywords": [ | ||
"p2p", | ||
"webrtc", | ||
"data channel" | ||
], | ||
"license": "MIT", | ||
"main": "index.js", | ||
"private": true, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/jhiesey/peercloud.git" | ||
}, | ||
"scripts": { | ||
"build": "mkdir -p static && npm run build-css && npm run build-js", | ||
"build-css": "stylus -u nib css/main.styl -o static/ -c", | ||
"build-js": "browserify client > static/bundle.js", | ||
"secret-download": "rsync -a -O -v --delete hiesey.com:\"/home/jhiesey/www/peercloud.io/secret/\" secret/", | ||
"secret-upload": "rsync -a -O -v --delete secret/ hiesey.com:/home/jhiesey/www/peercloud.io/secret/", | ||
"start": "node server", | ||
"test": "standard", | ||
"watch": "npm run watch-css & npm run watch-js & DEBUG=peercloud* nodemon server -e js,jade -d 1", | ||
"watch-css": "stylus -u nib css/main.styl -o static/ -w", | ||
"watch-js": "watchify client -o static/bundle.js -dv" | ||
} | ||
} |
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,4 @@ | ||
exports.twilio = { | ||
accountSid: '', | ||
authToken: '' | ||
} |
Oops, something went wrong.