This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
/
client.js
680 lines (578 loc) · 23.5 KB
/
client.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
function empty() { } ;
var ec2_httpclient = {
handler : null,
uri : null,
auxObj : null,
serviceURL : null,
regions : null,
elbURL : null,
accessCode : null,
secretKey : null,
timers : {},
USER_AGENT : "Elasticfox/1.7-000116",
API_VERSION : "2012-12-01",
ELB_API_VERSION : "2011-11-15",
VPN_CONFIG_PATH : "http://ec2-downloads.s3.amazonaws.com/",
getNsResolver : function() {
var client = this;
return function(prefix) {
var ns = {
's': "http://schemas.xmlsoap.org/soap/envelope/", // SOAP namespace
'ec2': "http://ec2.amazonaws.com/doc/"+client.API_VERSION+"/" // EC2 namespace, must match request version
};
return ns[prefix] || null;
}
},
setCredentials : function (accessCode, secretKey) {
this.accessCode = accessCode;
this.secretKey = secretKey;
},
setEndpoint : function (endpoint) {
if (endpoint != null) {
this.serviceURL = endpoint.url;
this.regions = endpoint.name;
this.elbURL = "https://elasticloadbalancing."+this.regions+".amazonaws.com";
}
},
newInstance : function() {
var xmlhttp;
if (typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = null;
}
}
return xmlhttp;
},
sigParamCmp : function(x, y) {
if (x[0].toLowerCase() < y[0].toLowerCase ()) {
return -1;
}
if (x[0].toLowerCase() > y[0].toLowerCase()) {
return 1;
}
return 0;
},
addZero : function(vNumber) {
return ((vNumber < 10) ? "0" : "") + vNumber;
},
formatDate : function(vDate, vFormat) {
var vDay = this.addZero(vDate.getUTCDate());
var vMonth = this.addZero(vDate.getUTCMonth()+1);
var vYearLong = this.addZero(vDate.getUTCFullYear());
var vYearShort = this.addZero(vDate.getUTCFullYear().toString().substring(3,4));
var vYear = (vFormat.indexOf("yyyy")>-1?vYearLong:vYearShort);
var vHour = this.addZero(vDate.getUTCHours());
var vMinute = this.addZero(vDate.getUTCMinutes());
var vSecond = this.addZero(vDate.getUTCSeconds());
var vDateString= vFormat.replace(/dd/g, vDay).replace(/MM/g, vMonth).replace(/y{1,4}/g, vYear);
vDateString = vDateString.replace(/hh/g, vHour).replace(/mm/g, vMinute).replace(/ss/g, vSecond);
return vDateString;
},
setEndpointURLForRegion : function(region) {
var reg = ec2ui_utils.determineRegionFromString(ec2ui_session.getActiveEndpoint().name);
log(reg + ": active region prefix");
if (reg != region) {
var newURL = null;
// Determine the region's EC2 URL
var endpointlist = ec2ui_session.getEndpoints();
region = region.toLowerCase();
for (var i = 0; i < endpointlist.length; ++i) {
var curr = endpointlist[i];
if (curr.name.indexOf(region) >= 0) {
newURL = curr.url;
break;
}
}
log(newURL + ": new URL");
if (newURL == null) {
return;
}
// Switch to the new URL
this.serviceURL = newURL;
}
},
queryEC2InRegion : function (region, action, params, objActions, isSync, reqType, callback) {
// Save the current Service URL
var oldURL = this.serviceURL;
log(oldURL + ": old URL");
this.setEndpointURLForRegion(region);
// Make the call
var toRet = this.queryEC2(action, params, objActions, isSync, reqType, callback);
// Switch back to the old URL
this.serviceURL = oldURL;
return toRet;
},
queryEC2 : function (action, params, objActions, isSync, reqType, callback) {
if (this.accessCode == null || this.accessCode == "") {
log ("No Access Code for user");
return;
}
if (this.serviceURL == null || this.serviceURL == "") {
this.setEndpoint(ec2ui_session.getActiveEndpoint());
}
var rsp = null;
while(true) {
try {
rsp = this.queryEC2Impl(action, params, objActions, isSync, reqType, callback);
if (rsp.hasErrors) {
if (!this.errorDialog(
"EC2 responded with an error for "+action,
rsp.faultCode,
rsp.requestId,
rsp.faultString)) {
break;
}
} else {
break;
}
} catch (e) {
alert ("An error occurred while calling "+action+"\n"+e);
rsp = null;
break;
}
}
return rsp;
},
queryELB : function (action, params, objActions, isSync, reqType, callback) {
if (this.accessCode == null || this.accessCode == "") {
log ("No Access Code for user");
return;
}
if (this.elbURL == null || this.elbURL == "") {
this.setEndpoint(ec2ui_session.getActiveEndpoint());
}
var rsp = null;
while(true) {
try {
rsp = this.queryELBImpl(action, params, objActions, isSync, reqType, callback);
if (rsp.hasErrors) {
if (action == 'RegisterInstancesWithLoadBalancer' && rsp.faultCode == 'ValidationError' && rsp.faultString == 'Instance cannot be empty.') {
break;
}
if (!this.errorDialog(
"EC2 responded with an error for "+action,
rsp.faultCode,
rsp.requestId,
rsp.faultString)) {
break;
}
} else {
break;
}
} catch (e) {
alert ("An error occurred while calling "+action+"\n"+e);
rsp = null;
break;
}
}
return rsp;
},
errorDialog : function(msg, code, rId, fStr) {
var retry = {value:null};
window.openDialog(
"chrome://ec2ui/content/dialog_retry_cancel.xul",
null,
"chrome,centerscreen,modal",
msg,
code,
rId,
fStr,
retry);
return retry.value;
},
x_url_encode: function(str) {
str = encodeURIComponent(str);
var func = function(m) {
switch(m) {
case '!':
return '%21';
case "'":
return '%27';
case '(':
return '%28';
case ')':
return '%29';
case '*':
return '%2A';
default:
return m;
}
};
return str.replace(/[!'()*~]/g, func); // '
},
queryEC2Impl : function (action, params, objActions, isSync, reqType, callback) {
var curTime = new Date();
var formattedTime = this.formatDate(curTime, "yyyy-MM-ddThh:mm:ssZ");
var sigValues = new Array();
sigValues.push(new Array("Action", action));
sigValues.push(new Array("AWSAccessKeyId", this.accessCode));
sigValues.push(new Array("SignatureVersion","2"));
sigValues.push(new Array("SignatureMethod","HmacSHA1"));
sigValues.push(new Array("Version",this.API_VERSION));
sigValues.push(new Array("Timestamp",formattedTime));
// Mix in the additional parameters. params must be an Array of tuples as for sigValues above
for (var i = 0; i < params.length; i++) {
sigValues.push(params[i]);
}
// Sort the parameters by their lowercase name
sigValues.sort(function(a, b) {
a = a[0]; b = b[0];
return (a < b) ? -1 : (a > b) ? 1 : 0;
});
// Construct the string to sign and query string
var queryParams = "";
for (var i = 0; i < sigValues.length; i++) {
queryParams += sigValues[i][0] + "=" + this.x_url_encode(sigValues[i][1]);
if (i < sigValues.length-1)
queryParams += "&";
}
var strSig = "POST\n" + this.serviceURL.replace(/^https?:\/\//, '') + "\n/\n" + queryParams;
log("StrSig ["+strSig+"]");
log("Params ["+queryParams+"]");
var sig = b64_hmac_sha1(this.secretKey, strSig);
log("Sig ["+sig+"]");
queryParams += ("&Signature=" + this.x_url_encode(sig));
var url = this.serviceURL + "/";
log("URL ["+url+"]");
log("QueryParams ["+queryParams+"]");
var timerKey = strSig+":"+formattedTime;
if (!ec2ui_prefs.isOfflineEnabled()) {
var xmlhttp = this.newInstance();
if (!xmlhttp) {
log("Could not create xmlhttp object");
return null;
}
xmlhttp.open("POST", url, !isSync);
xmlhttp.setRequestHeader("User-Agent", this.USER_AGENT);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
xmlhttp.setRequestHeader("Content-Length", queryParams.length);
xmlhttp.setRequestHeader("Connection", "close");
this.startTimer(timerKey, 30000, xmlhttp.abort);
var me = this;
if (isSync) {
xmlhttp.onreadystatechange = empty;
} else {
xmlhttp.onreadystatechange = function () {
me.handleAsyncResponse(xmlhttp, callback, reqType, objActions);
}
}
try {
xmlhttp.send(queryParams);
this.stopTimer(timerKey);
} catch(e) {
if (isSync && !this.stopTimer(timerKey)) {
// A timer didn't exist, this is unexpected
throw e;
}
var faultStr = "Please check your EC2 URL '" + url + "' for correctness, or delete the value in ec2ui.endpoints using about:config and retry.";
return this.newResponseObject(null, callback, reqType, true, "Request Error", faultStr, "");
}
}
return this.processXMLHTTPResponse(xmlhttp, reqType, isSync, timerKey, objActions, callback);
},
queryELBImpl : function (action, params, objActions, isSync, reqType, callback) {
var curTime = new Date();
//if (ec2ui_session.isAmazonEndpointSelected()) {
var formattedTime = this.formatDate(curTime, "yyyy-MM-ddThh:mm:ssZ");
//else {
// var formattedTime = this.formatDate(curTime, "yyyy-MM-ddThh:mm:ss");
//}
var sigValues = new Array();
sigValues.push(new Array("Action", action));
sigValues.push(new Array("AWSAccessKeyId", this.accessCode));
sigValues.push(new Array("SignatureVersion","1"));
sigValues.push(new Array("Version",this.ELB_API_VERSION));
sigValues.push(new Array("Timestamp",formattedTime));
// Mix in the additional parameters. params must be an Array of tuples as for sigValues above
for (var i = 0; i < params.length; i++) {
sigValues.push(params[i]);
}
// Sort the parameters by their lowercase name
sigValues.sort(this.sigParamCmp);
// Construct the string to sign and query string
var strSig = "";
var queryParams = "";
for (var i = 0; i < sigValues.length; i++) {
strSig += sigValues[i][0] + sigValues[i][1];
queryParams += sigValues[i][0] + "=" + encodeURIComponent(sigValues[i][1]);
if (i < sigValues.length-1)
queryParams += "&";
}
log("StrSig ["+strSig+"]");
log("Params ["+queryParams+"]");
var sig = b64_hmac_sha1(this.secretKey, strSig);
log("Sig ["+sig+"]");
queryParams += "&Signature="+encodeURIComponent(sig);
var url = this.elbURL + "/";
log("URL ["+url+"]");
log("QueryParams ["+queryParams+"]");
var timerKey = strSig+":"+formattedTime;
if (!ec2ui_prefs.isOfflineEnabled()) {
var xmlhttp = this.newInstance();
if (!xmlhttp) {
log("Could not create xmlhttp object");
return null;
}
xmlhttp.open("POST", url, !isSync);
xmlhttp.setRequestHeader("User-Agent", this.USER_AGENT);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-Length", queryParams.length);
xmlhttp.setRequestHeader("Connection", "close");
this.startTimer(timerKey, 30000, xmlhttp.abort);
var me = this;
if (isSync) {
xmlhttp.onreadystatechange = empty;
} else {
xmlhttp.onreadystatechange = function () {
me.handleAsyncResponse(xmlhttp, callback, reqType, objActions);
}
}
try {
xmlhttp.send(queryParams);
this.stopTimer(timerKey);
} catch(e) {
if (isSync && !this.stopTimer(timerKey)) {
// A timer didn't exist, this is unexpected
throw e;
}
var faultStr = "Please check your EC2 URL '" + url + "' for correctness, or delete the value in ec2ui.endpoints using about:config and retry.";
return this.newResponseObject(null, callback, reqType, true, "Request Error", faultStr, "");
}
}
return this.processXMLHTTPResponse(xmlhttp, reqType, isSync, timerKey, objActions, callback);
},
generateS3StringToSign : function(requestType, content, copySource, curTime, fileName) {
var sigValues = new Array();
sigValues.push(new Array("Request-Type", requestType));
sigValues.push(new Array("Content-MD5", ""));
var contType = "binary/octet-stream";
if (content &&
content.length > 0 &&
("@mozilla.org/login-manager;1" in Components.classes)) {
// This is firefox 3+, so an automatic content-encoding is added
contType = contType + "; charset=UTF-8";
}
sigValues.push(new Array("Content-Type", contType));
sigValues.push(new Array("Date", curTime));
if (copySource) {
sigValues.push(new Array("Amazon-S3-Extension", "x-amz-copy-source:" + encodeURIComponent(copySource)));
sigValues.push(new Array("Amazon-S3-Extension", "x-amz-metadata-directive:COPY"));
}
sigValues.push(new Array("path", fileName));
// Construct the string to sign and query string
var strSig = "";
for (var i = 0; i < (sigValues.length - 1); ++i) {
strSig += sigValues[i][1] + "\n";
}
// Append the last part of the request that needs to be signed.
strSig += sigValues[i][1];
log("StrSig ["+strSig+"]");
return strSig;
},
S3SignString : function(strSig) {
var sig = "AWS " + this.accessCode + ":" + b64_hmac_sha1(this.secretKey, strSig);
log("Auth Str["+sig+"]");
return sig;
},
handleAsyncResponse : function(xmlhttp, callback, reqType, objActions) {
if (xmlhttp.readyState == 4) {
var responseObject = null;
log("Async Response = " + xmlhttp.status + ", response: " + xmlhttp.responseText);
if (xmlhttp.status >= 200 && xmlhttp.status < 300) {
responseObject = this.newResponseObject(xmlhttp, callback, reqType, false);
} else {
log("Generating ASync Failure Response");
responseObject = this.unpackXMLErrorRsp(xmlhttp, reqType, callback);
}
responseObject.isAsync = true;
objActions.onResponseComplete(responseObject);
return responseObject;
}
},
makeS3HTTPRequest : function(requestType, fileName, url, content, xmlhttp, copySource, isAsync, reqType, objActions, callback) {
var curTime = new Date().toUTCString();
// isAsync might be undefined. This forces a true/false
// value to be associated with fAsync
var fAsync = (isAsync == true);
if (requestType != "GET" &&
requestType != "PUT" &&
requestType != "DELETE" &&
requestType != "HEAD") {
return null;
}
if (!content) {
content = null;
}
// Generate String to Sign
var strSig = this.generateS3StringToSign(requestType, content, copySource, curTime, fileName);
// Sign the string
var sig = this.S3SignString(strSig);
if (!xmlhttp) {
xmlhttp = this.newInstance();
}
// Ensure that the URL is encoded correctly and that an attack isn't
// launched against S3 via the bucket name or key
// This breaks the XMLHTTPRequest.Open call.
// url = encodeURIComponent(url);
var me = this;
xmlhttp.open(requestType, url, fAsync);
// In the future, the onreadystatechange event will be
// triggered for synchronous requests in Firefox as well.
if (fAsync) {
xmlhttp.onreadystatechange = function () {
me.handleAsyncResponse(xmlhttp, callback, reqType, objActions);
}
} else {
xmlhttp.onreadystatechange = empty;
}
xmlhttp.setRequestHeader("Content-Type", "binary/octet-stream");
xmlhttp.setRequestHeader("Content-MD5", "");
xmlhttp.setRequestHeader("Date", curTime);
xmlhttp.setRequestHeader("Authorization", sig);
if (content && content.length) {
xmlhttp.setRequestHeader("Content-Length", content.length);
log ("Content-Length: " + content.length);
} else {
xmlhttp.setRequestHeader("Content-Length", 0);
}
if (copySource) {
xmlhttp.setRequestHeader("x-amz-copy-source", encodeURIComponent(copySource));
xmlhttp.setRequestHeader("x-amz-metadata-directive", "COPY");
}
xmlhttp.setRequestHeader("User-Agent", this.USER_AGENT);
var timerKey = new Date().getTime();
this.startTimer(timerKey, 30 * 1000, xmlhttp.abort);
try {
log("Content: " + content);
xmlhttp.send(content);
this.stopTimer(timerKey);
} catch(e) {
if (!fAsync && !this.stopTimer(timerKey)) {
// A timer didn't exist, this is unexpected
throw e;
}
var faultStr = "Your request timed out. Please try again later.";
return this.newResponseObject(null, callback, reqType, true, "Timeout", faultStr, "");
}
// Process the response
return this.processXMLHTTPResponse(xmlhttp, reqType, !fAsync, timerKey, objActions, callback);
},
newResponseObject : function(xmlhttp, callback, reqType, hasErrors, faultCode, faultString, requestId) {
var xmlDoc = (xmlhttp) ? xmlhttp.responseXML : null;
var strHeaders = (xmlhttp) ? xmlhttp.getAllResponseHeaders() : null;
return {
xmlhttp : xmlhttp,
xmlDoc: xmlDoc,
strHeaders: strHeaders,
callback: callback,
requestType : reqType,
faultCode : faultCode,
requestId : requestId,
faultString : faultString,
hasErrors : hasErrors,
//responseText: ("" + xmlhttp.responseText.toString())
};
},
processXMLHTTPResponse : function(xmlhttp, reqType, isSync, timerKey, objActions, callback) {
if (isSync) {
log("Sync Response = " + xmlhttp.status + "("+xmlhttp.readyState+"): " + xmlhttp.responseText);
if (xmlhttp.status >= 200 && xmlhttp.status < 300) {
log("Generating Sync Success Response");
var resp = this.newResponseObject(xmlhttp, callback, reqType, false);
if (objActions) {
objActions.onResponseComplete(resp);
}
return resp;
} else {
log("Generating Sync Failure Response");
return this.unpackXMLErrorRsp(xmlhttp, reqType, callback);
}
} else {
return {hasErrors:false};
}
},
unpackXMLErrorRsp : function(xmlhttp, reqType, callback) {
var faultCode = "Unknown";
var faultString = "An unknown error occurred.";
var requestId = "";
var xmlDoc = xmlhttp.responseXML;
if (!xmlDoc) {
if (xmlhttp.responseText)
xmlDoc = new DOMParser().parseFromString(xmlhttp.responseText, "text/xml");
}
if (xmlDoc) {
faultCode = getNodeValueByName(xmlDoc, "Code");
faultString = getNodeValueByName(xmlDoc, "Message");
requestId = getNodeValueByName(xmlDoc, "RequestID");
}
log("Generated New Error Response Object");
return this.newResponseObject(xmlhttp, callback, reqType, true, faultCode, faultString, requestId);
},
queryVpnConnectionStylesheets : function(stylesheet) {
var xmlhttp = this.newInstance();
if (!xmlhttp) {
log("Could not create xmlhttp object");
return;
}
if (stylesheet == null) {
stylesheet = "customer-gateway-config-formats.xml";
}
var url = this.VPN_CONFIG_PATH + '2009-07-15' + "/" + stylesheet
log ("Retrieving: "+url);
xmlhttp.open("GET", url, false);
xmlhttp.onreadystatechange = empty;
xmlhttp.setRequestHeader("User-Agent", this.USER_AGENT);
xmlhttp.overrideMimeType('text/xml');
var timerKey = new Date().getTime();
// We'll wait up to 10 seconds for a response
this.startTimer(timerKey, 10 * 1000, xmlhttp.abort);
try {
xmlhttp.send(null);
this.stopTimer(timerKey);
} catch(e) {
if (!this.stopTimer(timerKey)) {
// A timer didn't exist, this is unexpected
throw e;
}
}
log("XMLHTTP is in state: "+xmlhttp.readyState);
return this.processXMLHTTPResponse(xmlhttp, stylesheet, true, timerKey, null, null);
},
queryCheckIP : function(reqType, retVal) {
var xmlhttp = this.newInstance();
if (!xmlhttp) {
log("Could not create xmlhttp object");
return;
}
xmlhttp.open("GET", "http://checkip.amazonaws.com/" + reqType, false);
xmlhttp.setRequestHeader("User-Agent", this.USER_AGENT);
var timerKey = new Date().getTime();
this.startTimer(timerKey, 10000, xmlhttp.abort);
try {
xmlhttp.send(null);
this.stopTimer(timerKey);
} catch(e) {
if (!this.stopTimer(timerKey)) {
// A timer didn't exist, this is unexpected
throw e;
}
}
retVal.ipAddress = xmlhttp.responseText;
},
startTimer : function(key, timeout, expr) {
var timer = window.setTimeout(expr, timeout);
this.timers[key] = timer;
},
stopTimer : function(key, timeout) {
var timer = this.timers[key];
this.timers[key] = null;
if (timer == null) {
return false;
}
window.clearTimeout(timer);
timer = null;
return true;
}
}