diff --git a/src/lib/constants.js b/src/lib/constants.js index 6f1f164..8005b25 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -26,10 +26,10 @@ let constants = { // Things that server owners _could_ change if they want to. tor: { - enabled: false, // If false, everything else in this block has no effect. + enabled: true, // If false, everything else in this block has no effect. password: null, // If `null`, Bibliogram will run its own Tor process instead. for: { - user_html: false, // User HTML page seems to have less forgiving rates, and Tor always fails, so it's disabled by default. + user_html: true, // User HTML page seems to have less forgiving rates, and Tor always fails, so it's disabled by default. timeline_graphql: true, post_graphql: true, reel_graphql: true @@ -40,7 +40,7 @@ let constants = { // change this to `true` to serve it, which will make extensions like Privacy Badger automatically whitelist the domain. does_not_track: false, - allow_user_from_reel: "preferForRSS", // one of: "never", "fallback", "prefer", "onlyPreferSaved", "preferForRSS" + allow_user_from_reel: "fallback", // one of: "never", "fallback", "prefer", "onlyPreferSaved", "preferForRSS" feeds: { // Whether feeds are enabled. diff --git a/src/lib/utils/tor.js b/src/lib/utils/tor.js index dd43a03..e56bd95 100644 --- a/src/lib/utils/tor.js +++ b/src/lib/utils/tor.js @@ -2,6 +2,7 @@ const SocksProxyAgent = require("socks-proxy-agent") const {connect} = require("net"); const constants = require("../constants") const {request} = require("./request") +const {RequestCache} = require("../cache") class TorManager { /** @@ -12,14 +13,17 @@ class TorManager { this.tor = tor this.port = port this.agent = new SocksProxyAgent("socks5://localhost:"+this.port) + this.circuitManager = new RequestCache() } async request(url, test) { let result = null - while (!result) { + let done = false + while (!done) { const req = await request(url, {agent: this.agent}, {log: true, statusLine: "TOR"}) try { result = await test(req) + done = true } catch (e) { await this.newCircuit() } @@ -28,9 +32,12 @@ class TorManager { } newCircuit() { - return new Promise(resolve => { - this.tor.cleanCircuits(() => resolve()) - }) + return this.circuitManager.getOrFetchPromise("circuit", () => { + console.log(" <> [TOR-CIR] Finding a new circuit...") + return new Promise(resolve => { + this.tor.cleanCircuits(() => resolve()) + }) + }).then(x => x.result) } }