-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
334a1fb
commit 11e7c35
Showing
12 changed files
with
237 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { BaseCommand } from "../../base-command.js"; | ||
export default class Get extends BaseCommand { | ||
static description: string; | ||
static examples: string[]; | ||
static args: { | ||
address: import("@oclif/core/lib/interfaces/parser.js").Arg<string, Record<string, unknown>>; | ||
}; | ||
run(): Promise<void>; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const core_1 = require("@oclif/core"); | ||
const base_command_js_1 = require("../../base-command.js"); | ||
const lodash_1 = tslib_1.__importDefault(require("lodash")); | ||
class Get extends base_command_js_1.BaseCommand { | ||
static description = "Fetch a local or remote subplebbit, and print its json in the terminal"; | ||
static examples = [ | ||
"plebbit subplebbit get plebmusic.eth", | ||
"plebbit subplebbit get 12D3KooWG3XbzoVyAE6Y9vHZKF64Yuuu4TjdgQKedk14iYmTEPWu" | ||
]; | ||
static args = { | ||
address: core_1.Args.string({ | ||
name: "address", | ||
required: true, | ||
description: "Address of the subplebbit address to fetch" | ||
}) | ||
}; | ||
async run() { | ||
const { args, flags } = await this.parse(Get); | ||
const plebbit = await this._connectToPlebbitRpc(flags.plebbitRpcApiUrl.toString()); | ||
const sub = await plebbit.getSubplebbit(args.address); | ||
await plebbit.destroy(); | ||
this.logJson({ posts: sub.toJSON().posts, ...lodash_1.default.omit(sub.toJSON(), "posts") }); // make sure posts is printed first, because most users won't look at it | ||
} | ||
} | ||
exports.default = Get; |
This file contains 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
This file contains 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
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export declare function getPlebbitLogger(): Promise<typeof import("@plebbit/plebbit-logger", { with: { "resolution-mode": "import" } }).default>; | ||
export declare function getLanIpV4Address(): string | undefined; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getPlebbitLogger = void 0; | ||
exports.getLanIpV4Address = exports.getPlebbitLogger = void 0; | ||
const tslib_1 = require("tslib"); | ||
const os_1 = tslib_1.__importDefault(require("os")); | ||
async function getPlebbitLogger() { | ||
const Logger = await import("@plebbit/plebbit-logger"); | ||
return Logger.default; | ||
} | ||
exports.getPlebbitLogger = getPlebbitLogger; | ||
function getLanIpV4Address() { | ||
const allInterfaces = os_1.default.networkInterfaces(); | ||
for (const k in allInterfaces) { | ||
const specificInterfaceInfos = allInterfaces[k]; | ||
if (!specificInterfaceInfos) | ||
continue; | ||
const lanAddress = specificInterfaceInfos.filter((info) => info.family === "IPv4" && !info.internal)[0] | ||
?.address; | ||
if (lanAddress) | ||
return lanAddress; | ||
} | ||
return undefined; | ||
} | ||
exports.getLanIpV4Address = getLanIpV4Address; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export declare function startDaemonServer(daemonPort: number, ipfsGatewayPort: number, ipfsApiEndpoint: string, plebbitDataPath: string): Promise<{ | ||
rpcAuthKey: string; | ||
listedSub: string[]; | ||
webuis: { | ||
name: string; | ||
endpointLocal: string; | ||
endpointRemote: string; | ||
}[]; | ||
}>; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.startDaemonServer = void 0; | ||
const tslib_1 = require("tslib"); | ||
const path_1 = tslib_1.__importDefault(require("path")); | ||
const promises_1 = tslib_1.__importDefault(require("fs/promises")); | ||
const util_1 = require("../util"); | ||
const crypto_1 = require("crypto"); | ||
const express_1 = tslib_1.__importDefault(require("express")); | ||
async function _writeModifiedIndexHtmlWithDefaultSettings(webuiPath, webuiName, ipfsGatewayPort) { | ||
const indexHtmlString = (await promises_1.default.readFile(path_1.default.join(webuiPath, "index.html"))).toString(); | ||
const defaultRpcOptionString = `[window.location.origin.replace("https", "wss").replace("http", "ws") + window.location.pathname.split('/' + '${webuiName}')[0]]`; | ||
// Ipfs media only locally because ipfs gateway doesn't allow remote connections | ||
const defaultIpfsMedia = `if (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1")window.defaultMediaIpfsGatewayUrl = 'http://' + window.location.hostname + ':' + ${ipfsGatewayPort}`; | ||
const defaultOptionsString = `<script>window.defaultPlebbitOptions = {plebbitRpcClientsOptions: ${defaultRpcOptionString}};${defaultIpfsMedia};console.log(window.defaultPlebbitOptions, window.defaultMediaIpfsGatewayUrl)</script>`; | ||
const modifiedIndexHtmlFileName = `index_with_rpc_settings.html`; | ||
const modifiedIndexHtmlContent = "<!DOCTYPE html>" + defaultOptionsString + indexHtmlString.replace("<!DOCTYPE html>", ""); | ||
await promises_1.default.writeFile(path_1.default.join(webuiPath, modifiedIndexHtmlFileName), modifiedIndexHtmlContent); | ||
return modifiedIndexHtmlFileName; | ||
} | ||
async function _generateRpcAuthKeyIfNotExisting(plebbitDataPath) { | ||
// generate plebbit rpc auth key if doesn't exist | ||
const plebbitRpcAuthKeyPath = path_1.default.join(plebbitDataPath, "auth-key"); | ||
let plebbitRpcAuthKey; | ||
try { | ||
plebbitRpcAuthKey = await promises_1.default.readFile(plebbitRpcAuthKeyPath, "utf-8"); | ||
} | ||
catch (e) { | ||
plebbitRpcAuthKey = (0, crypto_1.randomBytes)(32).toString("base64").replace(/[/+=]/g, "").substring(0, 40); | ||
await promises_1.default.writeFile(plebbitRpcAuthKeyPath, plebbitRpcAuthKey, { flag: "wx" }); | ||
} | ||
return plebbitRpcAuthKey; | ||
} | ||
// The daemon server will host both RPC and webui on the same port | ||
async function startDaemonServer(daemonPort, ipfsGatewayPort, ipfsApiEndpoint, plebbitDataPath) { | ||
// Start plebbit-js RPC | ||
const log = (await (0, util_1.getPlebbitLogger)())("plebbit-cli:daemon:startDaemonServer"); | ||
const webuiExpressApp = (0, express_1.default)(); | ||
const httpServer = webuiExpressApp.listen(daemonPort); | ||
const rpcAuthKey = await _generateRpcAuthKeyIfNotExisting(plebbitDataPath); | ||
const PlebbitWsServer = await import("@plebbit/plebbit-js/dist/node/rpc/src/index.js"); | ||
const rpcServer = await PlebbitWsServer.default.PlebbitWsServer({ | ||
server: httpServer, | ||
plebbitOptions: { | ||
ipfsHttpClientsOptions: [ipfsApiEndpoint], | ||
dataPath: plebbitDataPath | ||
}, | ||
authKey: rpcAuthKey | ||
}); | ||
const webuisDir = path_1.default.join(process.cwd(), "dist", "webuis"); | ||
const webUiNames = (await promises_1.default.readdir(webuisDir, { withFileTypes: true })).filter((file) => file.isDirectory()).map((file) => file.name); | ||
const webuis = []; | ||
log("Discovered webuis", webUiNames); | ||
for (const webuiNameWithVersion of webUiNames) { | ||
const webuiDirPath = path_1.default.join(webuisDir, webuiNameWithVersion); | ||
const webuiName = webuiNameWithVersion.split("-")[0]; // should be "seedit", "plebchan", "plebones" | ||
const modifiedIndexHtmlFileName = await _writeModifiedIndexHtmlWithDefaultSettings(webuiDirPath, webuiName, ipfsGatewayPort); | ||
const endpointLocal = `/${webuiName}`; | ||
webuiExpressApp.get(endpointLocal, (req, res, next) => { | ||
const isLocal = req.socket.localAddress && req.socket.localAddress === req.socket.remoteAddress; | ||
if (!isLocal) | ||
res.status(403).send("This endpoint does not exist for remote connections"); | ||
else | ||
next(); | ||
}); | ||
webuiExpressApp.use(endpointLocal, express_1.default.static(webuiDirPath, { index: modifiedIndexHtmlFileName, cacheControl: false })); | ||
const endpointRemote = `/${rpcAuthKey}/${webuiName}`; | ||
webuiExpressApp.get(endpointRemote, (req, res, next) => { | ||
const isLocal = req.socket.localAddress && req.socket.localAddress === req.socket.remoteAddress; | ||
if (isLocal) | ||
res.redirect(`http://localhost:${daemonPort}${endpointLocal}`); | ||
else | ||
next(); | ||
}); | ||
webuiExpressApp.use(endpointRemote, express_1.default.static(webuiDirPath, { index: modifiedIndexHtmlFileName, cacheControl: false })); | ||
webuis.push({ name: webuiName, endpointLocal, endpointRemote }); | ||
} | ||
process.on("exit", async () => { | ||
await rpcServer.destroy(); | ||
httpServer.close(); | ||
}); | ||
const handlRpcExit = async (signal) => { | ||
log(`Detecting exit signal ${signal}, shutting down rpc server and webui`); | ||
await rpcServer.destroy(); | ||
httpServer.close(); | ||
process.exit(); | ||
}; | ||
["SIGINT", "SIGTERM", "SIGHUP", "beforeExit"].forEach((exitSignal) => process.on(exitSignal, handlRpcExit)); | ||
return { rpcAuthKey, listedSub: await rpcServer.plebbit.listSubplebbits(), webuis }; | ||
} | ||
exports.startDaemonServer = startDaemonServer; |