*Initial commit

This commit is contained in:
Derrick Hammer 2023-01-12 12:40:28 -05:00
parent 3c52cafb11
commit d2ac85ae84
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 135 additions and 5 deletions

20
LICENSE
View File

@ -1,9 +1,21 @@
MIT License
Copyright (c) <year> <copyright holders>
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.

View File

@ -1,2 +1,2 @@
# hypercore-adapter-handshake
# hypercore-proxy-handshake

19
package.json Normal file
View File

@ -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"
}
}

84
src/index.ts Normal file
View File

@ -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);
}
}

15
tsconfig.json Normal file
View File

@ -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
}
}