-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
259 lines (256 loc) · 10.5 KB
/
stack.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
function newStack(worker, workerImageName, stackWorker, stackImageName) {
let p2vbuf = {
buf: new Uint8Array(0) // proxy => vm
};
let v2pbuf = {
buf: new Uint8Array(0) // vm => proxy
};
var proxyConn = {
sendbuf: p2vbuf,
recvbuf: v2pbuf
};
var vmConn = {
sendbuf: v2pbuf,
recvbuf: p2vbuf
}
var proxyShared = new SharedArrayBuffer(12 + 4096);
var certbuf = {
buf: new Uint8Array(0),
done: false
}
stackWorker.onmessage = connect("proxy", proxyShared, proxyConn, certbuf);
stackWorker.postMessage({type: "init", buf: proxyShared, imagename: stackImageName});
var vmShared = new SharedArrayBuffer(12 + 4096);
worker.postMessage({type: "init", buf: vmShared, imagename: workerImageName});
return connect("vm", vmShared, vmConn, certbuf);
}
function connect(name, shared, conn, certbuf) {
var streamCtrl = new Int32Array(shared, 0, 1);
var streamStatus = new Int32Array(shared, 4, 1);
var streamLen = new Int32Array(shared, 8, 1);
var streamData = new Uint8Array(shared, 12);
var sendbuf = conn.sendbuf;
var recvbuf = conn.recvbuf;
let accepted = false;
var httpConnections = {};
var curID = 0;
var maxID = 0x7FFFFFFF; // storable in streamStatus(signed 32bits)
function getID() {
var startID = curID;
while (true) {
if (httpConnections[curID] == undefined) {
return curID;
}
if (curID >= maxID) {
curID = 0;
} else {
curID++;
}
if (curID == startID) {
return -1; // exhausted
}
}
return curID;
}
function serveData(data, len) {
var length = len;
if (length > streamData.byteLength)
length = streamData.byteLength;
if (length > data.byteLength)
length = data.byteLength
var buf = data.slice(0, length);
var remain = data.slice(length, data.byteLength);
streamLen[0] = buf.byteLength;
streamData.set(buf, 0);
return remain;
}
return function(msg){
const req_ = msg.data;
if (typeof req_ == "object" && req_.type) {
switch (req_.type) {
case "accept":
accepted = true;
streamData[0] = 1; // opened
streamStatus[0] = 0;
break;
case "send":
if (!accepted) {
console.log(name + ":" + "cannot send to unaccepted socket");
streamStatus[0] = -1;
break;
}
sendbuf.buf = appendData(sendbuf.buf, req_.buf);
streamStatus[0] = 0;
break;
case "recv":
if (!accepted) {
console.log(name + ":" + "cannot recv from unaccepted socket");
streamStatus[0] = -1;
break;
}
recvbuf.buf = serveData(recvbuf.buf, req_.len);
streamStatus[0] = 0;
break;
case "recv-is-readable":
var recvbufP = recvbuf.buf;
if (recvbufP.byteLength > 0) {
streamData[0] = 1; // ready for reading
} else {
if ((req_.timeout != undefined) && (req_.timeout > 0)) {
if (this.timeoutHandler) {
clearTimeout(this.timeoutHandler);
this.timeoutHandler = null;
}
this.timeoutHandler = setTimeout(() => {
if (this.timeoutHandler) {
clearTimeout(this.timeoutHandler);
this.timeoutHandler = null;
}
if (recvbuf.buf.byteLength > 0) {
streamData[0] = 1; // ready for reading
} else {
streamData[0] = 0; // timeout
}
streamStatus[0] = 0;
Atomics.store(streamCtrl, 0, 1);
Atomics.notify(streamCtrl, 0);
}, req_.timeout * 1000);
return;
}
streamData[0] = 0; // timeout
}
streamStatus[0] = 0;
break;
case "http_send":
var reqObj = JSON.parse(new TextDecoder().decode(req_.req));
reqObj.mode = "cors";
reqObj.credentials = "omit";
if (reqObj.headers && reqObj.headers["User-Agent"] != "") {
delete reqObj.headers["User-Agent"]; // Browser will add its own value.
}
var reqID = getID();
if (reqID < 0) {
console.log(name + ":" + "failed to get id");
streamStatus[0] = -1;
break;
}
var connObj = {
address: new TextDecoder().decode(req_.address),
request: reqObj,
requestSent: false,
reqBodybuf: new Uint8Array(0),
reqBodyEOF: false,
};
httpConnections[reqID] = connObj;
streamStatus[0] = reqID;
break;
case "http_writebody":
httpConnections[req_.id].reqBodybuf = appendData(httpConnections[req_.id].reqBodybuf, req_.body)
httpConnections[req_.id].reqBodyEOF = req_.isEOF;
streamStatus[0] = 0;
if (req_.isEOF && !httpConnections[req_.id].requestSent) {
httpConnections[req_.id].requestSent = true;
var connObj = httpConnections[req_.id];
if ((connObj.request.method != "HEAD") && (connObj.request.method != "GET")) {
connObj.request.body = connObj.reqBodybuf;
}
fetch(connObj.address, connObj.request).then((resp) => {
connObj.response = new TextEncoder().encode(JSON.stringify({
bodyUsed: resp.bodyUsed,
headers: resp.headers,
redirected: resp.redirected,
status: resp.status,
statusText: resp.statusText,
type: resp.type,
url: resp.url
})),
connObj.done = false;
connObj.respBodybuf = new Uint8Array(0);
if (resp.ok) {
resp.arrayBuffer().then((data) => {
connObj.respBodybuf = new Uint8Array(data);
connObj.done = true;
}).catch((error) => {
connObj.respBodybuf = new Uint8Array(0);
connObj.done = true;
console.log("failed to fetch body: " + error);
});
} else {
connObj.done = true;
}
}).catch((error) => {
connObj.response = new TextEncoder().encode(JSON.stringify({
status: 503,
statusText: "Service Unavailable",
}))
connObj.respBodybuf = new Uint8Array(0);
connObj.done = true;
});
}
break;
case "http_isreadable":
if ((httpConnections[req_.id] != undefined) && (httpConnections[req_.id].response != undefined)) {
streamData[0] = 1; // ready for reading
} else {
streamData[0] = 0; // nothing to read
}
streamStatus[0] = 0;
break;
case "http_recv":
if ((httpConnections[req_.id] == undefined) || (httpConnections[req_.id].response == undefined)) {
console.log(name + ":" + "response is not available");
streamStatus[0] = -1;
break;
}
httpConnections[req_.id].response = serveData(httpConnections[req_.id].response, req_.len);
streamStatus[0] = 0;
if (httpConnections[req_.id].response.byteLength == 0) {
streamStatus[0] = 1; // isEOF
}
break;
case "http_readbody":
if ((httpConnections[req_.id] == undefined) || (httpConnections[req_.id].response == undefined)) {
console.log(name + ":" + "response body is not available");
streamStatus[0] = -1;
break;
}
httpConnections[req_.id].respBodybuf = serveData(httpConnections[req_.id].respBodybuf, req_.len);
streamStatus[0] = 0;
if ((httpConnections[req_.id].done) && (httpConnections[req_.id].respBodybuf.byteLength == 0)) {
streamStatus[0] = 1;
delete httpConnections[req_.id]; // connection done
}
break;
case "send_cert":
certbuf.buf = appendData(certbuf.buf, req_.buf);
certbuf.done = true;
streamStatus[0] = 0;
break;
case "recv_cert":
if (!certbuf.done) {
streamStatus[0] = -1;
break;
}
certbuf.buf = serveData(certbuf.buf, req_.len);
streamStatus[0] = 0;
if (certbuf.buf.byteLength == 0) {
streamStatus[0] = 1; // isEOF
}
break;
default:
console.log(name + ":" + "unknown request: " + req_.type)
return;
}
Atomics.store(streamCtrl, 0, 1);
Atomics.notify(streamCtrl, 0);
} else {
console.log("UNKNOWN MSG " + msg);
}
}
}
function appendData(data1, data2) {
buf2 = new Uint8Array(data1.byteLength + data2.byteLength);
buf2.set(new Uint8Array(data1), 0);
buf2.set(new Uint8Array(data2), data1.byteLength);
return buf2;
}