@celljs/ta-gateway 4.17.0 → 4.19.0
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/esm/node/gateway-cli.js +1 -1
- package/esm/node/gateway-files.d.ts +6 -0
- package/esm/node/gateway-files.js +33 -0
- package/esm/node/gateway-server.d.ts +12 -2
- package/esm/node/gateway-server.js +34 -25
- package/lib/node/gateway-cli.js +1 -1
- package/lib/node/gateway-files.d.ts +7 -0
- package/lib/node/gateway-files.js +36 -0
- package/lib/node/gateway-server.d.ts +12 -2
- package/lib/node/gateway-server.js +33 -24
- package/package.json +5 -4
package/esm/node/gateway-cli.js
CHANGED
|
@@ -79,7 +79,7 @@ export var GatewayCli;
|
|
|
79
79
|
return;
|
|
80
80
|
}
|
|
81
81
|
const url = await gateway.listen();
|
|
82
|
-
const ways = gateway.flow.providers.map(provider => provider.name);
|
|
82
|
+
const ways = [...gateway.flow.providers, ...gateway.flow.polled].map(provider => provider.name);
|
|
83
83
|
console.log(`ta gateway: ${url} (${dir})`);
|
|
84
84
|
console.log(`ta gateway: sandboxes of ${gateway.environments.providers.map(provider => `${provider.id} (${provider.type})`).join(', ') || 'none'}${ways.length ? `; sign-in with ${ways.join(', ')}` : '; no provider of accounts: sign people in with ta gateway link <account>'}`);
|
|
85
85
|
const stop = () => void gateway.close().then(() => process.exit(0));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type IncomingHttpHeaders, type IncomingMessage, type ServerResponse } from 'node:http';
|
|
2
|
+
/** `/api/files` of the Ta of a tenant, as the gateway passes a read on to it. */
|
|
3
|
+
export declare namespace GatewayFiles {
|
|
4
|
+
/** Reads `target` for `request`, its answer to `response` with the headers `answered` lets through. */
|
|
5
|
+
function forward(request: IncomingMessage, response: ServerResponse, target: URL, answered: (headers: IncomingHttpHeaders) => IncomingHttpHeaders): Promise<void>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { request as httpRequest } from 'node:http';
|
|
2
|
+
import { request as httpsRequest } from 'node:https';
|
|
3
|
+
import { pipeline } from 'node:stream';
|
|
4
|
+
/** `/api/files` of the Ta of a tenant, as the gateway passes a read on to it. */
|
|
5
|
+
export var GatewayFiles;
|
|
6
|
+
(function (GatewayFiles) {
|
|
7
|
+
/** What of a read the Ta of the tenant is told: its range and what the browser takes, nothing of who asked. */
|
|
8
|
+
const PASSED = ['range', 'if-none-match', 'accept', 'accept-encoding', 'user-agent'];
|
|
9
|
+
/** Reads `target` for `request`, its answer to `response` with the headers `answered` lets through. */
|
|
10
|
+
function forward(request, response, target, answered) {
|
|
11
|
+
const headers = {};
|
|
12
|
+
for (const name of PASSED) {
|
|
13
|
+
const value = request.headers[name];
|
|
14
|
+
if (value !== undefined) {
|
|
15
|
+
headers[name] = value;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const send = target.protocol === 'https:' ? httpsRequest : httpRequest;
|
|
20
|
+
const outgoing = send(target, { method: request.method, headers }, incoming => {
|
|
21
|
+
var _a;
|
|
22
|
+
response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, answered(incoming.headers));
|
|
23
|
+
// A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
|
|
24
|
+
pipeline(incoming, response, () => resolve());
|
|
25
|
+
});
|
|
26
|
+
outgoing.on('error', reject);
|
|
27
|
+
response.once('close', () => outgoing.destroy());
|
|
28
|
+
outgoing.end();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
GatewayFiles.forward = forward;
|
|
32
|
+
})(GatewayFiles || (GatewayFiles = {}));
|
|
33
|
+
//# sourceMappingURL=gateway-files.js.map
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { type IncomingMessage, type Server, type ServerResponse } from 'node:http';
|
|
2
|
-
import {
|
|
2
|
+
import type { Duplex } from 'node:stream';
|
|
3
3
|
import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
|
|
4
4
|
import { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
|
|
5
5
|
import { type SignInAccount } from '@celljs/ta-host/lib/node/sign-in/sign-in-provider';
|
|
6
6
|
import { type TaQuotaBlock } from '@celljs/ta-protocol';
|
|
7
|
+
import { RelayServerAccounts } from '@celljs/ta-relay/lib/node';
|
|
7
8
|
import { type EgressAuthorization, EgressProxy } from './egress-proxy.js';
|
|
8
9
|
import { Environments } from './environments.js';
|
|
9
10
|
import { GatewayAudit } from './gateway-audit.js';
|
|
@@ -37,6 +38,11 @@ export interface GatewayServerOptions {
|
|
|
37
38
|
version?: string;
|
|
38
39
|
}
|
|
39
40
|
export declare namespace GatewayServerUtil {
|
|
41
|
+
/** The account of a caller, for the account service (docs/design/ta-account.md 3). */
|
|
42
|
+
function accountOf(caller: Pick<GatewayCaller, 'account' | 'tenant'> | undefined): {
|
|
43
|
+
id: string;
|
|
44
|
+
name: string;
|
|
45
|
+
} | undefined;
|
|
40
46
|
/**
|
|
41
47
|
* Where a sandbox reaches the gateway, by the type of its provider: a container the host (host.docker.internal,
|
|
42
48
|
* mapped on Linux too), a process of this machine 127.0.0.1, another the public address.
|
|
@@ -76,6 +82,7 @@ export declare class GatewayServer {
|
|
|
76
82
|
protected readonly options: GatewayServerOptions;
|
|
77
83
|
readonly flow: SignInFlow;
|
|
78
84
|
readonly tenants: TenantStore;
|
|
85
|
+
protected readonly accounts: RelayServerAccounts;
|
|
79
86
|
readonly environments: Environments;
|
|
80
87
|
readonly links: SignInLinks;
|
|
81
88
|
protected readonly service: GatewayService;
|
|
@@ -127,7 +134,10 @@ export declare class GatewayServer {
|
|
|
127
134
|
protected signedIn(response: ServerResponse, token: string, account: SignInAccount, origin: string): void;
|
|
128
135
|
/** As the pages of signing in read it: behind a proxy of TLS, its X-Forwarded-Proto makes the cookie Secure. */
|
|
129
136
|
protected origin(request: IncomingMessage): string;
|
|
130
|
-
/**
|
|
137
|
+
/**
|
|
138
|
+
* `/api/files` of the Ta of the tenant whose cookie came along, or whose token: the app on a phone and Ta Desktop, of
|
|
139
|
+
* another origin, read them with it (TaFiles), and are answered as any origin is, the token being what lets them in.
|
|
140
|
+
*/
|
|
131
141
|
protected files(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
132
142
|
protected page(request: IncomingMessage, response: ServerResponse, path: string): void;
|
|
133
143
|
protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
import { accessSync, constants, createReadStream, existsSync, mkdirSync, statSync } from 'node:fs';
|
|
2
|
-
import { createServer
|
|
3
|
-
import { request as httpsRequest } from 'node:https';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
4
3
|
import { extname, join, normalize, sep } from 'node:path';
|
|
5
|
-
import { pipeline } from 'node:stream';
|
|
6
4
|
import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
|
|
7
5
|
import { AccessStore, AccessUtil } from '@celljs/ta-host/lib/node/sign-in/access-store';
|
|
8
6
|
import { SignInFlow, SignInUtil } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
|
|
9
7
|
import { SignInPages, SignInPageUtil } from '@celljs/ta-host/lib/node/sign-in/sign-in-pages';
|
|
10
8
|
import { SignInProviders } from '@celljs/ta-host/lib/node/sign-in/sign-in-provider';
|
|
11
9
|
import { TA_FILES_PATH, TA_WEBSOCKET_PATH, TA_WELL_KNOWN_PATH, TaDeploymentUtil } from '@celljs/ta-protocol';
|
|
10
|
+
import { RELAY_ACCOUNT_PATH } from '@celljs/ta-relay';
|
|
11
|
+
import { RelayServerAccounts } from '@celljs/ta-relay/lib/node';
|
|
12
12
|
import { CommandSandbox } from './command-sandbox.js';
|
|
13
13
|
import { DockerSandbox } from './docker-sandbox.js';
|
|
14
14
|
import { EgressProxy, EgressProxyUtil } from './egress-proxy.js';
|
|
15
15
|
import { Environments } from './environments.js';
|
|
16
16
|
import { GatewayAudit } from './gateway-audit.js';
|
|
17
17
|
import { GatewayExecProxy } from './gateway-exec.js';
|
|
18
|
+
import { GatewayFiles } from './gateway-files.js';
|
|
18
19
|
import { GatewayPolicy } from './gateway-policy.js';
|
|
19
20
|
import { GatewayQuotas } from './gateway-quotas.js';
|
|
20
21
|
import { GatewayService } from './gateway-service.js';
|
|
@@ -55,6 +56,11 @@ export const SANDBOX_TYPES = {
|
|
|
55
56
|
};
|
|
56
57
|
export var GatewayServerUtil;
|
|
57
58
|
(function (GatewayServerUtil) {
|
|
59
|
+
/** The account of a caller, for the account service (docs/design/ta-account.md 3). */
|
|
60
|
+
function accountOf(caller) {
|
|
61
|
+
return caller ? { id: caller.account, name: caller.tenant.name || caller.account } : undefined;
|
|
62
|
+
}
|
|
63
|
+
GatewayServerUtil.accountOf = accountOf;
|
|
58
64
|
/**
|
|
59
65
|
* Where a sandbox reaches the gateway, by the type of its provider: a container the host (host.docker.internal,
|
|
60
66
|
* mapped on Linux too), a process of this machine 127.0.0.1, another the public address.
|
|
@@ -163,8 +169,10 @@ export class GatewayServer {
|
|
|
163
169
|
const owners = AccessUtil.parseList(env.TA_GATEWAY_ADMINS);
|
|
164
170
|
this.policy = new GatewayPolicy(options.dir, owners);
|
|
165
171
|
const allowed = [...AccessUtil.parseList(env.TA_ALLOWED_USERS), ...owners];
|
|
166
|
-
this.flow = new SignInFlow(SignInProviders.fromEnv(env), new AccessStore(join(options.dir, 'access.json'), allowed), SignInProviders.publicUrl(env));
|
|
172
|
+
this.flow = new SignInFlow(SignInProviders.fromEnv(env), new AccessStore(join(options.dir, 'access.json'), allowed), SignInProviders.publicUrl(env), SignInProviders.polled(env));
|
|
167
173
|
this.tenants = new TenantStore(options.dir);
|
|
174
|
+
// The devices of each account (docs/design/ta-account.md 3), its tokens those of this gateway's sign-in.
|
|
175
|
+
this.accounts = new RelayServerAccounts(options.dir, { authenticate: async (token) => GatewayServerUtil.accountOf(this.caller(token)) });
|
|
168
176
|
this.links = new SignInLinks(options.dir);
|
|
169
177
|
this.environments = new Environments(providers, this.tenants, {
|
|
170
178
|
default: config.default,
|
|
@@ -280,6 +288,7 @@ export class GatewayServer {
|
|
|
280
288
|
}
|
|
281
289
|
async close() {
|
|
282
290
|
clearInterval(this.ticker);
|
|
291
|
+
this.flow.stopPolling();
|
|
283
292
|
await this.tick().catch(() => undefined);
|
|
284
293
|
for (const socket of this.sockets) {
|
|
285
294
|
socket.close(1001, 'The gateway is stopping.');
|
|
@@ -348,6 +357,9 @@ export class GatewayServer {
|
|
|
348
357
|
onSignedIn: (res, token, account, origin) => this.signedIn(res, token, account, origin),
|
|
349
358
|
});
|
|
350
359
|
}
|
|
360
|
+
else if (path.startsWith(RELAY_ACCOUNT_PATH)) {
|
|
361
|
+
this.accounts.serve(request, response);
|
|
362
|
+
}
|
|
351
363
|
else if (path === ModelProxyUtil.PATH || path.startsWith(`${ModelProxyUtil.PATH}/`)) {
|
|
352
364
|
await this.models.handle(request, response);
|
|
353
365
|
}
|
|
@@ -424,15 +436,30 @@ export class GatewayServer {
|
|
|
424
436
|
origin(request) {
|
|
425
437
|
return SignInUtil.origin(request, this.flow.publicUrl);
|
|
426
438
|
}
|
|
427
|
-
/**
|
|
439
|
+
/**
|
|
440
|
+
* `/api/files` of the Ta of the tenant whose cookie came along, or whose token: the app on a phone and Ta Desktop, of
|
|
441
|
+
* another origin, read them with it (TaFiles), and are answered as any origin is, the token being what lets them in.
|
|
442
|
+
*/
|
|
428
443
|
async files(request, response) {
|
|
429
444
|
var _a, _b;
|
|
445
|
+
const header = request.headers.authorization;
|
|
446
|
+
const bearer = (header === null || header === void 0 ? void 0 : header.startsWith('Bearer ')) ? header.slice('Bearer '.length) : undefined;
|
|
447
|
+
if (bearer !== undefined || request.method === 'OPTIONS') {
|
|
448
|
+
response.setHeader('Access-Control-Allow-Origin', '*');
|
|
449
|
+
response.setHeader('Access-Control-Allow-Headers', 'Authorization, Range');
|
|
450
|
+
response.setHeader('Access-Control-Expose-Headers', 'Content-Type, Content-Length, Content-Range');
|
|
451
|
+
response.setHeader('Access-Control-Max-Age', '600');
|
|
452
|
+
}
|
|
453
|
+
if (request.method === 'OPTIONS') {
|
|
454
|
+
SignInPageUtil.end(response, 204);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
430
457
|
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
431
458
|
response.setHeader('Allow', 'GET, HEAD');
|
|
432
459
|
SignInPageUtil.end(response, 405);
|
|
433
460
|
return;
|
|
434
461
|
}
|
|
435
|
-
const caller = this.caller((_a = SignInPageUtil.cookie(request.headers.cookie, FILES_COOKIE)) !== null && _a !== void 0 ? _a : '');
|
|
462
|
+
const caller = this.caller((_a = bearer !== null && bearer !== void 0 ? bearer : SignInPageUtil.cookie(request.headers.cookie, FILES_COOKIE)) !== null && _a !== void 0 ? _a : '');
|
|
436
463
|
if (!caller) {
|
|
437
464
|
SignInPageUtil.end(response, 404);
|
|
438
465
|
return;
|
|
@@ -443,25 +470,7 @@ export class GatewayServer {
|
|
|
443
470
|
SignInPageUtil.end(response, 404);
|
|
444
471
|
return;
|
|
445
472
|
}
|
|
446
|
-
|
|
447
|
-
for (const name of ['range', 'if-none-match', 'accept', 'accept-encoding', 'user-agent']) {
|
|
448
|
-
const value = request.headers[name];
|
|
449
|
-
if (value !== undefined) {
|
|
450
|
-
headers[name] = value;
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
await new Promise((resolve, reject) => {
|
|
454
|
-
const send = target.protocol === 'https:' ? httpsRequest : httpRequest;
|
|
455
|
-
const outgoing = send(target, { method: request.method, headers }, incoming => {
|
|
456
|
-
var _a;
|
|
457
|
-
response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, GatewayServerUtil.filesHeaders(incoming.headers));
|
|
458
|
-
// A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
|
|
459
|
-
pipeline(incoming, response, () => resolve());
|
|
460
|
-
});
|
|
461
|
-
outgoing.on('error', reject);
|
|
462
|
-
response.once('close', () => outgoing.destroy());
|
|
463
|
-
outgoing.end();
|
|
464
|
-
});
|
|
473
|
+
await GatewayFiles.forward(request, response, target, GatewayServerUtil.filesHeaders);
|
|
465
474
|
}
|
|
466
475
|
page(request, response, path) {
|
|
467
476
|
var _a;
|
package/lib/node/gateway-cli.js
CHANGED
|
@@ -82,7 +82,7 @@ var GatewayCli;
|
|
|
82
82
|
return;
|
|
83
83
|
}
|
|
84
84
|
const url = await gateway.listen();
|
|
85
|
-
const ways = gateway.flow.providers.map(provider => provider.name);
|
|
85
|
+
const ways = [...gateway.flow.providers, ...gateway.flow.polled].map(provider => provider.name);
|
|
86
86
|
console.log(`ta gateway: ${url} (${dir})`);
|
|
87
87
|
console.log(`ta gateway: sandboxes of ${gateway.environments.providers.map(provider => `${provider.id} (${provider.type})`).join(', ') || 'none'}${ways.length ? `; sign-in with ${ways.join(', ')}` : '; no provider of accounts: sign people in with ta gateway link <account>'}`);
|
|
88
88
|
const stop = () => void gateway.close().then(() => process.exit(0));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type IncomingHttpHeaders, type IncomingMessage, type ServerResponse } from 'node:http';
|
|
2
|
+
/** `/api/files` of the Ta of a tenant, as the gateway passes a read on to it. */
|
|
3
|
+
export declare namespace GatewayFiles {
|
|
4
|
+
/** Reads `target` for `request`, its answer to `response` with the headers `answered` lets through. */
|
|
5
|
+
function forward(request: IncomingMessage, response: ServerResponse, target: URL, answered: (headers: IncomingHttpHeaders) => IncomingHttpHeaders): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=gateway-files.d.ts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GatewayFiles = void 0;
|
|
4
|
+
const node_http_1 = require("node:http");
|
|
5
|
+
const node_https_1 = require("node:https");
|
|
6
|
+
const node_stream_1 = require("node:stream");
|
|
7
|
+
/** `/api/files` of the Ta of a tenant, as the gateway passes a read on to it. */
|
|
8
|
+
var GatewayFiles;
|
|
9
|
+
(function (GatewayFiles) {
|
|
10
|
+
/** What of a read the Ta of the tenant is told: its range and what the browser takes, nothing of who asked. */
|
|
11
|
+
const PASSED = ['range', 'if-none-match', 'accept', 'accept-encoding', 'user-agent'];
|
|
12
|
+
/** Reads `target` for `request`, its answer to `response` with the headers `answered` lets through. */
|
|
13
|
+
function forward(request, response, target, answered) {
|
|
14
|
+
const headers = {};
|
|
15
|
+
for (const name of PASSED) {
|
|
16
|
+
const value = request.headers[name];
|
|
17
|
+
if (value !== undefined) {
|
|
18
|
+
headers[name] = value;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const send = target.protocol === 'https:' ? node_https_1.request : node_http_1.request;
|
|
23
|
+
const outgoing = send(target, { method: request.method, headers }, incoming => {
|
|
24
|
+
var _a;
|
|
25
|
+
response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, answered(incoming.headers));
|
|
26
|
+
// A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
|
|
27
|
+
(0, node_stream_1.pipeline)(incoming, response, () => resolve());
|
|
28
|
+
});
|
|
29
|
+
outgoing.on('error', reject);
|
|
30
|
+
response.once('close', () => outgoing.destroy());
|
|
31
|
+
outgoing.end();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
GatewayFiles.forward = forward;
|
|
35
|
+
})(GatewayFiles || (exports.GatewayFiles = GatewayFiles = {}));
|
|
36
|
+
//# sourceMappingURL=gateway-files.js.map
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { type IncomingMessage, type Server, type ServerResponse } from 'node:http';
|
|
2
|
-
import {
|
|
2
|
+
import type { Duplex } from 'node:stream';
|
|
3
3
|
import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
|
|
4
4
|
import { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
|
|
5
5
|
import { type SignInAccount } from '@celljs/ta-host/lib/node/sign-in/sign-in-provider';
|
|
6
6
|
import { type TaQuotaBlock } from '@celljs/ta-protocol';
|
|
7
|
+
import { RelayServerAccounts } from '@celljs/ta-relay/lib/node';
|
|
7
8
|
import { type EgressAuthorization, EgressProxy } from './egress-proxy';
|
|
8
9
|
import { Environments } from './environments';
|
|
9
10
|
import { GatewayAudit } from './gateway-audit';
|
|
@@ -37,6 +38,11 @@ export interface GatewayServerOptions {
|
|
|
37
38
|
version?: string;
|
|
38
39
|
}
|
|
39
40
|
export declare namespace GatewayServerUtil {
|
|
41
|
+
/** The account of a caller, for the account service (docs/design/ta-account.md 3). */
|
|
42
|
+
function accountOf(caller: Pick<GatewayCaller, 'account' | 'tenant'> | undefined): {
|
|
43
|
+
id: string;
|
|
44
|
+
name: string;
|
|
45
|
+
} | undefined;
|
|
40
46
|
/**
|
|
41
47
|
* Where a sandbox reaches the gateway, by the type of its provider: a container the host (host.docker.internal,
|
|
42
48
|
* mapped on Linux too), a process of this machine 127.0.0.1, another the public address.
|
|
@@ -76,6 +82,7 @@ export declare class GatewayServer {
|
|
|
76
82
|
protected readonly options: GatewayServerOptions;
|
|
77
83
|
readonly flow: SignInFlow;
|
|
78
84
|
readonly tenants: TenantStore;
|
|
85
|
+
protected readonly accounts: RelayServerAccounts;
|
|
79
86
|
readonly environments: Environments;
|
|
80
87
|
readonly links: SignInLinks;
|
|
81
88
|
protected readonly service: GatewayService;
|
|
@@ -127,7 +134,10 @@ export declare class GatewayServer {
|
|
|
127
134
|
protected signedIn(response: ServerResponse, token: string, account: SignInAccount, origin: string): void;
|
|
128
135
|
/** As the pages of signing in read it: behind a proxy of TLS, its X-Forwarded-Proto makes the cookie Secure. */
|
|
129
136
|
protected origin(request: IncomingMessage): string;
|
|
130
|
-
/**
|
|
137
|
+
/**
|
|
138
|
+
* `/api/files` of the Ta of the tenant whose cookie came along, or whose token: the app on a phone and Ta Desktop, of
|
|
139
|
+
* another origin, read them with it (TaFiles), and are answered as any origin is, the token being what lets them in.
|
|
140
|
+
*/
|
|
131
141
|
protected files(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
132
142
|
protected page(request: IncomingMessage, response: ServerResponse, path: string): void;
|
|
133
143
|
protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
|
|
@@ -3,21 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.GatewayServer = exports.GatewayServerUtil = exports.SANDBOX_TYPES = void 0;
|
|
4
4
|
const node_fs_1 = require("node:fs");
|
|
5
5
|
const node_http_1 = require("node:http");
|
|
6
|
-
const node_https_1 = require("node:https");
|
|
7
6
|
const node_path_1 = require("node:path");
|
|
8
|
-
const node_stream_1 = require("node:stream");
|
|
9
7
|
const websocket_connection_1 = require("@celljs/rpc/lib/node/websocket/websocket-connection");
|
|
10
8
|
const access_store_1 = require("@celljs/ta-host/lib/node/sign-in/access-store");
|
|
11
9
|
const sign_in_flow_1 = require("@celljs/ta-host/lib/node/sign-in/sign-in-flow");
|
|
12
10
|
const sign_in_pages_1 = require("@celljs/ta-host/lib/node/sign-in/sign-in-pages");
|
|
13
11
|
const sign_in_provider_1 = require("@celljs/ta-host/lib/node/sign-in/sign-in-provider");
|
|
14
12
|
const ta_protocol_1 = require("@celljs/ta-protocol");
|
|
13
|
+
const ta_relay_1 = require("@celljs/ta-relay");
|
|
14
|
+
const node_1 = require("@celljs/ta-relay/lib/node");
|
|
15
15
|
const command_sandbox_1 = require("./command-sandbox");
|
|
16
16
|
const docker_sandbox_1 = require("./docker-sandbox");
|
|
17
17
|
const egress_proxy_1 = require("./egress-proxy");
|
|
18
18
|
const environments_1 = require("./environments");
|
|
19
19
|
const gateway_audit_1 = require("./gateway-audit");
|
|
20
20
|
const gateway_exec_1 = require("./gateway-exec");
|
|
21
|
+
const gateway_files_1 = require("./gateway-files");
|
|
21
22
|
const gateway_policy_1 = require("./gateway-policy");
|
|
22
23
|
const gateway_quotas_1 = require("./gateway-quotas");
|
|
23
24
|
const gateway_service_1 = require("./gateway-service");
|
|
@@ -58,6 +59,11 @@ exports.SANDBOX_TYPES = {
|
|
|
58
59
|
};
|
|
59
60
|
var GatewayServerUtil;
|
|
60
61
|
(function (GatewayServerUtil) {
|
|
62
|
+
/** The account of a caller, for the account service (docs/design/ta-account.md 3). */
|
|
63
|
+
function accountOf(caller) {
|
|
64
|
+
return caller ? { id: caller.account, name: caller.tenant.name || caller.account } : undefined;
|
|
65
|
+
}
|
|
66
|
+
GatewayServerUtil.accountOf = accountOf;
|
|
61
67
|
/**
|
|
62
68
|
* Where a sandbox reaches the gateway, by the type of its provider: a container the host (host.docker.internal,
|
|
63
69
|
* mapped on Linux too), a process of this machine 127.0.0.1, another the public address.
|
|
@@ -166,8 +172,10 @@ class GatewayServer {
|
|
|
166
172
|
const owners = access_store_1.AccessUtil.parseList(env.TA_GATEWAY_ADMINS);
|
|
167
173
|
this.policy = new gateway_policy_1.GatewayPolicy(options.dir, owners);
|
|
168
174
|
const allowed = [...access_store_1.AccessUtil.parseList(env.TA_ALLOWED_USERS), ...owners];
|
|
169
|
-
this.flow = new sign_in_flow_1.SignInFlow(sign_in_provider_1.SignInProviders.fromEnv(env), new access_store_1.AccessStore((0, node_path_1.join)(options.dir, 'access.json'), allowed), sign_in_provider_1.SignInProviders.publicUrl(env));
|
|
175
|
+
this.flow = new sign_in_flow_1.SignInFlow(sign_in_provider_1.SignInProviders.fromEnv(env), new access_store_1.AccessStore((0, node_path_1.join)(options.dir, 'access.json'), allowed), sign_in_provider_1.SignInProviders.publicUrl(env), sign_in_provider_1.SignInProviders.polled(env));
|
|
170
176
|
this.tenants = new tenant_store_1.TenantStore(options.dir);
|
|
177
|
+
// The devices of each account (docs/design/ta-account.md 3), its tokens those of this gateway's sign-in.
|
|
178
|
+
this.accounts = new node_1.RelayServerAccounts(options.dir, { authenticate: async (token) => GatewayServerUtil.accountOf(this.caller(token)) });
|
|
171
179
|
this.links = new sign_in_links_1.SignInLinks(options.dir);
|
|
172
180
|
this.environments = new environments_1.Environments(providers, this.tenants, {
|
|
173
181
|
default: config.default,
|
|
@@ -283,6 +291,7 @@ class GatewayServer {
|
|
|
283
291
|
}
|
|
284
292
|
async close() {
|
|
285
293
|
clearInterval(this.ticker);
|
|
294
|
+
this.flow.stopPolling();
|
|
286
295
|
await this.tick().catch(() => undefined);
|
|
287
296
|
for (const socket of this.sockets) {
|
|
288
297
|
socket.close(1001, 'The gateway is stopping.');
|
|
@@ -351,6 +360,9 @@ class GatewayServer {
|
|
|
351
360
|
onSignedIn: (res, token, account, origin) => this.signedIn(res, token, account, origin),
|
|
352
361
|
});
|
|
353
362
|
}
|
|
363
|
+
else if (path.startsWith(ta_relay_1.RELAY_ACCOUNT_PATH)) {
|
|
364
|
+
this.accounts.serve(request, response);
|
|
365
|
+
}
|
|
354
366
|
else if (path === model_proxy_1.ModelProxyUtil.PATH || path.startsWith(`${model_proxy_1.ModelProxyUtil.PATH}/`)) {
|
|
355
367
|
await this.models.handle(request, response);
|
|
356
368
|
}
|
|
@@ -427,15 +439,30 @@ class GatewayServer {
|
|
|
427
439
|
origin(request) {
|
|
428
440
|
return sign_in_flow_1.SignInUtil.origin(request, this.flow.publicUrl);
|
|
429
441
|
}
|
|
430
|
-
/**
|
|
442
|
+
/**
|
|
443
|
+
* `/api/files` of the Ta of the tenant whose cookie came along, or whose token: the app on a phone and Ta Desktop, of
|
|
444
|
+
* another origin, read them with it (TaFiles), and are answered as any origin is, the token being what lets them in.
|
|
445
|
+
*/
|
|
431
446
|
async files(request, response) {
|
|
432
447
|
var _a, _b;
|
|
448
|
+
const header = request.headers.authorization;
|
|
449
|
+
const bearer = (header === null || header === void 0 ? void 0 : header.startsWith('Bearer ')) ? header.slice('Bearer '.length) : undefined;
|
|
450
|
+
if (bearer !== undefined || request.method === 'OPTIONS') {
|
|
451
|
+
response.setHeader('Access-Control-Allow-Origin', '*');
|
|
452
|
+
response.setHeader('Access-Control-Allow-Headers', 'Authorization, Range');
|
|
453
|
+
response.setHeader('Access-Control-Expose-Headers', 'Content-Type, Content-Length, Content-Range');
|
|
454
|
+
response.setHeader('Access-Control-Max-Age', '600');
|
|
455
|
+
}
|
|
456
|
+
if (request.method === 'OPTIONS') {
|
|
457
|
+
sign_in_pages_1.SignInPageUtil.end(response, 204);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
433
460
|
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
434
461
|
response.setHeader('Allow', 'GET, HEAD');
|
|
435
462
|
sign_in_pages_1.SignInPageUtil.end(response, 405);
|
|
436
463
|
return;
|
|
437
464
|
}
|
|
438
|
-
const caller = this.caller((_a = sign_in_pages_1.SignInPageUtil.cookie(request.headers.cookie, FILES_COOKIE)) !== null && _a !== void 0 ? _a : '');
|
|
465
|
+
const caller = this.caller((_a = bearer !== null && bearer !== void 0 ? bearer : sign_in_pages_1.SignInPageUtil.cookie(request.headers.cookie, FILES_COOKIE)) !== null && _a !== void 0 ? _a : '');
|
|
439
466
|
if (!caller) {
|
|
440
467
|
sign_in_pages_1.SignInPageUtil.end(response, 404);
|
|
441
468
|
return;
|
|
@@ -446,25 +473,7 @@ class GatewayServer {
|
|
|
446
473
|
sign_in_pages_1.SignInPageUtil.end(response, 404);
|
|
447
474
|
return;
|
|
448
475
|
}
|
|
449
|
-
|
|
450
|
-
for (const name of ['range', 'if-none-match', 'accept', 'accept-encoding', 'user-agent']) {
|
|
451
|
-
const value = request.headers[name];
|
|
452
|
-
if (value !== undefined) {
|
|
453
|
-
headers[name] = value;
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
await new Promise((resolve, reject) => {
|
|
457
|
-
const send = target.protocol === 'https:' ? node_https_1.request : node_http_1.request;
|
|
458
|
-
const outgoing = send(target, { method: request.method, headers }, incoming => {
|
|
459
|
-
var _a;
|
|
460
|
-
response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, GatewayServerUtil.filesHeaders(incoming.headers));
|
|
461
|
-
// A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
|
|
462
|
-
(0, node_stream_1.pipeline)(incoming, response, () => resolve());
|
|
463
|
-
});
|
|
464
|
-
outgoing.on('error', reject);
|
|
465
|
-
response.once('close', () => outgoing.destroy());
|
|
466
|
-
outgoing.end();
|
|
467
|
-
});
|
|
476
|
+
await gateway_files_1.GatewayFiles.forward(request, response, target, GatewayServerUtil.filesHeaders);
|
|
468
477
|
}
|
|
469
478
|
page(request, response, path) {
|
|
470
479
|
var _a;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-gateway",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.19.0",
|
|
4
4
|
"description": "ta gateway: one Ta per account, each in a sandbox of its own, behind one sign-in",
|
|
5
5
|
"homepage": "https://github.com/cellbang/ta",
|
|
6
6
|
"bugs": {
|
|
@@ -29,9 +29,10 @@
|
|
|
29
29
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@celljs/rpc": "4.
|
|
33
|
-
"@celljs/ta-host": "4.
|
|
34
|
-
"@celljs/ta-protocol": "4.
|
|
32
|
+
"@celljs/rpc": "4.19.0",
|
|
33
|
+
"@celljs/ta-host": "4.19.0",
|
|
34
|
+
"@celljs/ta-protocol": "4.19.0",
|
|
35
|
+
"@celljs/ta-relay": "4.19.0"
|
|
35
36
|
},
|
|
36
37
|
"publishConfig": {
|
|
37
38
|
"access": "public"
|