Skip to content

Instantly share code, notes, and snippets.

@lanmower
Last active June 24, 2021 18:12
Show Gist options
  • Save lanmower/fb32a656e3dfc6eb55aa1d5a048b2ad5 to your computer and use it in GitHub Desktop.
Save lanmower/fb32a656e3dfc6eb55aa1d5a048b2ad5 to your computer and use it in GitHub Desktop.
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