Instantly share code, notes, and snippets.

@juliangruber /REAMD.MD Secret
Last active Aug 4, 2016

Embed
What would you like to do?
PROXY
const assert = require('assert')
const discoverySwarm = require('discovery-swarm')
const protocol = require('hypercore-protocol')
const peek = require('peek-length-prefixed-message')
const net = require('net')
const discoveryChannel = require('discovery-channel')
module.exports = Proxy
function Proxy (opts) {
assert(opts.discovery, '.discovery required')
var self = this
opts.discovery.stream = function () {
return protocol()
}
opts.discovery.hash = false
var waiting = {}
swarm = discoverySwarm(opts.discovery)
swarm.on('connection', function (con, info) {
console.log('swarm connection')
if (info.channel) join(info.channel)
else stream.once('open', join)
function join (discoveryKey) {
var key = discoveryKey.toString('hex')
console.log('swarm key', key)
console.log('waiting??', !!waiting[key])
if (!waiting[key]) return con.destroy()
waiting[key].pipe(con).pipe(waiting[key])
delete waiting[key]
swarm.leave(key)
}
})
swarm.listen()
var channel = discoveryChannel()
var server = net.createServer(function (con) {
console.log('tcp connection')
con = peek(con, function (err, msg) {
const key = protocol.parseDiscoveryKey(con).toString('hex')
console.log('tcp key', key)
swarm.join(key)
waiting[key] = con
})
})
server.listen(function () {
console.log('tcp listening')
channel.join('hypercore-proxy', server.address().port)
})
}
{
"name": "hypercore-proxy",
"version": "0.0.0",
"repository": "juliangruber/hypercore-proxy",
"license": "MIT",
"dependencies": {
"discovery-channel": "^5.3.0",
"discovery-swarm": "^4.0.2",
"hypercore-protocol": "^4.4.0",
"peek-length-prefixed-message": "^1.0.2",
"signalhub": "^4.7.4"
},
"devDependencies": {
"datland-swarm-defaults": "^1.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment