Emergency jank: Route through Tor

This commit is contained in:
Cadence Ember 2020-11-17 18:30:18 +13:00
parent 0e320c16d2
commit 72ee788799
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17
2 changed files with 14 additions and 7 deletions

View File

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

View File

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