@dominevo/series-scraper-core 0.0.87 → 0.0.89
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/controllers/logs.controller.d.ts +7 -6
- package/dist/controllers/logs.controller.js +26 -9
- package/dist/controllers/vpn.controller.d.ts +2 -0
- package/dist/controllers/vpn.controller.js +14 -4
- package/dist/index.d.ts +5 -0
- package/dist/index.js +10 -1
- package/dist/ports/host-info.d.ts +6 -0
- package/dist/ports/log-files.d.ts +17 -7
- package/dist/ports/log-files.js +12 -0
- package/dist/ports/log-scrub.d.ts +2 -0
- package/dist/ports/log-scrub.js +58 -0
- package/dist/ports/logger.d.ts +0 -5
- package/dist/ports/logger.js +12 -2
- package/dist/scraper.module.js +6 -0
- package/dist/services/core.service.d.ts +4 -1
- package/dist/services/core.service.js +21 -2
- package/dist/services/download.service.d.ts +3 -0
- package/dist/services/download.service.js +33 -0
- package/dist/services/egress/direct-allowance.d.ts +17 -0
- package/dist/services/egress/direct-allowance.js +14 -0
- package/dist/services/egress/egress-gate.d.ts +66 -0
- package/dist/services/egress/egress-gate.js +119 -0
- package/dist/services/egress/egress-router.d.ts +2 -0
- package/dist/services/egress/egress-router.js +5 -0
- package/dist/services/egress/firewall-policy.d.ts +3 -2
- package/dist/services/egress/firewall-policy.js +4 -3
- package/dist/services/egress/outbound-routes.d.ts +2 -0
- package/dist/services/egress/outbound-routes.js +33 -0
- package/dist/services/egress/routing-dispatcher.js +7 -0
- package/dist/services/egress/vpn-gate.guard.d.ts +8 -0
- package/dist/services/egress/vpn-gate.guard.js +35 -0
- package/dist/services/plugin-loader.service.d.ts +12 -1
- package/dist/services/plugin-loader.service.js +44 -3
- package/dist/services/plugin-network-guard.js +4 -2
- package/dist/services/vpn.service.d.ts +32 -2
- package/dist/services/vpn.service.js +216 -17
- package/package.json +1 -1
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { HostInfo } from '../../ports/host-info';
|
|
2
|
+
import { EgressNotReadyError } from './egress-router';
|
|
3
|
+
import type { LeakVerdict } from './leak-guard';
|
|
4
|
+
/** Which setting names an exit: the default, a host rule, or the captcha fallback. */
|
|
5
|
+
export type GateExitRole = 'default' | 'rule' | 'captcha';
|
|
6
|
+
export interface GateExitStatus {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
roles: GateExitRole[];
|
|
10
|
+
/** The tunnel is up and the connection test passed. */
|
|
11
|
+
ready: boolean;
|
|
12
|
+
/** Exit IP against the direct IP: `unknown` until a check has answered. */
|
|
13
|
+
verdict: LeakVerdict;
|
|
14
|
+
error?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface GateStatus {
|
|
17
|
+
/** The host demands a VPN exit before any outbound work. Without it the gate is always open. */
|
|
18
|
+
required: boolean;
|
|
19
|
+
open: boolean;
|
|
20
|
+
/** Why it is closed; absent when open. */
|
|
21
|
+
reason?: string;
|
|
22
|
+
/** When the gate last changed state (ISO). */
|
|
23
|
+
since: string;
|
|
24
|
+
exits: GateExitStatus[];
|
|
25
|
+
}
|
|
26
|
+
export type GateEvaluation = {
|
|
27
|
+
open: true;
|
|
28
|
+
} | {
|
|
29
|
+
open: false;
|
|
30
|
+
reason: string;
|
|
31
|
+
};
|
|
32
|
+
/** An outbound request or job was refused because the VPN gate is closed. Never rerouted. */
|
|
33
|
+
export declare class WaitingForVpnError extends EgressNotReadyError {
|
|
34
|
+
readonly status: GateStatus;
|
|
35
|
+
constructor(egressId: string, egressName: string, host: string, status: GateStatus);
|
|
36
|
+
}
|
|
37
|
+
type Listener = () => void;
|
|
38
|
+
/**
|
|
39
|
+
* "Nothing starts without the VPN". When the host requires an exit, this is closed from boot
|
|
40
|
+
* until the default exit is up, tested and verified by the leak check, and closes again the
|
|
41
|
+
* moment any of that stops being true. The VPN service evaluates it; everything that would go
|
|
42
|
+
* out asks it.
|
|
43
|
+
*/
|
|
44
|
+
export declare class EgressGate {
|
|
45
|
+
private readonly logger;
|
|
46
|
+
private open;
|
|
47
|
+
private reason;
|
|
48
|
+
private since;
|
|
49
|
+
private exits;
|
|
50
|
+
private decided;
|
|
51
|
+
private readonly onCloseListeners;
|
|
52
|
+
private readonly onOpenListeners;
|
|
53
|
+
readonly required: boolean;
|
|
54
|
+
constructor(host?: HostInfo);
|
|
55
|
+
/** True when work may go out: the host does not require an exit, or the gate has opened. */
|
|
56
|
+
isOpen(): boolean;
|
|
57
|
+
status(): GateStatus;
|
|
58
|
+
/** Throws the typed refusal while closed. `what` names the work in the message. */
|
|
59
|
+
assertOpen(what: string): void;
|
|
60
|
+
onClose(listener: Listener): () => void;
|
|
61
|
+
onOpen(listener: Listener): () => void;
|
|
62
|
+
/** The VPN service reports what it sees. Every transition is logged with its reason. */
|
|
63
|
+
evaluate(result: GateEvaluation, exits: GateExitStatus[]): void;
|
|
64
|
+
private fire;
|
|
65
|
+
}
|
|
66
|
+
export {};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var EgressGate_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.EgressGate = exports.WaitingForVpnError = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const tokens_1 = require("../../ports/tokens");
|
|
19
|
+
const logger_1 = require("../../ports/logger");
|
|
20
|
+
const egress_router_1 = require("./egress-router");
|
|
21
|
+
/** An outbound request or job was refused because the VPN gate is closed. Never rerouted. */
|
|
22
|
+
class WaitingForVpnError extends egress_router_1.EgressNotReadyError {
|
|
23
|
+
constructor(egressId, egressName, host, status) {
|
|
24
|
+
super(egressId, egressName, host, status.reason ?? 'waiting for the VPN');
|
|
25
|
+
this.status = status;
|
|
26
|
+
this.name = 'WaitingForVpnError';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.WaitingForVpnError = WaitingForVpnError;
|
|
30
|
+
/**
|
|
31
|
+
* "Nothing starts without the VPN". When the host requires an exit, this is closed from boot
|
|
32
|
+
* until the default exit is up, tested and verified by the leak check, and closes again the
|
|
33
|
+
* moment any of that stops being true. The VPN service evaluates it; everything that would go
|
|
34
|
+
* out asks it.
|
|
35
|
+
*/
|
|
36
|
+
let EgressGate = EgressGate_1 = class EgressGate {
|
|
37
|
+
constructor(host) {
|
|
38
|
+
this.logger = new logger_1.ScopedLogger(EgressGate_1.name);
|
|
39
|
+
this.open = false;
|
|
40
|
+
this.reason = 'waiting for the VPN checks to run';
|
|
41
|
+
this.since = new Date().toISOString();
|
|
42
|
+
this.exits = [];
|
|
43
|
+
this.decided = false;
|
|
44
|
+
this.onCloseListeners = new Set();
|
|
45
|
+
this.onOpenListeners = new Set();
|
|
46
|
+
this.required = host?.requireExit === true;
|
|
47
|
+
}
|
|
48
|
+
/** True when work may go out: the host does not require an exit, or the gate has opened. */
|
|
49
|
+
isOpen() {
|
|
50
|
+
return !this.required || this.open;
|
|
51
|
+
}
|
|
52
|
+
status() {
|
|
53
|
+
return {
|
|
54
|
+
required: this.required,
|
|
55
|
+
open: this.isOpen(),
|
|
56
|
+
...(this.isOpen() ? {} : { reason: this.reason }),
|
|
57
|
+
since: this.since,
|
|
58
|
+
exits: this.exits,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** Throws the typed refusal while closed. `what` names the work in the message. */
|
|
62
|
+
assertOpen(what) {
|
|
63
|
+
if (this.isOpen())
|
|
64
|
+
return;
|
|
65
|
+
throw new WaitingForVpnError('', 'VPN gate', what, this.status());
|
|
66
|
+
}
|
|
67
|
+
onClose(listener) {
|
|
68
|
+
this.onCloseListeners.add(listener);
|
|
69
|
+
return () => this.onCloseListeners.delete(listener);
|
|
70
|
+
}
|
|
71
|
+
onOpen(listener) {
|
|
72
|
+
this.onOpenListeners.add(listener);
|
|
73
|
+
return () => this.onOpenListeners.delete(listener);
|
|
74
|
+
}
|
|
75
|
+
/** The VPN service reports what it sees. Every transition is logged with its reason. */
|
|
76
|
+
evaluate(result, exits) {
|
|
77
|
+
if (!this.required)
|
|
78
|
+
return;
|
|
79
|
+
this.exits = exits;
|
|
80
|
+
const wasOpen = this.open;
|
|
81
|
+
const reason = result.open ? '' : result.reason;
|
|
82
|
+
const changed = !this.decided || wasOpen !== result.open || (!result.open && reason !== this.reason);
|
|
83
|
+
this.open = result.open;
|
|
84
|
+
if (!result.open)
|
|
85
|
+
this.reason = reason;
|
|
86
|
+
if (!changed)
|
|
87
|
+
return;
|
|
88
|
+
if (wasOpen !== result.open || !this.decided)
|
|
89
|
+
this.since = new Date().toISOString();
|
|
90
|
+
this.decided = true;
|
|
91
|
+
if (result.open) {
|
|
92
|
+
this.logger.log('VPN gate open: the default exit is up, tested and verified by the leak check');
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
this.logger.warn(`VPN gate closed: ${reason}`);
|
|
96
|
+
}
|
|
97
|
+
if (wasOpen && !result.open)
|
|
98
|
+
this.fire(this.onCloseListeners);
|
|
99
|
+
if (!wasOpen && result.open)
|
|
100
|
+
this.fire(this.onOpenListeners);
|
|
101
|
+
}
|
|
102
|
+
fire(listeners) {
|
|
103
|
+
for (const listener of listeners) {
|
|
104
|
+
try {
|
|
105
|
+
listener();
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
this.logger.error(`VPN gate listener failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
exports.EgressGate = EgressGate;
|
|
114
|
+
exports.EgressGate = EgressGate = EgressGate_1 = __decorate([
|
|
115
|
+
(0, common_1.Injectable)(),
|
|
116
|
+
__param(0, (0, common_1.Optional)()),
|
|
117
|
+
__param(0, (0, common_1.Inject)(tokens_1.HOST_INFO)),
|
|
118
|
+
__metadata("design:paramtypes", [Object])
|
|
119
|
+
], EgressGate);
|
|
@@ -13,6 +13,8 @@ export interface RouterDeps {
|
|
|
13
13
|
blocks: BlockRegistry;
|
|
14
14
|
logger: RouterLogger;
|
|
15
15
|
isLocal?: (host: string) => boolean;
|
|
16
|
+
/** A refusal for every non-local host while the VPN gate is closed; undefined when open. */
|
|
17
|
+
waiting?: (host: string) => EgressNotReadyError | undefined;
|
|
16
18
|
}
|
|
17
19
|
/** Why an exit was chosen. `container` means no exit: the container's own network. */
|
|
18
20
|
export type ExitSource = 'override' | 'rule' | 'default' | 'container';
|
|
@@ -48,6 +48,11 @@ class EgressRouter {
|
|
|
48
48
|
const isLocal = this.deps.isLocal ?? host_rules_1.isLocalHost;
|
|
49
49
|
if (isLocal(host))
|
|
50
50
|
return { kind: 'direct' };
|
|
51
|
+
const waiting = this.deps.waiting?.(host);
|
|
52
|
+
if (waiting) {
|
|
53
|
+
this.deps.logger.debug(`Refusing ${host}: waiting for the VPN`);
|
|
54
|
+
throw waiting;
|
|
55
|
+
}
|
|
51
56
|
// A watched entry's override comes first and stands alone: no rules, no fallback, no direct.
|
|
52
57
|
const override = egress_context_1.egressContext.current()?.egressId;
|
|
53
58
|
if (override)
|
|
@@ -2,6 +2,7 @@ import type { VpnSettings } from '../../types';
|
|
|
2
2
|
/**
|
|
3
3
|
* With the firewall on, strict mode is not a preference: a request with no
|
|
4
4
|
* exit must be refused, never sent from the host's own address. Returns
|
|
5
|
-
* a copy; the stored setting is untouched.
|
|
5
|
+
* a copy; the stored setting is untouched. A host that requires an exit forces it the same way,
|
|
6
|
+
* without claiming a firewall.
|
|
6
7
|
*/
|
|
7
|
-
export declare function applyFirewallPolicy(settings: VpnSettings, firewallEnabled: boolean): VpnSettings;
|
|
8
|
+
export declare function applyFirewallPolicy(settings: VpnSettings, firewallEnabled: boolean, requireExit?: boolean): VpnSettings;
|
|
@@ -4,10 +4,11 @@ exports.applyFirewallPolicy = applyFirewallPolicy;
|
|
|
4
4
|
/**
|
|
5
5
|
* With the firewall on, strict mode is not a preference: a request with no
|
|
6
6
|
* exit must be refused, never sent from the host's own address. Returns
|
|
7
|
-
* a copy; the stored setting is untouched.
|
|
7
|
+
* a copy; the stored setting is untouched. A host that requires an exit forces it the same way,
|
|
8
|
+
* without claiming a firewall.
|
|
8
9
|
*/
|
|
9
|
-
function applyFirewallPolicy(settings, firewallEnabled) {
|
|
10
|
-
if (!firewallEnabled || settings.strictMode)
|
|
10
|
+
function applyFirewallPolicy(settings, firewallEnabled, requireExit = false) {
|
|
11
|
+
if (!(firewallEnabled || requireExit) || settings.strictMode)
|
|
11
12
|
return settings;
|
|
12
13
|
return { ...settings, strictMode: true };
|
|
13
14
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startsOutboundWork = startsOutboundWork;
|
|
4
|
+
const route = (method, path) => ({ method, path });
|
|
5
|
+
const OUTBOUND_ROUTES = [
|
|
6
|
+
route('GET', /^\/api\/books\/(metadata\/.*|search|links)$/),
|
|
7
|
+
route('POST', /^\/api\/captchas\/[^/]+\/(solve|refresh|fallback)$/),
|
|
8
|
+
route('GET', /^\/api\/catalog\/lookup$/),
|
|
9
|
+
route('POST', /^\/api\/catalog\/ensure$/),
|
|
10
|
+
route('GET', /^\/api\/updates$/),
|
|
11
|
+
route('PUT', /^\/api\/wakeup$/),
|
|
12
|
+
route('POST', /^\/api\/core\/(start|restart)$/),
|
|
13
|
+
route('POST', /^\/api\/downloads\/(direct|resolve)$/),
|
|
14
|
+
route('POST', /^\/api\/downloads\/[^/]+\/resume$/),
|
|
15
|
+
route('POST', /^\/api\/notifications\/webhooks\/[^/]+\/test$/),
|
|
16
|
+
route('GET', /^\/api\/plex\/sections$/),
|
|
17
|
+
route('POST', /^\/api\/plex\/test$/),
|
|
18
|
+
route('GET', /^\/api\/(detect|search|seasons|episodes|links)$/),
|
|
19
|
+
route('GET', /^\/api\/(sonarr|radarr)\/.*$/),
|
|
20
|
+
route('POST', /^\/api\/(sonarr|radarr)\/.*$/),
|
|
21
|
+
route('POST', /^\/api\/sites\/analyze(\/search)?$/),
|
|
22
|
+
route('POST', /^\/api\/table\/[^/]+\/(download|restart)$/),
|
|
23
|
+
route('POST', /^\/api\/table\/[^/]+\/download\/[^/]+$/),
|
|
24
|
+
route('POST', /^\/api\/table\/[^/]+\/episode\/(download|external-download)$/),
|
|
25
|
+
route('GET', /^\/api\/table\/[^/]+\/(details|episode\/links)$/),
|
|
26
|
+
route('POST', /^\/api\/tags\/sync$/),
|
|
27
|
+
];
|
|
28
|
+
/** `path` is the request path without the query string. */
|
|
29
|
+
function startsOutboundWork(method, path) {
|
|
30
|
+
const m = method.toUpperCase();
|
|
31
|
+
const clean = path.length > 1 && path.endsWith('/') ? path.slice(0, -1) : path;
|
|
32
|
+
return OUTBOUND_ROUTES.some((r) => r.method === m && r.path.test(clean));
|
|
33
|
+
}
|
|
@@ -4,6 +4,7 @@ exports.RoutingDispatcher = exports.EgressNotReadyError = void 0;
|
|
|
4
4
|
const undici_1 = require("undici");
|
|
5
5
|
const block_classification_1 = require("./block-classification");
|
|
6
6
|
const rejecting_dispatcher_1 = require("./rejecting-dispatcher");
|
|
7
|
+
const direct_allowance_1 = require("./direct-allowance");
|
|
7
8
|
const egress_router_1 = require("./egress-router");
|
|
8
9
|
Object.defineProperty(exports, "EgressNotReadyError", { enumerable: true, get: function () { return egress_router_1.EgressNotReadyError; } });
|
|
9
10
|
/** A challenge page is tiny; past this the body is judged on its first bytes. */
|
|
@@ -186,6 +187,12 @@ class RoutingDispatcher extends undici_1.Dispatcher {
|
|
|
186
187
|
selection = this.router.select(host);
|
|
187
188
|
}
|
|
188
189
|
catch (err) {
|
|
190
|
+
const allowance = (0, direct_allowance_1.directAllowanceFor)(host);
|
|
191
|
+
if (allowance) {
|
|
192
|
+
// A pinned download the exits depend on, while no exit can serve it: direct, named, logged.
|
|
193
|
+
this.logger.log(`Direct download allowed while no exit is ready: ${host} (${allowance.reason})`);
|
|
194
|
+
return this.direct.dispatch(opts, handler);
|
|
195
|
+
}
|
|
189
196
|
return (0, rejecting_dispatcher_1.failDispatch)(handler, err instanceof Error ? err : new Error(String(err)));
|
|
190
197
|
}
|
|
191
198
|
if (selection.kind === 'direct')
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CanActivate, ExecutionContext } from '@nestjs/common';
|
|
2
|
+
import { EgressGate } from './egress-gate';
|
|
3
|
+
/** While the VPN gate is closed, a route that would go out answers 503 `waiting-for-vpn`. */
|
|
4
|
+
export declare class VpnGateGuard implements CanActivate {
|
|
5
|
+
private readonly gate;
|
|
6
|
+
constructor(gate: EgressGate);
|
|
7
|
+
canActivate(context: ExecutionContext): boolean;
|
|
8
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.VpnGateGuard = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const egress_gate_1 = require("./egress-gate");
|
|
15
|
+
const outbound_routes_1 = require("./outbound-routes");
|
|
16
|
+
/** While the VPN gate is closed, a route that would go out answers 503 `waiting-for-vpn`. */
|
|
17
|
+
let VpnGateGuard = class VpnGateGuard {
|
|
18
|
+
constructor(gate) {
|
|
19
|
+
this.gate = gate;
|
|
20
|
+
}
|
|
21
|
+
canActivate(context) {
|
|
22
|
+
if (this.gate.isOpen())
|
|
23
|
+
return true;
|
|
24
|
+
const req = context.switchToHttp().getRequest();
|
|
25
|
+
const path = req.path ?? req.url.split('?')[0];
|
|
26
|
+
if (!(0, outbound_routes_1.startsOutboundWork)(req.method, path))
|
|
27
|
+
return true;
|
|
28
|
+
throw new common_1.HttpException({ error: 'waiting-for-vpn', status: this.gate.status() }, common_1.HttpStatus.SERVICE_UNAVAILABLE);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
exports.VpnGateGuard = VpnGateGuard;
|
|
32
|
+
exports.VpnGateGuard = VpnGateGuard = __decorate([
|
|
33
|
+
(0, common_1.Injectable)(),
|
|
34
|
+
__metadata("design:paramtypes", [egress_gate_1.EgressGate])
|
|
35
|
+
], VpnGateGuard);
|
|
@@ -2,6 +2,13 @@ import type { Paths } from '../ports/paths';
|
|
|
2
2
|
import { ScraperAdapter, VpnPreset, BookAdapter, PluginSettingField, PluginSettingValues } from '../types';
|
|
3
3
|
import { PluginSettingsRepository } from '../repositories/plugin-settings.repository';
|
|
4
4
|
import { HeadlessBrowserService } from './headless-browser.service';
|
|
5
|
+
/** The resolver a plugin falls back to for anything that is not shared. */
|
|
6
|
+
export interface PluginFallbackRequire {
|
|
7
|
+
(id: string): unknown;
|
|
8
|
+
resolve(id: string, options?: {
|
|
9
|
+
paths?: string[];
|
|
10
|
+
}): string;
|
|
11
|
+
}
|
|
5
12
|
export type PluginKind = 'scraper' | 'vpn-preset' | 'book-adapter';
|
|
6
13
|
export interface PluginMeta {
|
|
7
14
|
name: string;
|
|
@@ -33,7 +40,7 @@ export interface PluginLoadResult {
|
|
|
33
40
|
* anything else (including no kind field) is treated as a scraper adapter.
|
|
34
41
|
* - `.json` — GenericSiteConfig (scraper adapter only).
|
|
35
42
|
*
|
|
36
|
-
*
|
|
43
|
+
* A plugin file is re-read on each loadAll(), enabling live reload after upload.
|
|
37
44
|
*/
|
|
38
45
|
export declare class PluginLoaderService {
|
|
39
46
|
private readonly headless;
|
|
@@ -49,6 +56,10 @@ export declare class PluginLoaderService {
|
|
|
49
56
|
/** Records a module's `settings` fields and applies stored values through its `configure`. */
|
|
50
57
|
private attachSettings;
|
|
51
58
|
private loadJson;
|
|
59
|
+
/** What a plugin's `require` falls back to. A seam so a spec does not depend on the environment's node_modules. */
|
|
60
|
+
protected fallbackRequire(filePath: string): PluginFallbackRequire;
|
|
61
|
+
/** Run a plugin file as a CommonJS module whose `require` serves the shared modules first. Never cached: a reload re-reads it. */
|
|
62
|
+
private evaluate;
|
|
52
63
|
private loadJs;
|
|
53
64
|
private isValidAdapter;
|
|
54
65
|
private isValidBookAdapter;
|
|
@@ -18,12 +18,38 @@ const common_1 = require("@nestjs/common");
|
|
|
18
18
|
const logger_1 = require("../ports/logger");
|
|
19
19
|
const tokens_1 = require("../ports/tokens");
|
|
20
20
|
const fs_1 = require("fs");
|
|
21
|
+
const module_1 = require("module");
|
|
21
22
|
const path_1 = require("path");
|
|
22
23
|
const plugin_settings_repository_1 = require("../repositories/plugin-settings.repository");
|
|
23
24
|
const plugin_settings_1 = require("./plugin-settings");
|
|
24
25
|
const generic_adapter_1 = require("../adapters/generic.adapter");
|
|
25
26
|
const headless_browser_service_1 = require("./headless-browser.service");
|
|
26
27
|
const plugin_network_guard_1 = require("./plugin-network-guard");
|
|
28
|
+
/**
|
|
29
|
+
* What a plugin may `require` from the host, by name. A plugin file lives in the data directory, and
|
|
30
|
+
* Node resolves its `require` from there: in Docker that found the app's `node_modules`, in a host
|
|
31
|
+
* that bundles its dependencies it finds nothing. These are served from the host itself, so the
|
|
32
|
+
* plugin gets the same module wherever the host runs. Everything else resolves normally (Node's own
|
|
33
|
+
* modules) and has already passed the network guard.
|
|
34
|
+
*
|
|
35
|
+
* Only modules that cannot open a connection belong here, and a module that CAN gets a facade without
|
|
36
|
+
* that export: `cheerio.fromURL` builds its own undici client, which bypasses the global dispatcher
|
|
37
|
+
* and so the VPN, so a plugin sees cheerio without it. The other cheerio exports work on memory only.
|
|
38
|
+
*/
|
|
39
|
+
const CHEERIO_NETWORK_EXPORTS = new Set(['fromURL']);
|
|
40
|
+
function cheerioFacade() {
|
|
41
|
+
const real = require('cheerio');
|
|
42
|
+
const facade = {};
|
|
43
|
+
for (const key of Object.keys(real)) {
|
|
44
|
+
if (!CHEERIO_NETWORK_EXPORTS.has(key))
|
|
45
|
+
facade[key] = real[key];
|
|
46
|
+
}
|
|
47
|
+
return Object.freeze(facade);
|
|
48
|
+
}
|
|
49
|
+
const SHARED_MODULES = {
|
|
50
|
+
cheerio: cheerioFacade,
|
|
51
|
+
uuid: () => require('uuid'),
|
|
52
|
+
};
|
|
27
53
|
/**
|
|
28
54
|
* Loads plugins at runtime from `data/plugins/`. Plugins can be either
|
|
29
55
|
* scraper adapters (default) or VPN provider presets.
|
|
@@ -34,7 +60,7 @@ const plugin_network_guard_1 = require("./plugin-network-guard");
|
|
|
34
60
|
* anything else (including no kind field) is treated as a scraper adapter.
|
|
35
61
|
* - `.json` — GenericSiteConfig (scraper adapter only).
|
|
36
62
|
*
|
|
37
|
-
*
|
|
63
|
+
* A plugin file is re-read on each loadAll(), enabling live reload after upload.
|
|
38
64
|
*/
|
|
39
65
|
let PluginLoaderService = PluginLoaderService_1 = class PluginLoaderService {
|
|
40
66
|
constructor(headless, paths, pluginSettings) {
|
|
@@ -150,9 +176,24 @@ let PluginLoaderService = PluginLoaderService_1 = class PluginLoaderService {
|
|
|
150
176
|
: undefined;
|
|
151
177
|
return new generic_adapter_1.GenericAdapter(config, fetchPageHtml);
|
|
152
178
|
}
|
|
179
|
+
/** What a plugin's `require` falls back to. A seam so a spec does not depend on the environment's node_modules. */
|
|
180
|
+
fallbackRequire(filePath) {
|
|
181
|
+
return (0, module_1.createRequire)(filePath);
|
|
182
|
+
}
|
|
183
|
+
/** Run a plugin file as a CommonJS module whose `require` serves the shared modules first. Never cached: a reload re-reads it. */
|
|
184
|
+
evaluate(filePath) {
|
|
185
|
+
const fallback = this.fallbackRequire(filePath);
|
|
186
|
+
const pluginRequire = Object.assign((id) => {
|
|
187
|
+
const shared = Object.prototype.hasOwnProperty.call(SHARED_MODULES, id) ? SHARED_MODULES[id] : undefined;
|
|
188
|
+
return shared ? shared() : fallback(id);
|
|
189
|
+
}, { resolve: (id, options) => fallback.resolve(id, options) });
|
|
190
|
+
const module = { exports: {} };
|
|
191
|
+
const run = new Function('exports', 'require', 'module', '__filename', '__dirname', (0, fs_1.readFileSync)(filePath, 'utf-8'));
|
|
192
|
+
run(module.exports, pluginRequire, module, filePath, (0, path_1.dirname)(filePath));
|
|
193
|
+
return module.exports;
|
|
194
|
+
}
|
|
153
195
|
loadJs(filePath) {
|
|
154
|
-
|
|
155
|
-
const mod = require(filePath);
|
|
196
|
+
const mod = this.evaluate(filePath);
|
|
156
197
|
if (typeof mod === 'function') {
|
|
157
198
|
try {
|
|
158
199
|
return new mod();
|
|
@@ -7,8 +7,10 @@ exports.findForbiddenModule = findForbiddenModule;
|
|
|
7
7
|
* leave from the container's own address, so it is refused at load.
|
|
8
8
|
*
|
|
9
9
|
* This reads the plugin's source for module references. It cannot see a
|
|
10
|
-
* module name assembled at runtime
|
|
11
|
-
*
|
|
10
|
+
* module name assembled at runtime, nor `process.binding`. Where the host runs
|
|
11
|
+
* an egress firewall (the Docker deployment with EGRESS_FIREWALL=1) that is the
|
|
12
|
+
* backstop. Where it does not (the embedded host has no firewall, its VPN gate is
|
|
13
|
+
* an application-level guarantee), such a plugin would leave directly.
|
|
12
14
|
*/
|
|
13
15
|
const FORBIDDEN_MODULES = [
|
|
14
16
|
'http', 'https', 'http2', 'net', 'tls', 'dgram', 'child_process',
|
|
@@ -7,6 +7,7 @@ import { VpnProvidersRepository } from '../repositories/vpn-providers.repository
|
|
|
7
7
|
import { VpnSettingsRepository } from '../repositories/vpn-settings.repository';
|
|
8
8
|
import { ActiveBlock, BlockSelector } from './egress/block-registry';
|
|
9
9
|
import { ActiveEgress } from './download-tracker.service';
|
|
10
|
+
import { EgressGate } from './egress/egress-gate';
|
|
10
11
|
import { SupervisorState, WireproxySupervisor } from './egress/wireproxy-supervisor';
|
|
11
12
|
export interface VpnTestResult {
|
|
12
13
|
ok: boolean;
|
|
@@ -37,6 +38,8 @@ export interface PuppeteerProxy {
|
|
|
37
38
|
/** The exit that serves this page, to record on the links it produces. */
|
|
38
39
|
egressId: string;
|
|
39
40
|
}
|
|
41
|
+
/** An error with its `cause` chain ("fetch failed" alone says nothing); the logger scrubs it. */
|
|
42
|
+
export declare function errorWithCause(err: unknown): string;
|
|
40
43
|
/** Where a leak-guard lookup should leave from. */
|
|
41
44
|
type LookupRoute = {
|
|
42
45
|
kind: 'direct';
|
|
@@ -65,9 +68,13 @@ export declare class VpnService implements OnApplicationBootstrap, OnModuleDestr
|
|
|
65
68
|
private tunnels;
|
|
66
69
|
private readonly leakFlagged;
|
|
67
70
|
private leakTimer;
|
|
71
|
+
private gateRetryTimer;
|
|
72
|
+
/** The last leak-check answer per exit. Absent means unknown: nothing has compared its IP yet. */
|
|
73
|
+
private readonly verdicts;
|
|
74
|
+
readonly gate: EgressGate;
|
|
68
75
|
private leakGuardActive;
|
|
69
76
|
private leakGuardRunning;
|
|
70
|
-
constructor(providersRepo: VpnProvidersRepository, settingsRepo: VpnSettingsRepository, host: HostInfo, binaries: Binaries);
|
|
77
|
+
constructor(providersRepo: VpnProvidersRepository, settingsRepo: VpnSettingsRepository, host: HostInfo, binaries: Binaries, gate?: EgressGate);
|
|
71
78
|
onApplicationBootstrap(): Promise<void>;
|
|
72
79
|
/** With the egress firewall on, say so once, and shout when nothing can leave at all. */
|
|
73
80
|
private logFirewallPosture;
|
|
@@ -137,6 +144,17 @@ export declare class VpnService implements OnApplicationBootstrap, OnModuleDestr
|
|
|
137
144
|
private rebuildExit;
|
|
138
145
|
private pruneTunnels;
|
|
139
146
|
private startLeakGuard;
|
|
147
|
+
/**
|
|
148
|
+
* Fetch a JSON document from a VPN provider's own API (the lookup that resolves a server for an exit).
|
|
149
|
+
* On a host that requires an exit this is the one outbound call allowed while the gate is closed: an
|
|
150
|
+
* operator with no exit yet could never create the one that opens it. It goes on the direct network,
|
|
151
|
+
* only to a provider API host, https only, bounded in time and size, and every call is logged.
|
|
152
|
+
*/
|
|
153
|
+
fetchProviderJson(url: string): Promise<unknown>;
|
|
154
|
+
/** How many bytes a provider lookup may read. Overridable for specs. */
|
|
155
|
+
protected providerMaxBytes(): number;
|
|
156
|
+
/** The direct network, as the leak probe uses it. Overridable for specs. */
|
|
157
|
+
protected providerDispatcher(): Dispatcher;
|
|
140
158
|
/** The IP the world sees for a route. Overridable; undefined when the lookup failed. */
|
|
141
159
|
protected lookupEgressIp(route: LookupRoute): Promise<string | undefined>;
|
|
142
160
|
/**
|
|
@@ -145,6 +163,18 @@ export declare class VpnService implements OnApplicationBootstrap, OnModuleDestr
|
|
|
145
163
|
* mode then refuses its traffic) until a later check differs.
|
|
146
164
|
*/
|
|
147
165
|
protected runLeakGuard(): Promise<void>;
|
|
148
|
-
private
|
|
166
|
+
private verdictOf;
|
|
167
|
+
/**
|
|
168
|
+
* An exit as a request may use it. On a host that requires an exit, a VPN exit serves only
|
|
169
|
+
* after the leak check has compared its IP with the direct one and found them different.
|
|
170
|
+
*/
|
|
171
|
+
private effective;
|
|
172
|
+
/** The refusal for a non-local host while the gate is closed. */
|
|
173
|
+
private waitingFor;
|
|
174
|
+
/** The default exit's tunnel is up, its connection test passed and the leak check says `ok`. */
|
|
175
|
+
private gateEvaluation;
|
|
176
|
+
private gateExits;
|
|
177
|
+
private updateGate;
|
|
178
|
+
protected runTest(dispatcher: Dispatcher): Promise<VpnTestResult>;
|
|
149
179
|
}
|
|
150
180
|
export {};
|