From 1fcdfce868c6b0dce41b7bb024908b2e10d2b1ea Mon Sep 17 00:00:00 2001 From: Cadence Fish Date: Mon, 3 Feb 2020 04:09:40 +1300 Subject: [PATCH] Add Tor status to home and stats --- src/lib/utils/torswitcher.js | 4 ++++ src/site/api/routes.js | 4 +++- src/site/api/stats.js | 8 +++++++- src/site/pug/home.pug | 3 ++- src/site/server.js | 8 +++++++- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/lib/utils/torswitcher.js b/src/lib/utils/torswitcher.js index ab8a840..e76195a 100644 --- a/src/lib/utils/torswitcher.js +++ b/src/lib/utils/torswitcher.js @@ -10,6 +10,10 @@ class TorSwitcher { this.torManager = torManager } + canUseTor() { + return !!this.torManager + } + /** * Request from the URL. * The test function will be called with the response object. diff --git a/src/site/api/routes.js b/src/site/api/routes.js index f480f66..91f6f99 100644 --- a/src/site/api/routes.js +++ b/src/site/api/routes.js @@ -1,4 +1,5 @@ const constants = require("../../lib/constants") +const switcher = require("../../lib/utils/torswitcher") const {fetchUser, getOrFetchShortcode, userRequestCache, history} = require("../../lib/collectors") const {render, redirect} = require("pinski/plugins") const {pugCache} = require("../passthrough") @@ -8,7 +9,8 @@ module.exports = [ route: "/", methods: ["GET"], code: async () => { return render(200, "pug/home.pug", { rssEnabled: constants.settings.rss_enabled, - allUnblocked: history.testNoneBlocked() + allUnblocked: history.testNoneBlocked(), + torAvailable: switcher.canUseTor() }) } }, diff --git a/src/site/api/stats.js b/src/site/api/stats.js index 259402e..1d89a1c 100644 --- a/src/site/api/stats.js +++ b/src/site/api/stats.js @@ -3,6 +3,7 @@ const child_process = require("child_process") const md = require("mixin-deep") const constants = require("../../lib/constants") const {history} = require("../../lib/collectors") +const switcher = require("../../lib/utils/torswitcher") const {redirect} = require("pinski/plugins") function reply(statusCode, content) { @@ -27,7 +28,7 @@ let commit = "" // Set up inner versioning -const displayVersions = ["1.0", "1.1", "1.2"] +const displayVersions = ["1.0", "1.1", "1.2", "1.3"] const versions = new Map(displayVersions.map(v => [v, semver.coerce(v)])) const features = [ "PAGE_PROFILE", @@ -61,6 +62,11 @@ const innerMap = new Map() rssEnabled: constants.settings.rss_enabled } }) + addVersion("1.3", { + settings: { + torAvailable: switcher.canUseTor() // server.js holds on loading this file until tor state is known, so this is fine + } + }) } module.exports = [ diff --git a/src/site/pug/home.pug b/src/site/pug/home.pug index 1e4b3a6..8d37e50 100644 --- a/src/site/pug/home.pug +++ b/src/site/pug/home.pug @@ -1,4 +1,4 @@ -//- Needs rssEnabled, allUnblocked +//- Needs rssEnabled, allUnblocked, torAvailable doctype html html @@ -37,6 +37,7 @@ html ul li Instance is #{allUnblocked ? "not blocked" : "blocked"} li RSS feeds are #{rssEnabled ? "enabled" : "disabled"} + li Tor is #{torAvailable ? "enabled" : "not available"} h2 External links ul.link-list - diff --git a/src/site/server.js b/src/site/server.js index a4d3e53..2dbc285 100644 --- a/src/site/server.js +++ b/src/site/server.js @@ -1,5 +1,6 @@ const {Pinski} = require("pinski") const {subdirs} = require("node-dir") +const constants = require("../lib/constants") const passthrough = require("./passthrough") @@ -8,7 +9,7 @@ const pinski = new Pinski({ relativeRoot: __dirname }) -subdirs("pug", (err, dirs) => { +subdirs("pug", async (err, dirs) => { if (err) throw err //pinski.addRoute("/", "pug/index.pug", "pug") @@ -20,6 +21,11 @@ subdirs("pug", (err, dirs) => { pinski.muteLogsStartingWith("/imageproxy") pinski.muteLogsStartingWith("/videoproxy") pinski.muteLogsStartingWith("/static") + + if (constants.tor.enabled) { + await require("../lib/utils/tor") // make sure tor state is known before going further + } + pinski.startServer() pinski.enableWS()