Production version 0.1.0

This commit is contained in:
Carlos 2016-10-18 01:00:36 +03:00
parent 9e39231650
commit 7eb72a22b6
2 changed files with 184 additions and 162 deletions

173
cachep2p.min.js vendored
View file

@ -20334,20 +20334,23 @@ var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
var link_lists = {}
var cached_link_lists = {}
var all_links = []
var added_links = []
var history_initialized = false
inherits(CacheP2P, EventEmitter)
var cached_mark
function CacheP2P(opts, callback){
var self = this
if(typeof(opts)==='function'){
callback = opts
}
if(document.security_sha2){
if(document.security_sha1){
self.security_sha1 = document.security_sha1
}
var self = this
cached_mark = opts && opts.cached_mark ? opts.cached_mark : "* ";
if (!(self instanceof CacheP2P)) return new CacheP2P(opts)
EventEmitter.call(self)
@ -20369,6 +20372,80 @@ function CacheP2P(opts, callback){
self.announceList = opts.announceList
}
self.fetch = function(page_link){
if(!document.security_sha1 || Object.keys(document.security_sha1).indexOf(page_link.href) > -1){
if(Object.keys(cached_link_lists).indexOf(page_link.href) === -1){
self.emit('message', "Pre-fetching '"+page_link.href + "' page from other peers browsing this website...")
self.emit('alert', "Please tell a friend to open this site's "+page_link.text+" to see it in action.")
added_links.push(page_link.href)
sha(page_link.href, function(result){
var magnet = 'magnet:?xt=urn:btih:'+result+'&dn=Unnamed+Torrent+1476541118022&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.openwebtorrent.com'
torrent = client.add(magnet, onTorrent)
torrent.on('done', function (info) {
self.emit('webtorrent', 'Cache received')
})
torrent.on('download', function (bytes) {
self.emit('webtorrent', 'Receiving Cache ('+bytes+' bytes)')
})
torrent.on('wire', function (wire) {
self.emit('webtorrent', 'Peer ('+wire.remoteAddress+') connected over '+wire.type+' (Connection ID: '+wire.peerId.substr(0,10)+').')
})
})
}
}
}
self.scan_links = function(){
self.emit('message', "Pre-fetching uncached links in this page... ")
var this_page_links = document.getElementsByTagName('a')
for(var i = 0; i < this_page_links.length ; i++){
if(this_page_links[i].href && this_page_links[i].href.length !== window.location.href.length && this_page_links[i].href.indexOf(window.location.href+'#') == -1 && this_page_links[i].href.indexOf(document.domain) > -1){
if(!document.security_sha1 || Object.keys(document.security_sha1).indexOf(this_page_links[i].href) > -1){
if(Object.keys(cached_link_lists).indexOf(this_page_links[i].href) === -1){
self.fetch(this_page_links[i])
}
}
}
}
self.update_links()
}
self.update_links = function(){
var all_links = document.getElementsByTagName('a')
Object.keys(cached_link_lists).forEach(function(each_url){
var got_page = cached_link_lists[each_url]
for(var i = 0 ; i < all_links.length ; i++ ){
if(all_links[i].href === got_page.url){
console.log('found link that points to url', each_url)
var link_to_page = all_links[i]
self.emit('alert', "Checking sha1 of content received: "+sha.sync(got_page.page)+"...")
self.emit('success', "Got this site's '" +all_links[i].text+"' in Cache (sha1: "+got_page.page_hash+" ✔)")
self.emit('success', "The main server will not be used when '"+link_to_page.text+"' is clicked.")
link_to_page.onclick = function(event){
event.preventDefault();
if(!history_initialized){
window.history.pushState({page: document.documentElement.innerHTML, title: document.title},"", window.location.href);
}
document.documentElement.innerHTML = cached_link_lists[event.target.href].page
document.title = cached_mark+' '+cached_link_lists[event.target.href].title
// setTimeout(function(){
// window.scrollTo(0, 0);
// }, 10)
self.emit('cache', event)
self.emit('ready')
self.scan_links()
window.history.pushState({page: got_page.page, title: got_page.title},"", got_page.url);
}
}
}
})
}
function onTorrent (torrent) {
torrent.files.forEach(function (file) {
file.getBuffer(function (err, b) {
@ -20378,31 +20455,8 @@ function CacheP2P(opts, callback){
var got_page = JSON.parse(b.toString('utf8'))
// self.emit('message', "Got cached version of "+got_page.url+" from web peer, modifying link to point to cache.")
link_lists[got_page.url].content = got_page.page
link_lists[got_page.url].title = got_page.title
link_lists[got_page.url].url = got_page.url
var link_to_page = link_lists[got_page.url].orig
self.emit('alert', "Checking sha1 of content received: "+sha.sync(got_page.page)+"...")
self.emit('success', "Got this site's '" +link_to_page.text+"' from another Peer (sha1: "+got_page.page_hash+" ✔)")
self.emit('success', "The server will not be used when '"+link_to_page.text+"' is clicked.")
link_to_page.onclick = function(event){
event.preventDefault();
if(!history_initialized){
window.history.pushState({page: document.documentElement.innerHTML, title: document.title},"", window.location.href);
}
document.documentElement.innerHTML = link_lists[event.target.href].content
document.title = cached_mark+' '+link_lists[event.target.href].title
// setTimeout(function(){
// window.scrollTo(0, 0);
// }, 10)
self.emit('cache', event)
self.emit('ready')
window.history.pushState({page: got_page.page, title: got_page.title},"", got_page.url);
}
cached_link_lists[got_page.url] = got_page
self.update_links()
window.onpopstate = function(to) {
document.documentElement.innerHTML = to.state.page
@ -20412,16 +20466,18 @@ function CacheP2P(opts, callback){
var this_page_links = document.getElementsByTagName('a')
for(var i = 0; i < this_page_links.length ; i++){
if(Object.keys(link_lists).indexOf(this_page_links[i].href) > -1){
if(Object.keys(cached_link_lists).indexOf(this_page_links[i].href) > -1){
this_page_links[i].onclick = function(event){
event.preventDefault();
document.documentElement.innerHTML = link_lists[event.target.href].content
document.title = cached_mark+' '+link_lists[event.target.href].title
window.history.pushState({page: link_lists[event.target.href].content, title: link_lists[event.target.href].title},"", event.target.href);
document.documentElement.innerHTML = cached_link_lists[event.target.href].page
document.title = cached_mark+' '+cached_link_lists[event.target.href].title
window.history.pushState({page: cached_link_lists[event.target.href].page, title: cached_link_lists[event.target.href].title},"", event.target.href);
setTimeout(function(){
window.scrollTo(0, 0);
}, 10)
}
} else {
self.fetch(this_page_links[i])
}
}
}
@ -20430,57 +20486,11 @@ function CacheP2P(opts, callback){
}
setTimeout(function(){
var this_page_links = document.getElementsByTagName('a')
self.emit('message', "Initializing CacheP2P, pre-fetching all links in this website... ")
self.emit('message', "Initializing CacheP2P")
for(var i = 0; i < this_page_links.length ; i++){
if(this_page_links[i].href && this_page_links[i].href.length !== window.location.href.length && this_page_links[i].href.indexOf(window.location.href+'#') == -1 && this_page_links[i].href.indexOf(document.domain) > -1){
if(!link_lists[this_page_links[i].href]){
link_lists[this_page_links[i].href] = {}
}
link_lists[this_page_links[i].href].orig = this_page_links[i]
self.emit('message', "Pre-fetching '"+this_page_links[i].text + "' page from other peers browsing this website...")
self.emit('alert', "Please tell a friend to open this site's "+this_page_links[i].text+" to see it in action.")
sha(this_page_links[i].href, function(result){
var magnet = 'magnet:?xt=urn:btih:'+result+'&dn=Unnamed+Torrent+1476541118022&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.openwebtorrent.com'
torrent = client.add(magnet, onTorrent)
torrent.on('done', function (info) {
self.emit('webtorrent', 'Cache received')
})
torrent.on('download', function (bytes) {
self.emit('webtorrent', 'Receiving ('+bytes+' bytes)')
})
torrent.on('wire', function (wire) {
self.emit('webtorrent', 'Peer ('+wire.remoteAddress+') connected over '+wire.type+' (Connection ID: '+wire.peerId.substr(0,10)+').')
})
})
self.scan_links()
}
}
self.emit('message', "Waiting for other people that are browing this same website...")
// var links = document.getElementsByTagName('link')
// var pageCssCache = ""
// for(i = 0; i < links.length; i++){
// if(links[i].rel.indexOf('stylesheet') > -1){
// cssRules = links[i].sheet.cssRules
// if(cssRules){
// for(r = 0; r < cssRules.length ; r++){
// pageCssCache = pageCssCache + cssRules[r].cssText
// }
// }
// }
// }
// var styles = document.getElementsByTagName('style')
// for(i = 0; i < styles.length; i++){
// cssRules = styles[i].innerHTML
// pageCssCache = pageCssCache + cssRules
// }
var message = {
location_href: window.location.href.split('#')[0],
content: document.documentElement.innerHTML,
@ -20499,11 +20509,12 @@ function CacheP2P(opts, callback){
var torrent = client.seed(buffer_payload,{forced_id: hash, announceList: self.announceList}, function(torrent){
// add_to_list(torrent, message.location_href)
debug(torrent.magnetURI)
cached_link_lists[message.location_href] = payload
torrent.on('upload', function (bytes) {
self.emit('webtorrent', 'Sending this page to peer ('+bytes+' bytes)')
})
torrent.on('wire', function (wire) {
console.log('wire2', wire)
self.emit('webtorrent', 'Peer ('+wire.remoteAddress+') connected over '+wire.type+' (Connection ID: '+wire.peerId.substr(0,10)+').')
})
// document.title = document.title

173
index.js
View file

@ -10,20 +10,23 @@ var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
var link_lists = {}
var cached_link_lists = {}
var all_links = []
var added_links = []
var history_initialized = false
inherits(CacheP2P, EventEmitter)
var cached_mark
function CacheP2P(opts, callback){
var self = this
if(typeof(opts)==='function'){
callback = opts
}
if(document.security_sha2){
if(document.security_sha1){
self.security_sha1 = document.security_sha1
}
var self = this
cached_mark = opts && opts.cached_mark ? opts.cached_mark : "* ";
if (!(self instanceof CacheP2P)) return new CacheP2P(opts)
EventEmitter.call(self)
@ -45,6 +48,80 @@ function CacheP2P(opts, callback){
self.announceList = opts.announceList
}
self.fetch = function(page_link){
if(!document.security_sha1 || Object.keys(document.security_sha1).indexOf(page_link.href) > -1){
if(Object.keys(cached_link_lists).indexOf(page_link.href) === -1){
self.emit('message', "Pre-fetching '"+page_link.href + "' page from other peers browsing this website...")
self.emit('alert', "Please tell a friend to open this site's "+page_link.text+" to see it in action.")
added_links.push(page_link.href)
sha(page_link.href, function(result){
var magnet = 'magnet:?xt=urn:btih:'+result+'&dn=Unnamed+Torrent+1476541118022&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.openwebtorrent.com'
torrent = client.add(magnet, onTorrent)
torrent.on('done', function (info) {
self.emit('webtorrent', 'Cache received')
})
torrent.on('download', function (bytes) {
self.emit('webtorrent', 'Receiving Cache ('+bytes+' bytes)')
})
torrent.on('wire', function (wire) {
self.emit('webtorrent', 'Peer ('+wire.remoteAddress+') connected over '+wire.type+' (Connection ID: '+wire.peerId.substr(0,10)+').')
})
})
}
}
}
self.scan_links = function(){
self.emit('message', "Pre-fetching uncached links in this page... ")
var this_page_links = document.getElementsByTagName('a')
for(var i = 0; i < this_page_links.length ; i++){
if(this_page_links[i].href && this_page_links[i].href.length !== window.location.href.length && this_page_links[i].href.indexOf(window.location.href+'#') == -1 && this_page_links[i].href.indexOf(document.domain) > -1){
if(!document.security_sha1 || Object.keys(document.security_sha1).indexOf(this_page_links[i].href) > -1){
if(Object.keys(cached_link_lists).indexOf(this_page_links[i].href) === -1){
self.fetch(this_page_links[i])
}
}
}
}
self.update_links()
}
self.update_links = function(){
var all_links = document.getElementsByTagName('a')
Object.keys(cached_link_lists).forEach(function(each_url){
var got_page = cached_link_lists[each_url]
for(var i = 0 ; i < all_links.length ; i++ ){
if(all_links[i].href === got_page.url){
console.log('found link that points to url', each_url)
var link_to_page = all_links[i]
self.emit('alert', "Checking sha1 of content received: "+sha.sync(got_page.page)+"...")
self.emit('success', "Got this site's '" +all_links[i].text+"' in Cache (sha1: "+got_page.page_hash+" ✔)")
self.emit('success', "The main server will not be used when '"+link_to_page.text+"' is clicked.")
link_to_page.onclick = function(event){
event.preventDefault();
if(!history_initialized){
window.history.pushState({page: document.documentElement.innerHTML, title: document.title},"", window.location.href);
}
document.documentElement.innerHTML = cached_link_lists[event.target.href].page
document.title = cached_mark+' '+cached_link_lists[event.target.href].title
// setTimeout(function(){
// window.scrollTo(0, 0);
// }, 10)
self.emit('cache', event)
self.emit('ready')
self.scan_links()
window.history.pushState({page: got_page.page, title: got_page.title},"", got_page.url);
}
}
}
})
}
function onTorrent (torrent) {
torrent.files.forEach(function (file) {
file.getBuffer(function (err, b) {
@ -54,31 +131,8 @@ function CacheP2P(opts, callback){
var got_page = JSON.parse(b.toString('utf8'))
// self.emit('message', "Got cached version of "+got_page.url+" from web peer, modifying link to point to cache.")
link_lists[got_page.url].content = got_page.page
link_lists[got_page.url].title = got_page.title
link_lists[got_page.url].url = got_page.url
var link_to_page = link_lists[got_page.url].orig
self.emit('alert', "Checking sha1 of content received: "+sha.sync(got_page.page)+"...")
self.emit('success', "Got this site's '" +link_to_page.text+"' from another Peer (sha1: "+got_page.page_hash+" ✔)")
self.emit('success', "The server will not be used when '"+link_to_page.text+"' is clicked.")
link_to_page.onclick = function(event){
event.preventDefault();
if(!history_initialized){
window.history.pushState({page: document.documentElement.innerHTML, title: document.title},"", window.location.href);
}
document.documentElement.innerHTML = link_lists[event.target.href].content
document.title = cached_mark+' '+link_lists[event.target.href].title
// setTimeout(function(){
// window.scrollTo(0, 0);
// }, 10)
self.emit('cache', event)
self.emit('ready')
window.history.pushState({page: got_page.page, title: got_page.title},"", got_page.url);
}
cached_link_lists[got_page.url] = got_page
self.update_links()
window.onpopstate = function(to) {
document.documentElement.innerHTML = to.state.page
@ -88,16 +142,18 @@ function CacheP2P(opts, callback){
var this_page_links = document.getElementsByTagName('a')
for(var i = 0; i < this_page_links.length ; i++){
if(Object.keys(link_lists).indexOf(this_page_links[i].href) > -1){
if(Object.keys(cached_link_lists).indexOf(this_page_links[i].href) > -1){
this_page_links[i].onclick = function(event){
event.preventDefault();
document.documentElement.innerHTML = link_lists[event.target.href].content
document.title = cached_mark+' '+link_lists[event.target.href].title
window.history.pushState({page: link_lists[event.target.href].content, title: link_lists[event.target.href].title},"", event.target.href);
document.documentElement.innerHTML = cached_link_lists[event.target.href].page
document.title = cached_mark+' '+cached_link_lists[event.target.href].title
window.history.pushState({page: cached_link_lists[event.target.href].page, title: cached_link_lists[event.target.href].title},"", event.target.href);
setTimeout(function(){
window.scrollTo(0, 0);
}, 10)
}
} else {
self.fetch(this_page_links[i])
}
}
}
@ -106,57 +162,11 @@ function CacheP2P(opts, callback){
}
setTimeout(function(){
var this_page_links = document.getElementsByTagName('a')
self.emit('message', "Initializing CacheP2P, pre-fetching all links in this website... ")
self.emit('message', "Initializing CacheP2P")
for(var i = 0; i < this_page_links.length ; i++){
if(this_page_links[i].href && this_page_links[i].href.length !== window.location.href.length && this_page_links[i].href.indexOf(window.location.href+'#') == -1 && this_page_links[i].href.indexOf(document.domain) > -1){
if(!link_lists[this_page_links[i].href]){
link_lists[this_page_links[i].href] = {}
}
link_lists[this_page_links[i].href].orig = this_page_links[i]
self.emit('message', "Pre-fetching '"+this_page_links[i].text + "' page from other peers browsing this website...")
self.emit('alert', "Please tell a friend to open this site's "+this_page_links[i].text+" to see it in action.")
sha(this_page_links[i].href, function(result){
var magnet = 'magnet:?xt=urn:btih:'+result+'&dn=Unnamed+Torrent+1476541118022&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.openwebtorrent.com'
torrent = client.add(magnet, onTorrent)
torrent.on('done', function (info) {
self.emit('webtorrent', 'Cache received')
})
torrent.on('download', function (bytes) {
self.emit('webtorrent', 'Receiving ('+bytes+' bytes)')
})
torrent.on('wire', function (wire) {
self.emit('webtorrent', 'Peer ('+wire.remoteAddress+') connected over '+wire.type+' (Connection ID: '+wire.peerId.substr(0,10)+').')
})
})
self.scan_links()
}
}
self.emit('message', "Waiting for other people that are browing this same website...")
// var links = document.getElementsByTagName('link')
// var pageCssCache = ""
// for(i = 0; i < links.length; i++){
// if(links[i].rel.indexOf('stylesheet') > -1){
// cssRules = links[i].sheet.cssRules
// if(cssRules){
// for(r = 0; r < cssRules.length ; r++){
// pageCssCache = pageCssCache + cssRules[r].cssText
// }
// }
// }
// }
// var styles = document.getElementsByTagName('style')
// for(i = 0; i < styles.length; i++){
// cssRules = styles[i].innerHTML
// pageCssCache = pageCssCache + cssRules
// }
var message = {
location_href: window.location.href.split('#')[0],
content: document.documentElement.innerHTML,
@ -175,11 +185,12 @@ function CacheP2P(opts, callback){
var torrent = client.seed(buffer_payload,{forced_id: hash, announceList: self.announceList}, function(torrent){
// add_to_list(torrent, message.location_href)
debug(torrent.magnetURI)
cached_link_lists[message.location_href] = payload
torrent.on('upload', function (bytes) {
self.emit('webtorrent', 'Sending this page to peer ('+bytes+' bytes)')
})
torrent.on('wire', function (wire) {
console.log('wire2', wire)
self.emit('webtorrent', 'Peer ('+wire.remoteAddress+') connected over '+wire.type+' (Connection ID: '+wire.peerId.substr(0,10)+').')
})
// document.title = document.title