-
Notifications
You must be signed in to change notification settings - Fork 79
/
amt-ider-server-ws-0.0.1.js
100 lines (88 loc) · 3.7 KB
/
amt-ider-server-ws-0.0.1.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
/**
* @description IDER Handling Module
* @author Ylian Saint-Hilaire
* @version v0.0.2
*/
// Construct a Intel AMT Server IDER object
var CreateAmtRemoteServerIder = function () {
var obj = {};
obj.protocol = 4; // IDER-Server
obj.iderStart = 0; // OnReboot = 0, Graceful = 1, Now = 2
obj.floppy = null;
obj.cdrom = null;
obj.state = 0;
obj.onStateChanged = null;
obj.m = {
sectorStats: null,
onDialogPrompt: null,
dialogPrompt: function (data) { obj.socket.send(JSON.stringify({ action: 'dialogResponse', args: data })); },
bytesToAmt: 0,
bytesFromAmt: 0,
server: true,
Stop: function () { obj.Stop(); }
};
// Private method
function debug() { if (urlvars && urlvars['idertrace']) { console.log(...arguments); } }
// Private method, called by parent when it change state
obj.xxStateChange = function (newstate) {
if (obj.state == newstate) return;
debug('SIDER-StateChange', newstate);
obj.state = newstate;
if (obj.onStateChanged != null) { obj.onStateChanged(obj, obj.state); }
}
obj.Start = function (host, port, user, pass, tls) {
debug('SIDER-Start', host, port, user, pass, tls);
obj.host = host;
obj.port = port;
obj.user = user;
obj.pass = pass;
obj.connectstate = 0;
obj.socket = new WebSocket(window.location.protocol.replace('http', 'ws') + '//' + window.location.host + window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + '/webider.ashx?host=' + host + '&port=' + port + '&tls=' + tls + ((user == '*') ? '&serverauth=1' : '') + ((typeof pass === 'undefined') ? ('&serverauth=1&user=' + user) : '') + '&tls1only=' + obj.tlsv1only);
obj.socket.onopen = obj.xxOnSocketConnected;
obj.socket.onmessage = obj.xxOnMessage;
obj.socket.onclose = obj.xxOnSocketClosed;
obj.xxStateChange(1);
}
obj.Stop = function () {
debug('SIDER-Stop');
if (obj.socket != null) { obj.socket.close(); obj.socket = null; }
obj.xxStateChange(0);
}
obj.xxOnSocketConnected = function () {
obj.xxStateChange(2);
obj.socket.send(JSON.stringify({ action: 'start' }));
}
obj.xxOnMessage = function (data) {
var msg = null;
try { msg = JSON.parse(data.data); } catch (ex) { }
if ((msg == null) || (typeof msg.action != 'string')) return;
switch (msg.action) {
case 'dialog': {
if (obj.m.onDialogPrompt != null) { obj.m.onDialogPrompt(obj, msg.args, msg.buttons); }
break;
}
case 'state': {
if (msg.state == 2) { obj.xxStateChange(3); }
break;
}
case 'stats': {
obj.m.bytesToAmt = msg.toAmt;
obj.m.bytesFromAmt = msg.fromAmt;
if (obj.m.sectorStats) { obj.m.sectorStats(msg.mode, msg.dev, msg.total, msg.start, msg.len); }
break;
}
case 'error': {
var iderErrorStrings = ['', "Floppy disk image does not exist", "Invalid floppy disk image", "Unable to open floppy disk image", "CDROM disk image does not exist", "Invalid CDROM disk image", "Unable to open CDROM disk image", "Can't perform IDER with no disk images"];
console.log('IDER Error: ' + iderErrorStrings[msg.code]);
// TODO: Display dialog box this error.
break;
}
default: {
console.log('Unknown Server IDER action: ' + msg.action);
breal;
}
}
}
obj.xxOnSocketClosed = function () { obj.Stop(); }
return obj;
}