Skip to content

Commit

Permalink
More work w/ DHT
Browse files Browse the repository at this point in the history
  • Loading branch information
cretz committed Feb 10, 2019
1 parent d2bac8d commit 7762961
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Early sandbox repo playing around w/ getting signaling to work w/ WebRTC via IPFS.

Result: IPFS is close but not quite ready for DHT. It doesn't have any bootstrappable peers that I can see and the built
JS from current master is like 2.5MB minimized. Will be waiting...
4 changes: 2 additions & 2 deletions browser-answer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<title>WebRTC IPFS Signalling Answerer</title>
<script src="https://unpkg.com/[email protected]/dist/index.js"></script>
<!-- <script src="js-ipfs.min.js"></script> -->
<script src="browser.js"></script>
</head>
<body>
Expand Down Expand Up @@ -36,8 +37,7 @@ <h3>WebRTC IPFS Signalling Answerer</h3>
location.reload()
}
// Create IPFS and do everything else once ready
const ipfs = new Ipfs()
ipfs.on('ready', () => {
newIPFS(ipfs => {
console.log('IPFS ready')

// Create new RTC conn
Expand Down
4 changes: 2 additions & 2 deletions browser-offer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<title>WebRTC IPFS Signalling Offerer</title>
<script src="https://unpkg.com/[email protected]/dist/index.js"></script>
<!-- <script src="js-ipfs.min.js"></script> -->
<script src="browser.js"></script>
</head>
<body>
Expand Down Expand Up @@ -36,8 +37,7 @@ <h3>WebRTC IPFS Signalling Offerer</h3>
location.reload()
}
// Create IPFS and do everything else once ready
const ipfs = new Ipfs()
ipfs.on('ready', () => {
newIPFS(ipfs => {
console.log('IPFS ready')

// Create new RTC conn
Expand Down
65 changes: 63 additions & 2 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const debug = console.log

function createWindowHashIfNotPresent() {
if (window.location.hash) return
const base58Chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
Expand All @@ -8,29 +10,88 @@ function createWindowHashIfNotPresent() {
window.history.replaceState(null, null, '#' + String.fromCharCode.apply(null, array))
}

function newIPFS(cb) {
const ipfs = new Ipfs({
// repo: String(Math.random() + Date.now()),
// libp2p: {
// config: {
// dht: {
// enabled: true
// }
// }
// },
// relay: {
// enabled: true,
// hop: {enabled: true}
// }
})
// Wait for peers
ipfs.on('ready', () => {
const tryListPeers = () => {
ipfs.swarm.peers((err, peers) => {
if (err) throw err
debug('Peers', peers)
if (!peers || peers.length == 0) setTimeout(() => tryListPeers(), 1000)
else cb(ipfs)
})
}
tryListPeers()
})
}

function ipfsDirBase() {
return window.location.hash.substring(1)
}

function writeIPFSFile(ipfs, file, content, cb) {
const path = '/' + ipfsDirBase() + '/' + file
const buf = ipfs.types.Buffer.from(content)
debug('Writing to ' + path)
writeIPFSFileMFS(ipfs, path, buf, cb)
// writeIPFSFileDHT(ipfs, path, buf, cb)
}

function writeIPFSFileMFS(ipfs, path, buf, cb) {
ipfs.files.write(path, buf, { create: true, truncate: true, parents: true}).then(() => {
console.log('Wrote to ' + path)
cb()
}).catch(console.error)
}

function writeIPFSFileDHT(ipfs, path, buf, cb) {
ipfs.dht.put(ipfs.types.Buffer.from(path), buf, (err) => {
if (err) throw err
console.log('Wrote to ' + path)
cb()
})
}

function waitForIPFSFile(ipfs, file, cb) {
const path = '/' + ipfsDirBase() + '/' + file
debug('Attempting to read from ' + path)
waitForIPFSFileMFS(ipfs, path, cb)
// waitForIPFSFileDHT(ipfs, path, cb)
}

function waitForIPFSFileMFS(ipfs, path, cb) {
ipfs.files.read(path, (err, buf) => {
if (err) {
if (!err.message || !err.message.endsWith(file + ' does not exist')) throw err
setTimeout(() => waitForIPFSFile(ipfs, file, cb), 2000)
debug('Failed reading', err.message)
if (!err.message || !err.message.endsWith(path + ' does not exist')) throw err
setTimeout(() => waitForIPFSFileMFS(ipfs, path, cb), 2000)
} else cb(buf.toString('utf8'))
})
}

function waitForIPFSFileDHT(ipfs, path, cb) {
ipfs.dht.get(ipfs.types.Buffer.from(path), (err, value) => {
if (err) {
debug('Failed reading', err.message)
setTimeout(() => waitForIPFSFileDHT(ipfs, path, cb), 2000)
} else cb(value.toString('utf8'))
})
}

function setupChatChannel(channel) {
const areaElem = document.getElementById('chat')
const messageElem = document.getElementById('message')
Expand Down
15 changes: 15 additions & 0 deletions local_web_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"log"
"net/http"
)

func main() {
fs := http.FileServer(http.Dir("."))
err := http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
fs.ServeHTTP(w, r)
}))
log.Fatal(err)
}

0 comments on commit 7762961

Please sign in to comment.