-
Notifications
You must be signed in to change notification settings - Fork 1
/
telnetAgent.js
117 lines (97 loc) · 3.34 KB
/
telnetAgent.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
/*
* telnetAgent lets normal web pages use APIs of apps
* It support tcp connection to external servers
*/
function telnetAgent() {
this.telnetSockets = {};
this.callback = {};
var tcp = chrome.sockets.tcp;
tcp.onReceive.addListener(this.onReceive_());
tcp.onReceiveError.addListener(this.onReceiveError_());
}
telnetAgent.prototype.getSocketId = function(telnetSocket) {
for(var webSocketId in this.telnetSockets) {
if(telnetSocket == this.telnetSockets[webSocketId])
return webSocketId;
}
return 0;
};
telnetAgent.prototype.create = function(socketId, host, port) {
var _this = this;
var tcp = chrome.sockets.tcp;
var onCreated = function(createInfo) {
if(createInfo.socketId <= 0) {
// dump("Unable to create socket");
}
var onConnected = function(result) {
_this.callback[socketId]({ action: "connected" });
_this.telnetSockets[socketId] = createInfo.socketId;
};
tcp.connect(createInfo.socketId, host, port, onConnected);
};
tcp.create({}, onCreated);
};
telnetAgent.prototype.onReceive_ = function() {
var _this = this;
this.onReceive = function(receiveInfo) {
var socketId = _this.getSocketId(receiveInfo.socketId);
if(!socketId)
return;
// convert ArrayBuffer (ab) to String (str)
var ab = receiveInfo.data;
var str = String.fromCharCode.apply(null, new Uint8Array(ab));
_this.callback[socketId]({
action: "data",
content: str
});
};
return this.onReceive;
};
telnetAgent.prototype.onReceiveError_ = function() {
var _this = this;
this.onReceiveError = function(receiveInfo) {
var socketId = _this.getSocketId(receiveInfo.socketId);
if(!socketId)
return;
switch(receiveInfo.resultCode) {
case -15: // socket is closed by peer, old version
case -100: // socket is closed by peer, new version
_this.callback[socketId]({ action: "disconnected" });
break;
default: // other errors
// dump("Unknown errors");
}
};
return this.onReceiveError;
};
telnetAgent.prototype.send = function(socketId, content) {
if(!this.telnetSockets[socketId])
return;
// convert String (str) to ArrayBuffer (ab)
var str = content;
var ab = (new Uint8Array(Array.prototype.map.call(
str, function(x) { return x.charCodeAt(0); }
))).buffer;
var tcp = chrome.sockets.tcp;
tcp.send(this.telnetSockets[socketId], ab, function() {});
};
telnetAgent.prototype.disconnect = function(socketId) {
if(!this.telnetSockets[socketId])
return;
var tcp = chrome.sockets.tcp;
tcp.disconnect(this.telnetSockets[socketId], function() {});
tcp.close(this.telnetSockets[socketId], function() {});
delete this.callback[socketId];
delete this.telnetSockets[socketId];
};
telnetAgent.prototype.close = function(socketId) {
if(socketId)
return this.disconnect(socketId);
for(var socketId in this.telnetSockets) {
if(this.telnetSockets[socketId])
this.callback[socketId]({ action: "disconnected" });
}
var tcp = chrome.sockets.tcp;
tcp.onReceive.removeListener(this.onReceive);
tcp.onReceiveError.removeListener(this.onReceiveError);
};