/
nestdevicemw.mjs
199 lines (176 loc) · 5.32 KB
/
nestdevicemw.mjs
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
import ndc from "node-datachannel";
import jose from "node-jose";
import crypto from "crypto";
import fs from "fs";
import tester from "./test_bandwidth_node.mjs";
// Read config and keyblob
const keys = JSON.parse(fs.readFileSync("./keys.json"));
const cfg = JSON.parse(fs.readFileSync("./config.json"));
const SESSION_TIMEOUT = 10 * 1000; // in MS
let keystore = {}; // Filled in init()
let pubkey_ksy0 = {}; // Filled in init()
let signkey = {}; // Filled in init()
let signer = {}; // Filled in init()
// Sessions
const sessions = {};
function new_session(){ // => UUID
const uuid = crypto.randomUUID();
let state = "pre-init";
let sdpwaiter = false;
let sdperrorwaiter = false;
let sdpfinalized = false;
let currentsdp = false;
let currentcandidates = [];
let conn = false;
let dc = false;
if(sessions[uuid]){
throw "???";
}
function iceresult(){
return {
d: currentsdp,
c: currentcandidates,
ident: uuid
};
}
function getsdp(){
return new Promise((res, rej) => {
if(sessions[uuid]){
if(sdpfinalized){
res(iceresult);
}else{
sdpwaiter = function(result){
sdpwaiter = false;
sdperrorwaiter = false;
res(result);
};
sdperrorwaiter = function(exp){
sdpwaiter = false;
sdperrorwaiter = false;
rej(exp);
};
}
}else{
res(false);
}
});
}
function delete_session(){
if(sdperrorwaiter){
sdperrorwaiter("session deleted");
}
}
function begin_session(){
const opts = {
iceServers: ["stun:stun.l.google.com:19302"]
};
conn = new ndc.PeerConnection(uuid, opts);
conn.onLocalDescription((sdp, type) => {
currentsdp = sdp;
});
conn.onLocalCandidate((candidate, mid) => {
currentcandidates.push({c: candidate, m: mid});
});
conn.onGatheringStateChange((state) => {
if(sessions[uuid]){
if(state == "complete"){
sdpfinalized = true;
if(sdpwaiter){
sdpwaiter(iceresult());
sdpwaiter = false;
sdperrorwaiter = false;
}
}
}
});
// Activate
dc = conn.createDataChannel("default");
tester.dc_tester(dc);
}
async function handle_peer_ice(input){
const sdp = input.s;
console.log(sdp);
conn.setRemoteDescription(sdp, "answer");
}
async function request(input){ // => res
console.log("Con request", input);
switch(state) {
case "pre-init":
if(input.req == "new-connection"){
state = "wait-peer-ice";
begin_session();
return await getsdp();
}else{
throw "???";
}
case "wait-peer-ice":
await handle_peer_ice(input);
return {
res: "done",
ident: uuid
};
default:
throw "???";
}
}
sessions[uuid] = {
begin_session: begin_session,
delete_session: delete_session,
request: request
};
setTimeout(delete_session, SESSION_TIMEOUT);
return uuid;
}
async function init(){
keystore = await jose.JWK.asKeyStore(keys);
pubkey_ksy0 = keystore.all({kty: "RSA"})[0];
signkey = keystore.all({kty: "EC"})[0];
signer = async function(obj){
const res = await jose.JWS.createSign({format: "compact"}, signkey).
update(JSON.stringify(obj)).
final();
return res;
}
}
async function mw_ksy0(ctx, next){
const blob = await signer(pubkey_ksy0);
console.log("ksy0", blob);
return ctx.text(blob);
}
async function mw_con(ctx, next){
const opts = {
algorithms: ["RSA*", "A*"]
};
try {
const body0 = await ctx.req.text();
console.log("Con reqbody", body0);
const body1 = await jose.JWE.createDecrypt(keystore, opts).
decrypt(body0);
const body = JSON.parse(body1.payload);
console.log("Input(con)", body);
if(body.req && body.req == "new-connection"){
const ident = new_session();
console.log("new", ident, sessions[ident]);
const res = await sessions[ident].request(body);
const out = await signer(res);
return ctx.text(out);
}else if(sessions[body.ident]){
const res = await sessions[body.ident].request(body);
const out = await signer(res);
return ctx.text(out);
}else{
ctx.status(404);
return ctx.text("Specified ident was removed already.");
}
}catch(e){
console.log("Err(con)", e);
ctx.status(500);
return ctx.text("Something wrong");
}
}
//ndc.initLogger("Debug");
init();
export default {
ksy0: mw_ksy0,
con: mw_con
};