-
Notifications
You must be signed in to change notification settings - Fork 0
/
party.js
310 lines (279 loc) · 10.6 KB
/
party.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// This module exports a function that makes WebSeif parties.
// A party has an identity (its keypair) and knows about other parties
// (its acquaintences). This information is persisted in its store. A party is
// also supplied with a transport that is used as a basis for communication
// with other parties.
/*jslint browser, node, deno, global */
import elliptic from "./elliptic.js";
import protocol from "./protocol.js";
function do_nothing() {
return;
}
function party(store, transport, autogenerate_keypair = false) {
function get_keypair() {
// Read the keypair from the store, or if it is missing, generate a new one.
return store.read_keypair().then(function (keypair) {
if (keypair !== undefined) {
return keypair;
}
if (autogenerate_keypair === false) {
throw new Error("Missing keypair.");
}
if (autogenerate_keypair === true) {
// A keypair is autogenerated exactly once.
autogenerate_keypair = elliptic.generate_keypair().then(
function (keypair) {
return store.write_keypair(keypair).then(function () {
return keypair;
});
}
);
}
return autogenerate_keypair;
});
}
function get_acquaintance(petname) {
// Read an acquaintance from the store. Fail if the acquaintance is not found.
return store.read_acquaintance(petname).then(function (acquaintance) {
if (acquaintance === undefined) {
throw new Error("Not acquainted with " + petname + ".");
}
return acquaintance;
});
}
function connect({
petname,
on_open = do_nothing,
on_message = do_nothing,
on_close = do_nothing,
hello_value,
connection_info
}) {
let protocol_close;
function destroy(reason) {
if (protocol_close !== undefined) {
protocol_close(reason);
protocol_close = undefined;
}
if (on_close !== undefined) {
on_close(undefined, reason);
on_close = undefined;
}
}
Promise.all([
get_keypair(),
get_acquaintance(petname)
]).then(function ([keypair, acquaintance]) {
// Proceed with the connection only if 'close' has not been called.
if (on_close === undefined) {
return;
}
protocol_close = protocol.connect({
keypair,
transport_connect: transport.connect,
address: acquaintance.address,
remote_public_key: acquaintance.public_key,
hello_value,
connection_info,
on_open,
on_message,
on_close: function (
connection,
reason,
address,
public_key,
permanent
) {
if (public_key !== undefined) {
// The connection is currently being redirected. If this is a permanent
// redirect, update the store.
reason = "redirected";
if (permanent) {
store.add_acquaintance({
petname,
address,
public_key
}).catch(
destroy
);
}
}
return on_close(connection, reason);
}
});
}).catch(
destroy
);
return function close(reason) {
on_close = undefined;
return destroy(reason);
};
}
function listen({
address,
on_open = do_nothing,
on_message = do_nothing,
on_close = do_nothing
}) {
let protocol_stop;
let connection_weakmap = new WeakMap();
function destroy(reason) {
if (protocol_stop !== undefined) {
protocol_stop(reason);
protocol_stop = undefined;
}
if (on_close !== undefined) {
on_close(undefined, reason);
on_close = undefined;
}
}
function swizzle(callback) {
// Connection objects provided by the protocol are not quite the same as those
// provided by the party. The difference is in the 'redirect' method, which
// takes an address instead of a petname.
// Our strategy is to modify 'on_open', 'on_message' and 'on_close', mapping the
// connection objects as necessary. A WeakMap is used to ensure a 1-to-1
// mapping between protocol and party connection objects.
return function protocol_callback(protocol_connection, ...rest) {
if (protocol_connection === undefined) {
return callback(undefined, ...rest);
}
let connection = connection_weakmap.get(protocol_connection);
if (connection === undefined) {
connection = Object.freeze({
send: protocol_connection.send,
status_send: protocol_connection.status_send,
close: protocol_connection.close,
redirect(petname, permanent, redirect_context) {
get_acquaintance(
petname
).then(function (acquaintance) {
protocol_connection.redirect(
acquaintance.address,
acquaintance.public_key,
permanent,
redirect_context
);
}).catch(
destroy
);
}
});
connection_weakmap.set(protocol_connection, connection);
}
return callback(connection, ...rest);
};
}
get_keypair().then(function (keypair) {
// Proceed with listening only if 'stop' has not been called.
if (on_close === undefined) {
return;
}
protocol_stop = protocol.listen({
keypair,
transport_listen: transport.listen,
address,
on_open: swizzle(on_open),
on_message: swizzle(on_message),
on_close: swizzle(on_close)
});
}).catch(
destroy
);
return function stop(reason) {
on_close = undefined;
return destroy(reason);
};
}
return Object.freeze({connect, listen});
}
if (import.meta.main) {
const trace = globalThis.console.log;
Promise.all(
typeof Deno === "object"
? [
import("./transport/deno_tcp_transport.js"),
import("./store/deno_filesystem_store.js")
]
: [
import("./transport/node_tcp_transport.js"),
import("./store/node_filesystem_store.js")
]
).then(function ([tcp_transport_module, filesystem_store_module]) {
const tcp_transport = tcp_transport_module.default;
const filesystem_store = filesystem_store_module.default;
const transport = tcp_transport();
const alice_store = filesystem_store("/tmp/alice", "letmein", 1000);
const alice = party(alice_store, transport, true);
const bob_store = filesystem_store("/tmp/bob", "secret1", 1000);
const bob_address = "127.0.0.1:6200";
const bob = party(bob_store, transport, false);
const carol_store = filesystem_store("/tmp/carol", "p@ssw0rd", 1000);
const carol_address = "127.0.0.1:6201";
const carol = party(carol_store, transport, false);
Promise.all([
elliptic.generate_keypair(),
elliptic.generate_keypair()
]).then(function ([bob_keypair, carol_keypair]) {
return Promise.all([
bob_store.write_keypair(bob_keypair),
carol_store.write_keypair(carol_keypair),
alice_store.add_acquaintance({
petname: "bob",
address: bob_address,
public_key: bob_keypair.publicKey
}),
bob_store.add_acquaintance({
petname: "carol",
address: carol_address,
public_key: carol_keypair.publicKey
})
]);
}).then(function () {
const stop_bob = bob.listen({
address: bob_address,
on_open(_, ...rest) {
trace("bob on_open", ...rest);
},
on_message(connection) {
connection.status_send({greeting: "It's Bob!"});
connection.redirect("carol", true);
},
on_close(_, reason) {
trace("bob on_close", reason);
}
});
const stop_carol = carol.listen({
address: carol_address,
on_open() {
trace("carol on_open");
},
on_message(connection) {
connection.status_send({greeting: "It's Carol!"});
},
on_close(_, reason) {
trace("carol on_close", reason);
}
});
const close_alice = alice.connect({
petname: "bob",
hello_value: "Hello data.",
connection_info: "Connection info.",
on_open(connection) {
trace("alice on_open");
connection.status_send({greeting: "It's Alice!"});
},
on_message(_, message) {
trace("alice on_message", message);
},
on_close(_, reason) {
trace("alice on_close", reason);
}
});
setTimeout(stop_bob, Math.random() * 2000, "Done.");
setTimeout(stop_carol, Math.random() * 2000, "Done.");
setTimeout(close_alice, Math.random() * 2000, "Done.");
});
});
}
export default Object.freeze(party);