kraki-relay 0.1.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/LICENSE +21 -0
- package/dist/auth.d.ts +104 -0
- package/dist/auth.js +213 -0
- package/dist/auth.js.map +1 -0
- package/dist/channel-manager.d.ts +92 -0
- package/dist/channel-manager.js +197 -0
- package/dist/channel-manager.js.map +1 -0
- package/dist/cli.d.ts +21 -0
- package/dist/cli.js +156 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +31 -0
- package/dist/logger.js +103 -0
- package/dist/logger.js.map +1 -0
- package/dist/router.d.ts +29 -0
- package/dist/router.js +211 -0
- package/dist/router.js.map +1 -0
- package/dist/server.d.ts +77 -0
- package/dist/server.js +645 -0
- package/dist/server.js.map +1 -0
- package/dist/storage.d.ts +103 -0
- package/dist/storage.js +351 -0
- package/dist/storage.js.map +1 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kraki Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
export interface AuthUser {
|
|
2
|
+
id: string;
|
|
3
|
+
login: string;
|
|
4
|
+
provider: string;
|
|
5
|
+
email?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface AuthSuccess {
|
|
8
|
+
ok: true;
|
|
9
|
+
user: AuthUser;
|
|
10
|
+
}
|
|
11
|
+
export interface AuthFailure {
|
|
12
|
+
ok: false;
|
|
13
|
+
message: string;
|
|
14
|
+
}
|
|
15
|
+
export type AuthOutcome = AuthSuccess | AuthFailure;
|
|
16
|
+
/**
|
|
17
|
+
* Abstract auth provider. Extend this to support different auth methods.
|
|
18
|
+
* The head calls `authenticate()` with whatever credentials the device sends.
|
|
19
|
+
*/
|
|
20
|
+
export interface AuthProvider {
|
|
21
|
+
/** Unique provider name (e.g., 'github', 'open', 'apikey') */
|
|
22
|
+
readonly name: string;
|
|
23
|
+
/** Validate credentials and return user info */
|
|
24
|
+
authenticate(credentials: AuthCredentials): Promise<AuthOutcome>;
|
|
25
|
+
}
|
|
26
|
+
/** Credentials a device can send */
|
|
27
|
+
export interface AuthCredentials {
|
|
28
|
+
token?: string;
|
|
29
|
+
channelKey?: string;
|
|
30
|
+
/** GitHub OAuth authorization code (exchanged for access token) */
|
|
31
|
+
githubCode?: string;
|
|
32
|
+
/** IP address of the connecting device (set by the server, not the client) */
|
|
33
|
+
ip?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* GitHub OAuth token provider.
|
|
37
|
+
* Validates token against api.github.com/user.
|
|
38
|
+
* Optionally exchanges an OAuth authorization code for an access token.
|
|
39
|
+
*/
|
|
40
|
+
export declare class GitHubAuthProvider implements AuthProvider {
|
|
41
|
+
readonly name = "github";
|
|
42
|
+
private fetcher;
|
|
43
|
+
private clientId?;
|
|
44
|
+
private clientSecret?;
|
|
45
|
+
constructor(opts?: {
|
|
46
|
+
fetcher?: typeof fetch;
|
|
47
|
+
clientId?: string;
|
|
48
|
+
clientSecret?: string;
|
|
49
|
+
});
|
|
50
|
+
/** Whether OAuth code exchange is configured */
|
|
51
|
+
get oauthConfigured(): boolean;
|
|
52
|
+
/** Get the configured OAuth client ID (for auth_info_response) */
|
|
53
|
+
getClientId(): string | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Exchange a GitHub OAuth authorization code for an access token.
|
|
56
|
+
* Requires clientId and clientSecret to be configured.
|
|
57
|
+
*/
|
|
58
|
+
exchangeCode(code: string): Promise<{
|
|
59
|
+
ok: true;
|
|
60
|
+
token: string;
|
|
61
|
+
} | {
|
|
62
|
+
ok: false;
|
|
63
|
+
message: string;
|
|
64
|
+
}>;
|
|
65
|
+
authenticate(credentials: AuthCredentials): Promise<AuthOutcome>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Open provider for self-hosted mode. No validation — all connections accepted.
|
|
69
|
+
* Optionally accepts a shared key so tentacles, head, and apps can use the same
|
|
70
|
+
* secret to form a trusted group without full OAuth.
|
|
71
|
+
*
|
|
72
|
+
* TODO: Support multi-user on self-hosted without OAuth (e.g., local user registry)
|
|
73
|
+
*/
|
|
74
|
+
export declare class OpenAuthProvider implements AuthProvider {
|
|
75
|
+
readonly name = "open";
|
|
76
|
+
private sharedKey?;
|
|
77
|
+
constructor(sharedKey?: string);
|
|
78
|
+
authenticate(credentials: AuthCredentials): Promise<AuthOutcome>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Static API key provider for self-hosted relays that want basic protection.
|
|
82
|
+
* Set a key in env/config, devices must send matching key.
|
|
83
|
+
*/
|
|
84
|
+
export declare class ApiKeyAuthProvider implements AuthProvider {
|
|
85
|
+
readonly name = "apikey";
|
|
86
|
+
private validKey;
|
|
87
|
+
constructor(validKey: string);
|
|
88
|
+
authenticate(credentials: AuthCredentials): Promise<AuthOutcome>;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Simple per-IP throttle for auth attempts.
|
|
92
|
+
* Wraps any AuthProvider and rejects after maxAttempts failures within windowMs.
|
|
93
|
+
*/
|
|
94
|
+
export declare class ThrottledAuthProvider implements AuthProvider {
|
|
95
|
+
get name(): string;
|
|
96
|
+
private inner;
|
|
97
|
+
private maxAttempts;
|
|
98
|
+
private windowMs;
|
|
99
|
+
private failures;
|
|
100
|
+
constructor(inner: AuthProvider, maxAttempts?: number, windowMs?: number);
|
|
101
|
+
authenticate(credentials: AuthCredentials): Promise<AuthOutcome>;
|
|
102
|
+
/** Remove stale entries to prevent memory growth on long-running servers. */
|
|
103
|
+
cleanup(): void;
|
|
104
|
+
}
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// ------------------------------------------------------------
|
|
2
|
+
// Auth abstraction — extensible for multiple providers
|
|
3
|
+
// ------------------------------------------------------------
|
|
4
|
+
import { timingSafeEqual } from 'crypto';
|
|
5
|
+
import { getLogger } from './logger.js';
|
|
6
|
+
/** Timing-safe string comparison to prevent timing attacks on secrets */
|
|
7
|
+
function safeEqual(a, b) {
|
|
8
|
+
if (a.length !== b.length)
|
|
9
|
+
return false;
|
|
10
|
+
return timingSafeEqual(Buffer.from(a), Buffer.from(b));
|
|
11
|
+
}
|
|
12
|
+
// --- Built-in providers ---
|
|
13
|
+
/**
|
|
14
|
+
* GitHub OAuth token provider.
|
|
15
|
+
* Validates token against api.github.com/user.
|
|
16
|
+
* Optionally exchanges an OAuth authorization code for an access token.
|
|
17
|
+
*/
|
|
18
|
+
export class GitHubAuthProvider {
|
|
19
|
+
name = 'github';
|
|
20
|
+
fetcher;
|
|
21
|
+
clientId;
|
|
22
|
+
clientSecret;
|
|
23
|
+
constructor(opts) {
|
|
24
|
+
this.fetcher = opts?.fetcher ?? globalThis.fetch;
|
|
25
|
+
this.clientId = opts?.clientId;
|
|
26
|
+
this.clientSecret = opts?.clientSecret;
|
|
27
|
+
}
|
|
28
|
+
/** Whether OAuth code exchange is configured */
|
|
29
|
+
get oauthConfigured() {
|
|
30
|
+
return !!(this.clientId && this.clientSecret);
|
|
31
|
+
}
|
|
32
|
+
/** Get the configured OAuth client ID (for auth_info_response) */
|
|
33
|
+
getClientId() {
|
|
34
|
+
return this.clientId;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Exchange a GitHub OAuth authorization code for an access token.
|
|
38
|
+
* Requires clientId and clientSecret to be configured.
|
|
39
|
+
*/
|
|
40
|
+
async exchangeCode(code) {
|
|
41
|
+
if (!this.clientId || !this.clientSecret) {
|
|
42
|
+
return { ok: false, message: 'GitHub OAuth not configured (missing client_id/client_secret)' };
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
const res = await this.fetcher('https://github.com/login/oauth/access_token', {
|
|
46
|
+
method: 'POST',
|
|
47
|
+
headers: {
|
|
48
|
+
'Content-Type': 'application/json',
|
|
49
|
+
Accept: 'application/json',
|
|
50
|
+
},
|
|
51
|
+
body: JSON.stringify({
|
|
52
|
+
client_id: this.clientId,
|
|
53
|
+
client_secret: this.clientSecret,
|
|
54
|
+
code,
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
if (!res.ok) {
|
|
58
|
+
return { ok: false, message: `GitHub OAuth token exchange returned ${res.status}` };
|
|
59
|
+
}
|
|
60
|
+
const data = await res.json();
|
|
61
|
+
if (data.error) {
|
|
62
|
+
return { ok: false, message: `GitHub OAuth error: ${data.error_description || data.error}` };
|
|
63
|
+
}
|
|
64
|
+
if (typeof data.access_token !== 'string') {
|
|
65
|
+
return { ok: false, message: 'GitHub OAuth response missing access_token' };
|
|
66
|
+
}
|
|
67
|
+
return { ok: true, token: data.access_token };
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
return { ok: false, message: `GitHub OAuth exchange failed: ${err.message}` };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async authenticate(credentials) {
|
|
74
|
+
let token = credentials.token;
|
|
75
|
+
// If a GitHub OAuth code is provided, exchange it for an access token first
|
|
76
|
+
if (!token && credentials.githubCode) {
|
|
77
|
+
const exchange = await this.exchangeCode(credentials.githubCode);
|
|
78
|
+
if (!exchange.ok) {
|
|
79
|
+
return { ok: false, message: exchange.message };
|
|
80
|
+
}
|
|
81
|
+
token = exchange.token;
|
|
82
|
+
}
|
|
83
|
+
if (!token) {
|
|
84
|
+
return { ok: false, message: 'Token required for GitHub auth' };
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
const res = await this.fetcher('https://api.github.com/user', {
|
|
88
|
+
headers: {
|
|
89
|
+
Authorization: `Bearer ${token}`,
|
|
90
|
+
Accept: 'application/vnd.github+json',
|
|
91
|
+
'User-Agent': 'kraki-head',
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
if (!res.ok) {
|
|
95
|
+
getLogger().warn('GitHub auth failed', { status: res.status, ip: credentials.ip });
|
|
96
|
+
return { ok: false, message: `GitHub API returned ${res.status}` };
|
|
97
|
+
}
|
|
98
|
+
const data = await res.json();
|
|
99
|
+
if (!data.id || !data.login) {
|
|
100
|
+
return { ok: false, message: 'Unexpected GitHub API response' };
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
ok: true,
|
|
104
|
+
user: { id: String(data.id), login: String(data.login), provider: 'github', email: typeof data.email === 'string' ? data.email : undefined },
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
return { ok: false, message: `GitHub API request failed: ${err.message}` };
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Open provider for self-hosted mode. No validation — all connections accepted.
|
|
114
|
+
* Optionally accepts a shared key so tentacles, head, and apps can use the same
|
|
115
|
+
* secret to form a trusted group without full OAuth.
|
|
116
|
+
*
|
|
117
|
+
* TODO: Support multi-user on self-hosted without OAuth (e.g., local user registry)
|
|
118
|
+
*/
|
|
119
|
+
export class OpenAuthProvider {
|
|
120
|
+
name = 'open';
|
|
121
|
+
sharedKey;
|
|
122
|
+
constructor(sharedKey) {
|
|
123
|
+
this.sharedKey = sharedKey;
|
|
124
|
+
}
|
|
125
|
+
async authenticate(credentials) {
|
|
126
|
+
if (this.sharedKey) {
|
|
127
|
+
if (!credentials.token || !safeEqual(credentials.token, this.sharedKey)) {
|
|
128
|
+
getLogger().warn('Open auth failed: invalid shared key', { ip: credentials.ip });
|
|
129
|
+
return { ok: false, message: 'Invalid shared key' };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
ok: true,
|
|
134
|
+
user: { id: 'local', login: 'local', provider: 'open' },
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Static API key provider for self-hosted relays that want basic protection.
|
|
140
|
+
* Set a key in env/config, devices must send matching key.
|
|
141
|
+
*/
|
|
142
|
+
export class ApiKeyAuthProvider {
|
|
143
|
+
name = 'apikey';
|
|
144
|
+
validKey;
|
|
145
|
+
constructor(validKey) {
|
|
146
|
+
this.validKey = validKey;
|
|
147
|
+
}
|
|
148
|
+
async authenticate(credentials) {
|
|
149
|
+
if (!credentials.token || !safeEqual(credentials.token, this.validKey)) {
|
|
150
|
+
getLogger().warn('API key auth failed', { ip: credentials.ip });
|
|
151
|
+
return { ok: false, message: 'Invalid API key' };
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
ok: true,
|
|
155
|
+
user: { id: 'apikey-user', login: 'apikey-user', provider: 'apikey' },
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// --- Auth throttle ---
|
|
160
|
+
/**
|
|
161
|
+
* Simple per-IP throttle for auth attempts.
|
|
162
|
+
* Wraps any AuthProvider and rejects after maxAttempts failures within windowMs.
|
|
163
|
+
*/
|
|
164
|
+
export class ThrottledAuthProvider {
|
|
165
|
+
get name() { return this.inner.name; }
|
|
166
|
+
inner;
|
|
167
|
+
maxAttempts;
|
|
168
|
+
windowMs;
|
|
169
|
+
failures = new Map();
|
|
170
|
+
constructor(inner, maxAttempts = 5, windowMs = 60_000) {
|
|
171
|
+
this.inner = inner;
|
|
172
|
+
this.maxAttempts = maxAttempts;
|
|
173
|
+
this.windowMs = windowMs;
|
|
174
|
+
}
|
|
175
|
+
async authenticate(credentials) {
|
|
176
|
+
const ip = credentials.ip ?? 'unknown';
|
|
177
|
+
const now = Date.now();
|
|
178
|
+
const record = this.failures.get(ip);
|
|
179
|
+
if (record) {
|
|
180
|
+
if (now - record.firstAt > this.windowMs) {
|
|
181
|
+
this.failures.delete(ip);
|
|
182
|
+
}
|
|
183
|
+
else if (record.count >= this.maxAttempts) {
|
|
184
|
+
getLogger().warn('Auth throttled', { ip, attempts: record.count });
|
|
185
|
+
return { ok: false, message: 'Too many auth attempts. Try again later.' };
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const result = await this.inner.authenticate(credentials);
|
|
189
|
+
if (!result.ok) {
|
|
190
|
+
const existing = this.failures.get(ip);
|
|
191
|
+
if (existing) {
|
|
192
|
+
existing.count++;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
this.failures.set(ip, { count: 1, firstAt: now });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
this.failures.delete(ip);
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
/** Remove stale entries to prevent memory growth on long-running servers. */
|
|
204
|
+
cleanup() {
|
|
205
|
+
const now = Date.now();
|
|
206
|
+
for (const [ip, record] of this.failures) {
|
|
207
|
+
if (now - record.firstAt > this.windowMs) {
|
|
208
|
+
this.failures.delete(ip);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=auth.js.map
|
package/dist/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,uDAAuD;AACvD,+DAA+D;AAE/D,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,yEAAyE;AACzE,SAAS,SAAS,CAAC,CAAS,EAAE,CAAS;IACrC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACxC,OAAO,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACzD,CAAC;AA0CD,6BAA6B;AAE7B;;;;GAIG;AACH,MAAM,OAAO,kBAAkB;IACpB,IAAI,GAAG,QAAQ,CAAC;IACjB,OAAO,CAAe;IACtB,QAAQ,CAAU;IAClB,YAAY,CAAU;IAE9B,YAAY,IAA2E;QACrF,IAAI,CAAC,OAAO,GAAG,IAAI,EAAE,OAAO,IAAI,UAAU,CAAC,KAAK,CAAC;QACjD,IAAI,CAAC,QAAQ,GAAG,IAAI,EAAE,QAAQ,CAAC;QAC/B,IAAI,CAAC,YAAY,GAAG,IAAI,EAAE,YAAY,CAAC;IACzC,CAAC;IAED,gDAAgD;IAChD,IAAI,eAAe;QACjB,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,YAAY,CAAC,CAAC;IAChD,CAAC;IAED,kEAAkE;IAClE,WAAW;QACT,OAAO,IAAI,CA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GAAG,EAAE,CAAC,CAAC;YACpD,CAAC;QACH,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAC3B,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,6EAA6E;IAC7E,OAAO;QACL,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,KAAK,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YACzC,IAAI,GAAG,GAAG,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACzC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;YAC3B,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { DeviceSummary, SessionSummary, DeviceRole, DeviceKind, DeviceCapabilities } from '@kraki/protocol';
|
|
2
|
+
import type { AuthUser } from './auth.js';
|
|
3
|
+
import { Storage } from './storage.js';
|
|
4
|
+
export interface ConnectedDevice {
|
|
5
|
+
deviceId: string;
|
|
6
|
+
channelId: string;
|
|
7
|
+
name: string;
|
|
8
|
+
role: DeviceRole;
|
|
9
|
+
kind?: DeviceKind;
|
|
10
|
+
publicKey?: string;
|
|
11
|
+
encryptionKey?: string;
|
|
12
|
+
capabilities?: DeviceCapabilities;
|
|
13
|
+
send: (data: string) => void;
|
|
14
|
+
}
|
|
15
|
+
export interface RegisterDeviceInput {
|
|
16
|
+
channelId: string;
|
|
17
|
+
name: string;
|
|
18
|
+
role: DeviceRole;
|
|
19
|
+
send: (data: string) => void;
|
|
20
|
+
kind?: DeviceKind;
|
|
21
|
+
publicKey?: string;
|
|
22
|
+
encryptionKey?: string;
|
|
23
|
+
capabilities?: DeviceCapabilities;
|
|
24
|
+
/** Client-provided stable device ID for reconnection. If omitted, a new one is generated. */
|
|
25
|
+
clientDeviceId?: string;
|
|
26
|
+
}
|
|
27
|
+
export interface SessionMeta {
|
|
28
|
+
agent: string;
|
|
29
|
+
model?: string;
|
|
30
|
+
}
|
|
31
|
+
export declare class ChannelManager {
|
|
32
|
+
private storage;
|
|
33
|
+
/** In-memory map of connected devices by deviceId */
|
|
34
|
+
private connections;
|
|
35
|
+
/** Session tracking: sessionId → SessionRecord */
|
|
36
|
+
private sessions;
|
|
37
|
+
/** Reverse index: deviceId → Set of sessionIds (for fast cleanup) */
|
|
38
|
+
private deviceSessions;
|
|
39
|
+
/** Per-channel seq counter */
|
|
40
|
+
private seqCounters;
|
|
41
|
+
constructor(storage: Storage);
|
|
42
|
+
/**
|
|
43
|
+
* Get or create a channel for a user. Returns the channel ID.
|
|
44
|
+
*/
|
|
45
|
+
getOrCreateChannel(user: AuthUser): string;
|
|
46
|
+
/**
|
|
47
|
+
* Register a device connection. Returns the assigned deviceId.
|
|
48
|
+
* If clientDeviceId is provided and matches an existing device, reuses it (no ghost).
|
|
49
|
+
*/
|
|
50
|
+
registerDevice(input: RegisterDeviceInput): string;
|
|
51
|
+
/**
|
|
52
|
+
* Remove a device connection (on disconnect).
|
|
53
|
+
*/
|
|
54
|
+
disconnectDevice(deviceId: string): ConnectedDevice | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Register that a tentacle owns a session, with metadata.
|
|
57
|
+
*/
|
|
58
|
+
registerSession(sessionId: string, deviceId: string, meta: SessionMeta): void;
|
|
59
|
+
/**
|
|
60
|
+
* Get the tentacle deviceId that owns a session.
|
|
61
|
+
* Checks in-memory first, then falls back to persistent storage.
|
|
62
|
+
*/
|
|
63
|
+
getSessionOwner(sessionId: string): string | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Get the next seq number for a channel (monotonically increasing).
|
|
66
|
+
*/
|
|
67
|
+
nextSeq(channelId: string): number;
|
|
68
|
+
/**
|
|
69
|
+
* Get all connected devices on a channel.
|
|
70
|
+
*/
|
|
71
|
+
getConnectedDevices(channelId: string): ConnectedDevice[];
|
|
72
|
+
/**
|
|
73
|
+
* Get connected devices filtered by role.
|
|
74
|
+
*/
|
|
75
|
+
getConnectedByRole(channelId: string, role: DeviceRole): ConnectedDevice[];
|
|
76
|
+
/**
|
|
77
|
+
* Get device summaries for a channel (includes offline devices from storage).
|
|
78
|
+
*/
|
|
79
|
+
getDeviceSummaries(channelId: string): DeviceSummary[];
|
|
80
|
+
/**
|
|
81
|
+
* Get session summaries for a channel.
|
|
82
|
+
*/
|
|
83
|
+
getSessionSummaries(channelId: string): SessionSummary[];
|
|
84
|
+
/**
|
|
85
|
+
* Get a specific connected device.
|
|
86
|
+
*/
|
|
87
|
+
getConnection(deviceId: string): ConnectedDevice | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* Get the underlying storage instance.
|
|
90
|
+
*/
|
|
91
|
+
getStorage(): Storage;
|
|
92
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { v4 as uuid } from 'uuid';
|
|
2
|
+
export class ChannelManager {
|
|
3
|
+
storage;
|
|
4
|
+
/** In-memory map of connected devices by deviceId */
|
|
5
|
+
connections = new Map();
|
|
6
|
+
/** Session tracking: sessionId → SessionRecord */
|
|
7
|
+
sessions = new Map();
|
|
8
|
+
/** Reverse index: deviceId → Set of sessionIds (for fast cleanup) */
|
|
9
|
+
deviceSessions = new Map();
|
|
10
|
+
/** Per-channel seq counter */
|
|
11
|
+
seqCounters = new Map();
|
|
12
|
+
constructor(storage) {
|
|
13
|
+
this.storage = storage;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Get or create a channel for a user. Returns the channel ID.
|
|
17
|
+
*/
|
|
18
|
+
getOrCreateChannel(user) {
|
|
19
|
+
this.storage.upsertUser(user.id, user.login, user.provider, user.email);
|
|
20
|
+
const existing = this.storage.getChannelByOwner(user.id);
|
|
21
|
+
if (existing)
|
|
22
|
+
return existing.id;
|
|
23
|
+
const channelId = `ch_${uuid().slice(0, 12)}`;
|
|
24
|
+
this.storage.createChannel(channelId, user.id);
|
|
25
|
+
return channelId;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Register a device connection. Returns the assigned deviceId.
|
|
29
|
+
* If clientDeviceId is provided and matches an existing device, reuses it (no ghost).
|
|
30
|
+
*/
|
|
31
|
+
registerDevice(input) {
|
|
32
|
+
const deviceId = input.clientDeviceId ?? `dev_${uuid().slice(0, 12)}`;
|
|
33
|
+
this.storage.upsertDevice(deviceId, input.channelId, input.name, input.role, input.kind, input.publicKey, input.encryptionKey);
|
|
34
|
+
this.connections.set(deviceId, {
|
|
35
|
+
deviceId,
|
|
36
|
+
channelId: input.channelId,
|
|
37
|
+
name: input.name,
|
|
38
|
+
role: input.role,
|
|
39
|
+
kind: input.kind,
|
|
40
|
+
publicKey: input.publicKey,
|
|
41
|
+
encryptionKey: input.encryptionKey,
|
|
42
|
+
capabilities: input.capabilities,
|
|
43
|
+
send: input.send,
|
|
44
|
+
});
|
|
45
|
+
return deviceId;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Remove a device connection (on disconnect).
|
|
49
|
+
*/
|
|
50
|
+
disconnectDevice(deviceId) {
|
|
51
|
+
const device = this.connections.get(deviceId);
|
|
52
|
+
if (device) {
|
|
53
|
+
this.connections.delete(deviceId);
|
|
54
|
+
this.deviceSessions.delete(deviceId);
|
|
55
|
+
}
|
|
56
|
+
return device;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Register that a tentacle owns a session, with metadata.
|
|
60
|
+
*/
|
|
61
|
+
registerSession(sessionId, deviceId, meta) {
|
|
62
|
+
this.sessions.set(sessionId, { deviceId, meta });
|
|
63
|
+
if (!this.deviceSessions.has(deviceId)) {
|
|
64
|
+
this.deviceSessions.set(deviceId, new Set());
|
|
65
|
+
}
|
|
66
|
+
this.deviceSessions.get(deviceId).add(sessionId);
|
|
67
|
+
// Persist to storage for durability across restarts
|
|
68
|
+
const device = this.connections.get(deviceId);
|
|
69
|
+
const channelId = device?.channelId ?? this.storage.getDevice(deviceId)?.channelId;
|
|
70
|
+
if (channelId) {
|
|
71
|
+
this.storage.upsertSession(sessionId, channelId, deviceId, meta.agent, meta.model);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get the tentacle deviceId that owns a session.
|
|
76
|
+
* Checks in-memory first, then falls back to persistent storage.
|
|
77
|
+
*/
|
|
78
|
+
getSessionOwner(sessionId) {
|
|
79
|
+
const inMemory = this.sessions.get(sessionId);
|
|
80
|
+
if (inMemory)
|
|
81
|
+
return inMemory.deviceId;
|
|
82
|
+
// Check persistent storage (session may have survived a head restart)
|
|
83
|
+
const stored = this.storage.getSessionById(sessionId);
|
|
84
|
+
if (stored) {
|
|
85
|
+
// Restore to in-memory map
|
|
86
|
+
this.sessions.set(sessionId, {
|
|
87
|
+
deviceId: stored.deviceId,
|
|
88
|
+
meta: { agent: stored.agent, model: stored.model ?? undefined },
|
|
89
|
+
});
|
|
90
|
+
return stored.deviceId;
|
|
91
|
+
}
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get the next seq number for a channel (monotonically increasing).
|
|
96
|
+
*/
|
|
97
|
+
nextSeq(channelId) {
|
|
98
|
+
if (!this.seqCounters.has(channelId)) {
|
|
99
|
+
const maxSeq = this.storage.getMaxSeq(channelId);
|
|
100
|
+
this.seqCounters.set(channelId, maxSeq);
|
|
101
|
+
}
|
|
102
|
+
const next = this.seqCounters.get(channelId) + 1;
|
|
103
|
+
this.seqCounters.set(channelId, next);
|
|
104
|
+
return next;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Get all connected devices on a channel.
|
|
108
|
+
*/
|
|
109
|
+
getConnectedDevices(channelId) {
|
|
110
|
+
// TODO: Add channelId → Set<deviceId> index if this becomes a bottleneck
|
|
111
|
+
return Array.from(this.connections.values()).filter(d => d.channelId === channelId);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get connected devices filtered by role.
|
|
115
|
+
*/
|
|
116
|
+
getConnectedByRole(channelId, role) {
|
|
117
|
+
return this.getConnectedDevices(channelId).filter(d => d.role === role);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Get device summaries for a channel (includes offline devices from storage).
|
|
121
|
+
*/
|
|
122
|
+
getDeviceSummaries(channelId) {
|
|
123
|
+
const stored = this.storage.getDevicesByChannel(channelId);
|
|
124
|
+
return stored.map(d => {
|
|
125
|
+
const conn = this.connections.get(d.id);
|
|
126
|
+
return {
|
|
127
|
+
id: d.id,
|
|
128
|
+
name: d.name,
|
|
129
|
+
role: d.role,
|
|
130
|
+
kind: d.kind ?? undefined,
|
|
131
|
+
publicKey: d.publicKey ?? undefined,
|
|
132
|
+
encryptionKey: conn?.encryptionKey ?? d.encryptionKey ?? undefined,
|
|
133
|
+
online: !!conn,
|
|
134
|
+
capabilities: conn?.capabilities,
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Get session summaries for a channel.
|
|
140
|
+
*/
|
|
141
|
+
getSessionSummaries(channelId) {
|
|
142
|
+
// Merge in-memory sessions with persisted sessions from storage
|
|
143
|
+
const seen = new Set();
|
|
144
|
+
const summaries = [];
|
|
145
|
+
// In-memory sessions first (most up-to-date)
|
|
146
|
+
for (const [sessionId, record] of this.sessions) {
|
|
147
|
+
const conn = this.connections.get(record.deviceId);
|
|
148
|
+
const channelMatch = conn
|
|
149
|
+
? conn.channelId === channelId
|
|
150
|
+
: this.storage.getDevice(record.deviceId)?.channelId === channelId;
|
|
151
|
+
if (channelMatch) {
|
|
152
|
+
seen.add(sessionId);
|
|
153
|
+
let deviceName = conn?.name;
|
|
154
|
+
if (!deviceName) {
|
|
155
|
+
const stored = this.storage.getDevice(record.deviceId);
|
|
156
|
+
deviceName = stored?.name ?? record.deviceId;
|
|
157
|
+
}
|
|
158
|
+
summaries.push({
|
|
159
|
+
id: sessionId,
|
|
160
|
+
deviceId: record.deviceId,
|
|
161
|
+
deviceName,
|
|
162
|
+
agent: record.meta.agent,
|
|
163
|
+
model: record.meta.model,
|
|
164
|
+
messageCount: 0,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// Add persisted sessions not already in memory (survives head restart)
|
|
169
|
+
for (const stored of this.storage.getSessionsByChannel(channelId)) {
|
|
170
|
+
if (!seen.has(stored.id)) {
|
|
171
|
+
const device = this.storage.getDevice(stored.deviceId);
|
|
172
|
+
summaries.push({
|
|
173
|
+
id: stored.id,
|
|
174
|
+
deviceId: stored.deviceId,
|
|
175
|
+
deviceName: device?.name ?? stored.deviceId,
|
|
176
|
+
agent: stored.agent,
|
|
177
|
+
model: stored.model ?? undefined,
|
|
178
|
+
messageCount: 0,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return summaries;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Get a specific connected device.
|
|
186
|
+
*/
|
|
187
|
+
getConnection(deviceId) {
|
|
188
|
+
return this.connections.get(deviceId);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Get the underlying storage instance.
|
|
192
|
+
*/
|
|
193
|
+
getStorage() {
|
|
194
|
+
return this.storage;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
//# sourceMappingURL=channel-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel-manager.js","sourceRoot":"","sources":["../src/channel-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAwClC,MAAM,OAAO,cAAc;IACjB,OAAO,CAAU;IACzB,qDAAqD;IAC7C,WAAW,GAAG,IAAI,GAAG,EAA2B,CAAC;IACzD,kDAAkD;IAC1C,QAAQ,GAAG,IAAI,GAAG,EAAyB,CAAC;IACpD,qEAAqE;IAC7D,cAAc,GAAG,IAAI,GAAG,EAA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MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;gBACvD,SAAS,CAAC,IAAI,CAAC;oBACb,EAAE,EAAE,MAAM,CAAC,EAAE;oBACb,QAAQ,EAAE,MAAM,CAAC,QAAQ;oBACzB,UAAU,EAAE,MAAM,EAAE,IAAI,IAAI,MAAM,CAAC,QAAQ;oBAC3C,KAAK,EAAE,MAAM,CAAC,KAAK;oBACnB,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,SAAS;oBAChC,YAAY,EAAE,CAAC;iBAChB,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;OAEG;IACH,aAAa,CAAC,QAAgB;QAC5B,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACxC,CAAC;IAED;;OAEG;IACH,UAAU;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF"}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* kraki-relay — CLI entry point for the Kraki relay server.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* npx kraki-relay
|
|
7
|
+
* npx kraki-relay --port 8080
|
|
8
|
+
* npx kraki-relay --auth github --e2e true
|
|
9
|
+
*
|
|
10
|
+
* Environment variables:
|
|
11
|
+
* PORT Server port (default: 4000)
|
|
12
|
+
* DB_PATH SQLite database path (default: kraki-head.db)
|
|
13
|
+
* AUTH_MODE Auth mode: open | github | apikey (default: open)
|
|
14
|
+
* E2E_MODE Enable E2E encryption: true | false (default: false)
|
|
15
|
+
* API_KEY API key for apikey auth mode
|
|
16
|
+
* GITHUB_CLIENT_ID GitHub OAuth App client ID (for web login)
|
|
17
|
+
* GITHUB_CLIENT_SECRET GitHub OAuth App client secret (for web login)
|
|
18
|
+
* LOG_LEVEL Log level: debug | info | warn | error (default: info)
|
|
19
|
+
* LOG_PATH Log file path (optional)
|
|
20
|
+
*/
|
|
21
|
+
export {};
|