@celljs/ta-gateway 0.0.0-stage → 4.9.1
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/README.en.md +80 -0
- package/README.md +79 -2
- package/esm/node/command-sandbox.d.ts +28 -0
- package/esm/node/command-sandbox.js +83 -0
- package/esm/node/docker-engine.d.ts +56 -0
- package/esm/node/docker-engine.js +151 -0
- package/esm/node/docker-sandbox.d.ts +45 -0
- package/esm/node/docker-sandbox.js +110 -0
- package/esm/node/environments.d.ts +68 -0
- package/esm/node/environments.js +237 -0
- package/esm/node/gateway-cli.d.ts +9 -0
- package/esm/node/gateway-cli.js +99 -0
- package/esm/node/gateway-server.d.ts +67 -0
- package/esm/node/gateway-server.js +288 -0
- package/esm/node/gateway-service.d.ts +58 -0
- package/esm/node/gateway-service.js +187 -0
- package/esm/node/gateway-socket.d.ts +33 -0
- package/esm/node/gateway-socket.js +143 -0
- package/esm/node/index.d.ts +13 -0
- package/esm/node/index.js +14 -0
- package/esm/node/local-sandbox.d.ts +23 -0
- package/esm/node/local-sandbox.js +73 -0
- package/esm/node/sandbox-provider.d.ts +72 -0
- package/esm/node/sandbox-provider.js +83 -0
- package/esm/node/sign-in-links.d.ts +25 -0
- package/esm/node/sign-in-links.js +44 -0
- package/esm/node/ta-sandbox.d.ts +23 -0
- package/esm/node/ta-sandbox.js +47 -0
- package/esm/node/tenant-store.d.ts +36 -0
- package/esm/node/tenant-store.js +83 -0
- package/esm/package.json +3 -0
- package/lib/node/command-sandbox.d.ts +29 -0
- package/lib/node/command-sandbox.js +87 -0
- package/lib/node/docker-engine.d.ts +57 -0
- package/lib/node/docker-engine.js +156 -0
- package/lib/node/docker-sandbox.d.ts +46 -0
- package/lib/node/docker-sandbox.js +114 -0
- package/lib/node/environments.d.ts +69 -0
- package/lib/node/environments.js +242 -0
- package/lib/node/gateway-cli.d.ts +10 -0
- package/lib/node/gateway-cli.js +102 -0
- package/lib/node/gateway-server.d.ts +68 -0
- package/lib/node/gateway-server.js +292 -0
- package/lib/node/gateway-service.d.ts +59 -0
- package/lib/node/gateway-service.js +192 -0
- package/lib/node/gateway-socket.d.ts +34 -0
- package/lib/node/gateway-socket.js +147 -0
- package/lib/node/index.d.ts +14 -0
- package/lib/node/index.js +30 -0
- package/lib/node/local-sandbox.d.ts +24 -0
- package/lib/node/local-sandbox.js +77 -0
- package/lib/node/sandbox-provider.d.ts +73 -0
- package/lib/node/sandbox-provider.js +87 -0
- package/lib/node/sign-in-links.d.ts +26 -0
- package/lib/node/sign-in-links.js +48 -0
- package/lib/node/ta-sandbox.d.ts +24 -0
- package/lib/node/ta-sandbox.js +51 -0
- package/lib/node/tenant-store.d.ts +37 -0
- package/lib/node/tenant-store.js +88 -0
- package/package.json +57 -4
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type TaEnvironmentChoice, type TaEnvironmentInfo, type TaEnvironmentOption, type TaEnvironmentStatus } from '@celljs/ta-protocol';
|
|
2
|
+
import type { SandboxEndpoint, SandboxProvider, SandboxTenant } from './sandbox-provider.js';
|
|
3
|
+
import type { Tenant, TenantStore } from './tenant-store.js';
|
|
4
|
+
interface Running {
|
|
5
|
+
status: TaEnvironmentStatus;
|
|
6
|
+
error?: string;
|
|
7
|
+
endpoint?: SandboxEndpoint;
|
|
8
|
+
starting?: Promise<SandboxEndpoint>;
|
|
9
|
+
connections: number;
|
|
10
|
+
idle?: ReturnType<typeof setTimeout>;
|
|
11
|
+
}
|
|
12
|
+
export declare class EnvironmentError extends Error {
|
|
13
|
+
}
|
|
14
|
+
export declare namespace EnvironmentsUtil {
|
|
15
|
+
/** Whether a Ta service answers at `url`, whatever it answers. */
|
|
16
|
+
function waitReady(url: string, timeoutMs?: number, fetcher?: typeof fetch): Promise<void>;
|
|
17
|
+
/** One call of the Ta service at `endpoint` over a WebSocket of its own: the gateway's, not a browser's. */
|
|
18
|
+
function call<T>(endpoint: SandboxEndpoint, method: string, params?: unknown[], timeoutMs?: number): Promise<T>;
|
|
19
|
+
function sandboxTenant(tenant: Tenant): SandboxTenant;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The environments of the tenants: which provider runs each, starting it on its first connection (once,
|
|
23
|
+
* whoever asks meanwhile), and stopping it when nobody has been connected for `idleMinutes`.
|
|
24
|
+
*/
|
|
25
|
+
export declare class Environments {
|
|
26
|
+
readonly providers: SandboxProvider[];
|
|
27
|
+
protected readonly tenants: TenantStore;
|
|
28
|
+
readonly settings: {
|
|
29
|
+
default?: string;
|
|
30
|
+
own: boolean;
|
|
31
|
+
idleMinutes: number;
|
|
32
|
+
readyTimeoutMs?: number;
|
|
33
|
+
};
|
|
34
|
+
protected readonly running: Map<string, Running>;
|
|
35
|
+
constructor(providers: SandboxProvider[], tenants: TenantStore, settings: {
|
|
36
|
+
default?: string;
|
|
37
|
+
own: boolean;
|
|
38
|
+
idleMinutes: number;
|
|
39
|
+
readyTimeoutMs?: number;
|
|
40
|
+
});
|
|
41
|
+
/** The providers one may pick. */
|
|
42
|
+
choices(): TaEnvironmentOption[];
|
|
43
|
+
/** The provider of a tenant: its own Ta service, the one it picked, or the default. */
|
|
44
|
+
provider(tenant: Tenant): {
|
|
45
|
+
provider: SandboxProvider;
|
|
46
|
+
own: boolean;
|
|
47
|
+
};
|
|
48
|
+
info(tenant: Tenant): TaEnvironmentInfo;
|
|
49
|
+
connections(tenantId: string): number;
|
|
50
|
+
/** Where the Ta of the tenant answers, started when it does not run. */
|
|
51
|
+
ensure(tenant: Tenant): Promise<SandboxEndpoint>;
|
|
52
|
+
/** The sandbox of the tenant answered no more (its connection dropped): looked for again on the next. */
|
|
53
|
+
lost(tenantId: string): void;
|
|
54
|
+
connect(tenantId: string): () => void;
|
|
55
|
+
/** Its connections are the caller's to close: they would only find the sandbox gone. */
|
|
56
|
+
stop(tenantId: string): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* The account of the tenant is no longer let in: the devices paired with its Ta (`ta pair`, which reach it
|
|
59
|
+
* through a relay, not the gateway) are unpaired, and its sandbox is stopped. A Ta service of the user's
|
|
60
|
+
* is theirs: left as it is.
|
|
61
|
+
*/
|
|
62
|
+
revoke(tenant: Tenant): Promise<number>;
|
|
63
|
+
/** Another environment for the tenant: the one before is stopped (a service of the user's is left as it is); its connections are the caller's to close. */
|
|
64
|
+
change(tenant: Tenant, choice: TaEnvironmentChoice | undefined): Promise<Tenant>;
|
|
65
|
+
dispose(): Promise<void>;
|
|
66
|
+
protected state(tenantId: string): Running;
|
|
67
|
+
}
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { TA_WEBSOCKET_PATH } from '@celljs/ta-protocol';
|
|
2
|
+
import { TaSandbox } from './ta-sandbox.js';
|
|
3
|
+
/** How long a sandbox has to answer once started: the first start of a container may pull its image first. */
|
|
4
|
+
const READY_TIMEOUT_MS = 120000;
|
|
5
|
+
export class EnvironmentError extends Error {
|
|
6
|
+
}
|
|
7
|
+
export var EnvironmentsUtil;
|
|
8
|
+
(function (EnvironmentsUtil) {
|
|
9
|
+
/** Whether a Ta service answers at `url`, whatever it answers. */
|
|
10
|
+
async function waitReady(url, timeoutMs = READY_TIMEOUT_MS, fetcher = fetch) {
|
|
11
|
+
var _a;
|
|
12
|
+
const until = Date.now() + timeoutMs;
|
|
13
|
+
let last;
|
|
14
|
+
while (Date.now() < until) {
|
|
15
|
+
try {
|
|
16
|
+
const response = await fetcher(`${url}/`, { method: 'HEAD', signal: AbortSignal.timeout(5000) });
|
|
17
|
+
await ((_a = response.body) === null || _a === void 0 ? void 0 : _a.cancel());
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
last = error;
|
|
22
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
throw new EnvironmentError(`Nothing answered at ${url} within ${Math.round(timeoutMs / 1000)} seconds${last instanceof Error ? `: ${last.message}` : ''}.`);
|
|
26
|
+
}
|
|
27
|
+
EnvironmentsUtil.waitReady = waitReady;
|
|
28
|
+
/** One call of the Ta service at `endpoint` over a WebSocket of its own: the gateway's, not a browser's. */
|
|
29
|
+
function call(endpoint, method, params = [], timeoutMs = 10000) {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const url = new URL(`${endpoint.url.replace(/^http/, 'ws')}${TA_WEBSOCKET_PATH}`);
|
|
32
|
+
url.searchParams.set('token', endpoint.token);
|
|
33
|
+
const socket = new WebSocket(url);
|
|
34
|
+
const timer = setTimeout(() => finish(new EnvironmentError(`${method} took too long`)), timeoutMs);
|
|
35
|
+
const finish = (error, result) => {
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
socket.close();
|
|
38
|
+
if (error) {
|
|
39
|
+
reject(error);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
resolve(result);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
socket.onopen = () => socket.send(JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }));
|
|
46
|
+
socket.onerror = () => finish(new EnvironmentError(`Cannot reach ${endpoint.url}`));
|
|
47
|
+
socket.onmessage = message => {
|
|
48
|
+
const answer = JSON.parse(String(message.data));
|
|
49
|
+
if (answer.id === 1) {
|
|
50
|
+
finish(answer.error ? new EnvironmentError(answer.error.message) : undefined, answer.result);
|
|
51
|
+
}
|
|
52
|
+
else if (answer.method !== undefined && answer.id !== undefined) {
|
|
53
|
+
// A push of the server (TaClient): answered, so that it does not wait.
|
|
54
|
+
socket.send(JSON.stringify({ jsonrpc: '2.0', id: answer.id, result: null }));
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
EnvironmentsUtil.call = call;
|
|
60
|
+
function sandboxTenant(tenant) {
|
|
61
|
+
return { id: tenant.id, account: tenant.account, token: tenant.token };
|
|
62
|
+
}
|
|
63
|
+
EnvironmentsUtil.sandboxTenant = sandboxTenant;
|
|
64
|
+
})(EnvironmentsUtil || (EnvironmentsUtil = {}));
|
|
65
|
+
/**
|
|
66
|
+
* The environments of the tenants: which provider runs each, starting it on its first connection (once,
|
|
67
|
+
* whoever asks meanwhile), and stopping it when nobody has been connected for `idleMinutes`.
|
|
68
|
+
*/
|
|
69
|
+
export class Environments {
|
|
70
|
+
constructor(providers, tenants, settings) {
|
|
71
|
+
this.providers = providers;
|
|
72
|
+
this.tenants = tenants;
|
|
73
|
+
this.settings = settings;
|
|
74
|
+
this.running = new Map();
|
|
75
|
+
}
|
|
76
|
+
/** The providers one may pick. */
|
|
77
|
+
choices() {
|
|
78
|
+
return this.providers.map(({ id, name, type }) => ({ id, name, type }));
|
|
79
|
+
}
|
|
80
|
+
/** The provider of a tenant: its own Ta service, the one it picked, or the default. */
|
|
81
|
+
provider(tenant) {
|
|
82
|
+
var _a;
|
|
83
|
+
var _b, _c, _d, _e;
|
|
84
|
+
const choice = tenant.environment;
|
|
85
|
+
if (choice && 'type' in choice) {
|
|
86
|
+
return {
|
|
87
|
+
provider: new TaSandbox('own', { type: 'ta', url: choice.url, token: choice.token, name: (_b = choice.name) !== null && _b !== void 0 ? _b : new URL(choice.url).host }),
|
|
88
|
+
own: true,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const id = (_d = (_c = choice === null || choice === void 0 ? void 0 : choice.provider) !== null && _c !== void 0 ? _c : this.settings.default) !== null && _d !== void 0 ? _d : (_a = this.providers[0]) === null || _a === void 0 ? void 0 : _a.id;
|
|
92
|
+
const provider = (_e = this.providers.find(one => one.id === id)) !== null && _e !== void 0 ? _e : this.providers.find(one => one.id === this.settings.default);
|
|
93
|
+
if (!provider) {
|
|
94
|
+
throw new EnvironmentError('The gateway has no provider of sandboxes: add one to sandboxes.json, or use a Ta service of your own.');
|
|
95
|
+
}
|
|
96
|
+
return { provider, own: false };
|
|
97
|
+
}
|
|
98
|
+
info(tenant) {
|
|
99
|
+
var _a;
|
|
100
|
+
const running = this.running.get(tenant.id);
|
|
101
|
+
let picked;
|
|
102
|
+
try {
|
|
103
|
+
picked = this.provider(tenant);
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
return { provider: '', name: '', status: 'failed', error: error.message };
|
|
107
|
+
}
|
|
108
|
+
const { provider, own } = picked;
|
|
109
|
+
return {
|
|
110
|
+
provider: own ? 'own' : provider.id,
|
|
111
|
+
name: provider.name,
|
|
112
|
+
status: (_a = running === null || running === void 0 ? void 0 : running.status) !== null && _a !== void 0 ? _a : 'stopped',
|
|
113
|
+
error: running === null || running === void 0 ? void 0 : running.error,
|
|
114
|
+
url: own && provider instanceof TaSandbox ? provider.url : undefined,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
connections(tenantId) {
|
|
118
|
+
var _a;
|
|
119
|
+
var _b;
|
|
120
|
+
return (_b = (_a = this.running.get(tenantId)) === null || _a === void 0 ? void 0 : _a.connections) !== null && _b !== void 0 ? _b : 0;
|
|
121
|
+
}
|
|
122
|
+
/** Where the Ta of the tenant answers, started when it does not run. */
|
|
123
|
+
ensure(tenant) {
|
|
124
|
+
const running = this.state(tenant.id);
|
|
125
|
+
if (running.status === 'running' && running.endpoint) {
|
|
126
|
+
return Promise.resolve(running.endpoint);
|
|
127
|
+
}
|
|
128
|
+
if (running.starting) {
|
|
129
|
+
return running.starting;
|
|
130
|
+
}
|
|
131
|
+
running.status = 'starting';
|
|
132
|
+
running.error = undefined;
|
|
133
|
+
const starting = (async () => {
|
|
134
|
+
var _a;
|
|
135
|
+
const { provider } = this.provider(tenant);
|
|
136
|
+
const endpoint = await provider.start(EnvironmentsUtil.sandboxTenant(tenant));
|
|
137
|
+
await EnvironmentsUtil.waitReady(endpoint.url, (_a = this.settings.readyTimeoutMs) !== null && _a !== void 0 ? _a : provider.readyTimeoutMs);
|
|
138
|
+
return endpoint;
|
|
139
|
+
})();
|
|
140
|
+
running.starting = starting;
|
|
141
|
+
starting.then(endpoint => {
|
|
142
|
+
if (running.starting === starting) {
|
|
143
|
+
Object.assign(running, { status: 'running', endpoint, starting: undefined });
|
|
144
|
+
}
|
|
145
|
+
}, (error) => {
|
|
146
|
+
if (running.starting === starting) {
|
|
147
|
+
Object.assign(running, { status: 'failed', error: error.message, endpoint: undefined, starting: undefined });
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
return starting;
|
|
151
|
+
}
|
|
152
|
+
/** The sandbox of the tenant answered no more (its connection dropped): looked for again on the next. */
|
|
153
|
+
lost(tenantId) {
|
|
154
|
+
const running = this.running.get(tenantId);
|
|
155
|
+
if (running && !running.starting) {
|
|
156
|
+
Object.assign(running, { status: 'stopped', endpoint: undefined });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
connect(tenantId) {
|
|
160
|
+
const running = this.state(tenantId);
|
|
161
|
+
running.connections++;
|
|
162
|
+
clearTimeout(running.idle);
|
|
163
|
+
let done = false;
|
|
164
|
+
return () => {
|
|
165
|
+
var _a, _b;
|
|
166
|
+
if (done) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
done = true;
|
|
170
|
+
running.connections--;
|
|
171
|
+
const minutes = this.settings.idleMinutes;
|
|
172
|
+
if (running.connections === 0 && minutes > 0) {
|
|
173
|
+
running.idle = setTimeout(() => void this.stop(tenantId).catch(() => undefined), minutes * 60000);
|
|
174
|
+
(_b = (_a = running.idle).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/** Its connections are the caller's to close: they would only find the sandbox gone. */
|
|
179
|
+
async stop(tenantId) {
|
|
180
|
+
var _a;
|
|
181
|
+
const tenant = this.tenants.get(tenantId);
|
|
182
|
+
if (!tenant) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const running = this.state(tenantId);
|
|
186
|
+
await ((_a = running.starting) === null || _a === void 0 ? void 0 : _a.catch(() => undefined));
|
|
187
|
+
Object.assign(running, { status: 'stopped', endpoint: undefined, error: undefined });
|
|
188
|
+
await this.provider(tenant).provider.stop(EnvironmentsUtil.sandboxTenant(tenant));
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The account of the tenant is no longer let in: the devices paired with its Ta (`ta pair`, which reach it
|
|
192
|
+
* through a relay, not the gateway) are unpaired, and its sandbox is stopped. A Ta service of the user's
|
|
193
|
+
* is theirs: left as it is.
|
|
194
|
+
*/
|
|
195
|
+
async revoke(tenant) {
|
|
196
|
+
const { provider, own } = this.provider(tenant);
|
|
197
|
+
if (own) {
|
|
198
|
+
return 0;
|
|
199
|
+
}
|
|
200
|
+
let unpaired = 0;
|
|
201
|
+
if ((await provider.state(EnvironmentsUtil.sandboxTenant(tenant))) === 'running') {
|
|
202
|
+
const endpoint = await provider.start(EnvironmentsUtil.sandboxTenant(tenant));
|
|
203
|
+
const info = await EnvironmentsUtil.call(endpoint, 'TaRelayServer/getRelay');
|
|
204
|
+
for (const device of info.devices) {
|
|
205
|
+
await EnvironmentsUtil.call(endpoint, 'TaRelayServer/unpair', [device.id]);
|
|
206
|
+
unpaired++;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
await this.stop(tenant.id);
|
|
210
|
+
return unpaired;
|
|
211
|
+
}
|
|
212
|
+
/** Another environment for the tenant: the one before is stopped (a service of the user's is left as it is); its connections are the caller's to close. */
|
|
213
|
+
async change(tenant, choice) {
|
|
214
|
+
await this.stop(tenant.id).catch(() => undefined);
|
|
215
|
+
const next = this.tenants.update(tenant.id, { environment: choice });
|
|
216
|
+
// What is picked must be there: an id of none throws here, not on the next start.
|
|
217
|
+
this.provider(next);
|
|
218
|
+
// The state stays: the connections it counts close and open again, and release it as they do.
|
|
219
|
+
Object.assign(this.state(tenant.id), { status: 'stopped', endpoint: undefined, error: undefined });
|
|
220
|
+
return next;
|
|
221
|
+
}
|
|
222
|
+
async dispose() {
|
|
223
|
+
for (const running of this.running.values()) {
|
|
224
|
+
clearTimeout(running.idle);
|
|
225
|
+
}
|
|
226
|
+
await Promise.all(this.providers.map(provider => { var _a; return (_a = provider.dispose) === null || _a === void 0 ? void 0 : _a.call(provider); }));
|
|
227
|
+
}
|
|
228
|
+
state(tenantId) {
|
|
229
|
+
let running = this.running.get(tenantId);
|
|
230
|
+
if (!running) {
|
|
231
|
+
running = { status: 'stopped', connections: 0 };
|
|
232
|
+
this.running.set(tenantId, running);
|
|
233
|
+
}
|
|
234
|
+
return running;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=environments.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface GatewayCliOptions {
|
|
2
|
+
/** The web client: dist/public of @celljs/ta. */
|
|
3
|
+
publicDir: string;
|
|
4
|
+
/** How to run `ta` (the local sandboxes). */
|
|
5
|
+
taCommand: string[];
|
|
6
|
+
}
|
|
7
|
+
export declare namespace GatewayCli {
|
|
8
|
+
function run(argv: string[], options: GatewayCliOptions): Promise<void>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { GatewayServer } from './gateway-server.js';
|
|
5
|
+
import { SandboxProviders } from './sandbox-provider.js';
|
|
6
|
+
import { SignInLinks } from './sign-in-links.js';
|
|
7
|
+
const HELP = `Usage: ta gateway [--host <host>] [--port <port>] [--dir <dir>]
|
|
8
|
+
ta gateway link <account> [--name <name>] [--dir <dir>]
|
|
9
|
+
|
|
10
|
+
One sign-in for many people, each with a Ta of their own: an account that signs in is a tenant, whose Ta
|
|
11
|
+
runs in a sandbox of its own (a container per tenant by default), or in a Ta service of the user's. The web
|
|
12
|
+
client is served here; each browser's connection goes to the Ta of its tenant. Stops on SIGINT or SIGTERM.
|
|
13
|
+
|
|
14
|
+
--host Address to listen on. Default: 127.0.0.1
|
|
15
|
+
--port Port to listen on. Default: 4100, or PORT
|
|
16
|
+
--dir Its configuration and data. Default: TA_GATEWAY_DIR, else ~/.ta/gateway
|
|
17
|
+
|
|
18
|
+
Accounts sign in with the providers of the environment (TA_GITHUB_*, TA_GOOGLE_*, TA_WECHAT_*, TA_OIDC_*, as
|
|
19
|
+
for ta serve) when TA_ALLOWED_USERS lets them in; TA_GATEWAY_ADMINS are let in too and manage the others.
|
|
20
|
+
"ta gateway link <account>" prints a link that signs in as the account once, within a day, and lets it in:
|
|
21
|
+
for someone without a provider of accounts (user:alice), or before one is set up.
|
|
22
|
+
|
|
23
|
+
The sandboxes are those of sandboxes.json in the directory, as pi's models.json for models:
|
|
24
|
+
{ "default": "docker", "providers": { "docker": { "type": "docker", "image": "ta:latest", "memory": "4g" } } }
|
|
25
|
+
Types: docker, command (shell commands for any platform), local (no isolation: tests), ta (a Ta service that
|
|
26
|
+
runs), module (a file whose default export makes a provider). See the README of @celljs/ta-gateway.
|
|
27
|
+
`;
|
|
28
|
+
export var GatewayCli;
|
|
29
|
+
(function (GatewayCli) {
|
|
30
|
+
async function run(argv, options) {
|
|
31
|
+
var _a, _b, _c, _d;
|
|
32
|
+
const { values, positionals } = parseArgs({
|
|
33
|
+
args: argv,
|
|
34
|
+
allowPositionals: true,
|
|
35
|
+
options: {
|
|
36
|
+
host: { type: 'string' },
|
|
37
|
+
port: { type: 'string' },
|
|
38
|
+
dir: { type: 'string' },
|
|
39
|
+
name: { type: 'string' },
|
|
40
|
+
help: { type: 'boolean', short: 'h' },
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
if (values.help) {
|
|
44
|
+
process.stdout.write(HELP);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const dir = (_b = (_a = values.dir) !== null && _a !== void 0 ? _a : process.env.TA_GATEWAY_DIR) !== null && _b !== void 0 ? _b : join(homedir(), '.ta', 'gateway');
|
|
48
|
+
if (positionals[0] === 'link') {
|
|
49
|
+
link(dir, positionals[1], values.name);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (positionals.length > 0) {
|
|
53
|
+
process.stdout.write(HELP);
|
|
54
|
+
process.exitCode = 1;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
let gateway;
|
|
58
|
+
try {
|
|
59
|
+
gateway = await GatewayServer.create({
|
|
60
|
+
dir,
|
|
61
|
+
host: values.host,
|
|
62
|
+
port: Number((_d = (_c = values.port) !== null && _c !== void 0 ? _c : process.env.PORT) !== null && _d !== void 0 ? _d : 4100),
|
|
63
|
+
publicDir: options.publicDir,
|
|
64
|
+
taCommand: options.taCommand,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
console.error(`ta gateway: ${error.message}`);
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const url = await gateway.listen();
|
|
73
|
+
const ways = gateway.flow.providers.map(provider => provider.name);
|
|
74
|
+
console.log(`ta gateway: ${url} (${dir})`);
|
|
75
|
+
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>'}`);
|
|
76
|
+
const stop = () => void gateway.close().then(() => process.exit(0));
|
|
77
|
+
process.once('SIGINT', stop);
|
|
78
|
+
process.once('SIGTERM', stop);
|
|
79
|
+
}
|
|
80
|
+
GatewayCli.run = run;
|
|
81
|
+
function link(dir, account, name) {
|
|
82
|
+
if (!account) {
|
|
83
|
+
console.error('ta gateway link: name the account, such as github:octocat or user:alice.');
|
|
84
|
+
process.exitCode = 1;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
const path = new SignInLinks(dir).create(account, name);
|
|
89
|
+
const base = SandboxProviders.publicBase(process.env);
|
|
90
|
+
console.log('Open this link to sign in as the account, once, within a day. Anyone with it signs in as it: send it to its owner only.\n');
|
|
91
|
+
console.log(base ? `${base}${path}` : `<address of the gateway>${path}`);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
console.error(`ta gateway link: ${error.message}`);
|
|
95
|
+
process.exitCode = 1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
})(GatewayCli || (GatewayCli = {}));
|
|
99
|
+
//# sourceMappingURL=gateway-cli.js.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type IncomingMessage, type Server, type ServerResponse } from 'node:http';
|
|
2
|
+
import { type Duplex } from 'node:stream';
|
|
3
|
+
import { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
|
|
4
|
+
import { type SignInAccount } from '@celljs/ta-host/lib/node/sign-in/sign-in-provider';
|
|
5
|
+
import { Environments } from './environments.js';
|
|
6
|
+
import { type GatewayCaller, GatewayService } from './gateway-service.js';
|
|
7
|
+
import { GatewaySocket } from './gateway-socket.js';
|
|
8
|
+
import { type SandboxProviderFactory, SandboxProviders } from './sandbox-provider.js';
|
|
9
|
+
import { SignInLinks } from './sign-in-links.js';
|
|
10
|
+
import { TenantStore } from './tenant-store.js';
|
|
11
|
+
/** The built-in types of providers of sandboxes. */
|
|
12
|
+
export declare const SANDBOX_TYPES: Record<string, SandboxProviderFactory>;
|
|
13
|
+
export interface GatewayServerOptions {
|
|
14
|
+
/** Its configuration and data: sandboxes.json, access.json, tenants/, links.json. */
|
|
15
|
+
dir: string;
|
|
16
|
+
host?: string;
|
|
17
|
+
port?: number;
|
|
18
|
+
/** The web client: dist/public of @celljs/ta. */
|
|
19
|
+
publicDir: string;
|
|
20
|
+
taCommand: string[];
|
|
21
|
+
env?: Record<string, string | undefined>;
|
|
22
|
+
}
|
|
23
|
+
export declare namespace GatewayServerUtil {
|
|
24
|
+
/** A file of `root` for a path of the page, never outside it; the page for what is not a file. */
|
|
25
|
+
function file(root: string, urlPath: string): string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* `ta gateway`: one sign-in for many people, each with a Ta of their own. An account that signs in is a
|
|
29
|
+
* tenant (GitHub, Google, WeChat, OpenID Connect, or a link of `ta gateway link`); its Ta runs in a sandbox
|
|
30
|
+
* of its own, made by a provider of sandboxes.json, or in a Ta service of the user's. The gateway serves the
|
|
31
|
+
* web client, forwards each browser's WebSocket to the Ta of its tenant, and answers TaGatewayServer and
|
|
32
|
+
* TaAccessServer itself.
|
|
33
|
+
*/
|
|
34
|
+
export declare class GatewayServer {
|
|
35
|
+
protected readonly options: GatewayServerOptions;
|
|
36
|
+
readonly flow: SignInFlow;
|
|
37
|
+
readonly tenants: TenantStore;
|
|
38
|
+
readonly environments: Environments;
|
|
39
|
+
readonly links: SignInLinks;
|
|
40
|
+
protected readonly service: GatewayService;
|
|
41
|
+
protected readonly sockets: Set<GatewaySocket>;
|
|
42
|
+
protected readonly admins: string[];
|
|
43
|
+
protected server?: Server;
|
|
44
|
+
static create(options: GatewayServerOptions): Promise<GatewayServer>;
|
|
45
|
+
protected constructor(options: GatewayServerOptions, providers: Awaited<ReturnType<typeof SandboxProviders.create>>, config: ReturnType<typeof SandboxProviders.read>);
|
|
46
|
+
isAdmin(account: string): boolean;
|
|
47
|
+
listen(): Promise<string>;
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
/** The browsers whose sign-in no longer works are cut off. */
|
|
50
|
+
cutOff(): void;
|
|
51
|
+
/**
|
|
52
|
+
* The connections of the tenant of now close, and connect again to its environment: in a moment, so that
|
|
53
|
+
* the answer to the call that changed it goes first. Those that open meanwhile stay.
|
|
54
|
+
*/
|
|
55
|
+
reset(tenantId: string): void;
|
|
56
|
+
/** Who a token signs in, while its account is let in. */
|
|
57
|
+
caller(token: string): GatewayCaller | undefined;
|
|
58
|
+
protected handle(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
59
|
+
/** A link of `ta gateway link`: lets its account in, and signs this browser in as it. */
|
|
60
|
+
protected useLink(request: IncomingMessage, response: ServerResponse, secret: string): void;
|
|
61
|
+
protected signedIn(response: ServerResponse, token: string, account: SignInAccount, origin: string): void;
|
|
62
|
+
protected origin(request: IncomingMessage): string;
|
|
63
|
+
/** `/api/files` of the Ta of the tenant whose cookie came along. */
|
|
64
|
+
protected files(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
65
|
+
protected page(request: IncomingMessage, response: ServerResponse, path: string): void;
|
|
66
|
+
protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
|
|
67
|
+
}
|