From d2ac85ae84e1dcb19737b29bb965cd448fef37ee Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 12 Jan 2023 12:40:28 -0500 Subject: [PATCH] *Initial commit --- LICENSE | 20 +++++++++--- README.md | 2 +- package.json | 19 ++++++++++++ src/index.ts | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 15 +++++++++ 5 files changed, 135 insertions(+), 5 deletions(-) create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/LICENSE b/LICENSE index 2071b23..f1938bf 100644 --- a/LICENSE +++ b/LICENSE @@ -1,9 +1,21 @@ MIT License -Copyright (c) +Copyright (c) 2023 Hammer Technologies LLC -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 03f551b..0a156f9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# hypercore-adapter-handshake +# hypercore-proxy-handshake diff --git a/package.json b/package.json new file mode 100644 index 0000000..11f0b5a --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "@lumeweb/hypercore-proxy-handshake", + "version": "0.1.0", + "dependencies": { + "@lumeweb/libhyperproxy": "https://git.lumeweb.com/LumeWeb/libhyperproxy.git", + "b4a": "^1.6.1", + "compact-encoding": "^2.11.0", + "hsd": "^4.0.1", + "hyperswarm": "^4.3.5", + "protomux": "^3.4.0", + "streamx": "^2.13.0" + }, + "devDependencies": { + "@types/b4a": "^1.6.0", + "@types/node": "^18.11.18", + "prettier": "^2.8.2", + "typescript": "^4.9.4" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..af1f498 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,84 @@ +// @ts-ignore +import Protomux from "protomux"; +// @ts-ignore +import { SPVNode } from "hsd/lib/node"; +// @ts-ignore +import { Peer as HNSPeer } from "hsd/lib/net"; +// @ts-ignore +import sodium from "sodium-universal"; +import b4a from "b4a"; +// @ts-ignore +import c from "compact-encoding"; +const PROTOCOL = "lumeweb.proxy.handshake"; + +import { + Proxy, + createSocket, + createServer, + Socket, +} from "@lumeweb/libhyperproxy"; + +export default class HandshakeProxy extends Proxy { + private _node?: any; + constructor({ swarm, listen = false }: { swarm: any; listen?: boolean }) { + super({ + swarm, + listen, + autostart: true, + protocol: PROTOCOL, + async onopen(socket: Socket) { + const peer = HNSPeer.fromInbound(self._node.pool.options, socket); + peer.connected = false; + peer.outbound = true; + + if ( + self._node.pool.peers.map.has(peer.hostname()) || + self._node.pool.peers.ids.has(peer.id) + ) { + return { connect: false }; + } + + self._node.pool.bindPeer(peer); + const open = peer.tryOpen(); + // @ts-ignore + socket.emit("connect"); + await open; + self._node.pool.peers.add(peer); + self._node.pool.connectedGroups.add(peer.address.getGroup()); + self._node.pool.emit("peer", peer); + return { connect: false }; + // self._node.pool.server.emit("connection", socket); + }, + }); + const self = this; + } + + protected async _init() { + this._node = new SPVNode({ + config: false, + argv: false, + env: false, + noDns: true, + memory: true, + logFile: false, + logConsole: true, + logLevel: "info", + workers: true, + network: "main", + createServer, + createSocket, + }); + + this._node.http.http.listen = (port: number, host: string, cb: Function) => + cb(); + + await this._node.open(); + this._node.pool.connected = true; + this._node.startSync(); + + const topic = b4a.from(PROTOCOL); + const topicHash = b4a.allocUnsafe(32); + sodium.crypto_generichash(topicHash, topic); + this.swarm.join(topicHash); + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..c714ea9 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "target": "es2020", + "module": "commonjs", + "moduleResolution": "node", + "rootDir": "src", + "outDir": "dist", + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "resolveJsonModule": true, + "declaration": true + } +}