*Initial version

This commit is contained in:
Derrick Hammer 2022-12-20 13:34:21 -05:00
parent 72787e9cdd
commit 2a881417f2
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
7 changed files with 2281 additions and 0 deletions

View File

@ -0,0 +1,15 @@
diff --git a/src/index.js b/src/index.js
index bf4c57f259df2e16761b45e2636db307c89ba419..a23827c73f2da725294298467fc3a07a0e4df34a 100644
--- a/src/index.js
+++ b/src/index.js
@@ -3,8 +3,5 @@
* treat as a browser.
*/
-if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
- module.exports = require('./browser.js');
-} else {
- module.exports = require('./node.js');
-}
+module.exports = require('./node.js');
+

29
package.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "@lumeweb/relay-plugin-discovery-bittorrent",
"version": "0.1.0",
"type": "module",
"scripts": {
"build": "rollup -c rollup.config.js"
},
"devDependencies": {
"@lumeweb/peer-discovery": "https://git.lumeweb.com/LumeWeb/peer-discovery.git",
"@lumeweb/relay-types": "https://git.lumeweb.com/LumeWeb/relay-types.git",
"@types/b4a": "^1.6.0",
"object-merger": "^1.0.3",
"prettier": "^2.8.1",
"rollup": "^3.7.5"
},
"dependencies": {
"@lumeweb/relay-plugin-rollup-preset": "https://git.lumeweb.com/LumeWeb/relay-plugin-rollup-preset.git",
"b4a": "^1.6.1",
"bittorrent-dht": "^11.0.1",
"bittorrent-dht-sodium": "^1.2.0",
"sha.js": "^2.4.11"
},
"resolutions": {
"debug@^4.3.4": "patch:debug@npm%3A4.3.4#./.yarn/patches/debug-npm-4.3.4-4513954577.patch",
"debug@4": "patch:debug@npm%3A4.3.4#./.yarn/patches/debug-npm-4.3.4-4513954577.patch",
"debug@^4.1.0": "patch:debug@npm%3A4.3.4#./.yarn/patches/debug-npm-4.3.4-4513954577.patch",
"debug@^4.3.3": "patch:debug@npm%3A4.3.4#./.yarn/patches/debug-npm-4.3.4-4513954577.patch"
}
}

View File

@ -0,0 +1,5 @@
{
"plugins": [
"discovery-bittorrent"
]
}

13
rollup.config.js Normal file
View File

@ -0,0 +1,13 @@
import { defineConfig } from "rollup";
import preset from "@lumeweb/relay-plugin-rollup-preset";
import merger from "object-merger";
export default defineConfig(
merger(preset(), {
input: "src/index.ts",
output: {
file: "dist/discovery-bittorrent.js",
format: "cjs",
},
})
);

85
src/index.ts Normal file
View File

@ -0,0 +1,85 @@
import type { Plugin, PluginAPI } from "@lumeweb/relay-types";
import DHT from "bittorrent-dht";
import eddsa from "bittorrent-dht-sodium";
import type { Peer } from "@lumeweb/peer-discovery";
import b4a from "b4a";
import sha from "sha.js";
async function get(dht: DHT, api: PluginAPI) {
return new Promise((resolve, reject) => {
dht.get(getHash(api.identity.publicKeyRaw), (err, res) => {
if (err) {
reject(err);
return;
}
resolve(res);
});
});
}
async function put(dht: DHT, api: PluginAPI) {
let existing;
let seq = 1;
try {
existing = await get(dht, api);
seq = existing.seq;
} catch (e) {
api.logger.debug(e);
}
let data = getData(api);
if (existing) {
let existingData = JSON.parse(existing.v.toString());
if (data.host !== existingData.host || data.port !== existingData.port) {
seq++;
}
}
dht.put(
{
k: api.identity.publicKeyRaw,
seq,
v: getDataEncoded(api),
sign: function (buf) {
let key = b4a.alloc(64);
b4a.copy(api.identity.privateKey, key);
b4a.copy(api.identity.publicKeyRaw, key, 32);
return eddsa.sign(buf, key);
},
},
function (err) {
if (err) {
api.logger.error(err);
}
}
);
}
function getData(api: PluginAPI): Peer {
return {
host: api.config.str("domain"),
port: api.config.uint("port"),
};
}
function getDataEncoded(api: PluginAPI): Buffer {
return b4a.from(JSON.stringify(getData(api))) as Buffer;
}
function getHash(pubkey): string {
return sha("sha1").update(pubkey).digest();
}
const plugin: Plugin = {
name: "discovery-bittorrent",
async plugin(api: PluginAPI): Promise<void> {
debugger;
const dht = new DHT({ verify: eddsa.verify });
await put(dht, api);
setTimeout(() => put(dht, api), 1000 * 60 * 60);
},
};
export default plugin;

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"esModuleInterop": true,
"outDir": "dist",
"declaration": true,
"moduleResolution": "node"
},
"include": ["src"],
"exclude": [
"node_modules"
]
}

2120
yarn.lock Normal file

File diff suppressed because it is too large Load Diff