Last active
June 24, 2021 18:12
-
-
Save lanmower/fb32a656e3dfc6eb55aa1d5a048b2ad5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const DHT = require("@hyperswarm/dht"); | |
const crypto = require('hypercore-crypto') | |
const node = new DHT({}); | |
module.exports = (key='')=>{ | |
return { | |
serve: (command, cb)=>{ | |
const keyPair = crypto.keyPair(crypto.data(Buffer.from(key+command))); | |
const server = node.createServer(); | |
server.on("connection", function(socket) { | |
socket.on("data", async data => { | |
cb(data, (err, output)=>{ | |
if(err) throw err; | |
socket.write(output); | |
socket.end(); | |
}); | |
}); | |
}); | |
server.listen(keyPair); | |
}, | |
run:(command, args, cb)=>{ | |
const keyPair = crypto.keyPair(crypto.data(Buffer.from(key+command))); | |
node.connect(keyPair.publicKey); | |
const socket = node.connect(keyPair.publicKey); | |
socket.on("data", (res)=>{ | |
cb(null, res); | |
socket.end(); | |
}); | |
socket.write(args); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment