-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
407 lines (384 loc) · 12 KB
/
index.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
'use strict'
const WIN = require("ui/window");
const Tabbar = require('ui/tabbar');
const LANG = require('./language/');
const LANG_T = antSword['language']['toastr'];
class Plugin {
constructor(opt) {
let self = this;
self.opt = opt;
self.core = new antSword['core'][opt['type']](opt);
self.isWindows = false;
self.WinSupportCore = ['aspx'];
self.coreType = opt['type'];
let cache = new antSword['CacheManager'](self.opt["_id"]);
const cache_info = cache.get('info');
if (cache_info) {
if (cache_info[0] === "/") {
self.isWindows = false;
} else {
self.isWindows = true;
if (!self.WinSupportCore.includes(self.coreType)) {
toastr.error(LANG['msg']['winnotsupport'], LANG_T['error']);
return
}
}
self.initUI();
} else {
self.core.request(
self.core.base.info()
).then((ret) => {
if (ret['text'][0] === "/") {
self.isWindows = false;
} else {
self.isWindows = true;
if (!self.WinSupportCore.includes(self.coreType)) {
toastr.error(LANG['msg']['winnotsupport'], LANG_T['error']);
return
}
};
self.initUI();
}).catch((err) => {
toastr.error(err, LANG_T['error']);
});
}
}
initUI() {
let self = this;
self.tabbar = new Tabbar();
self.tabbar.cell.setText(`<i class="fa fa-signal"></i> ${LANG['title']}-${self.opt['ip']}`);
self.grid = null;
self.toolbar = null;
self.createToolBar();
self.createGrid();
self.bindToolbarClickHandler();
}
createToolBar() {
let self = this;
let toolbar = self.tabbar.cell.attachToolbar();
toolbar.loadStruct([
{ id: 'label', type: 'text', text: LANG['main']['toolbar']['label'], },
{ id: 'tcp4', type: 'buttonTwoState', text: 'TCP4', title: LANG['main']['toolbar']['tcp4'], pressed: true, },
{ id: 'udp4', type: 'buttonTwoState', text: 'UDP4', title: LANG['main']['toolbar']['udp4'], pressed: false, },
{ type: 'separator' },
{ id: 'start', type: 'button', text: LANG['main']['toolbar']['start'], icon: 'play' },
{ type: 'separator' },
{ id: 'local', type: 'button', text: LANG['main']['toolbar']['manual'], icon: 'edit' },
{ type: 'separator' },
{ id: 'clear', type: 'button', text: LANG['main']['toolbar']['clear'], icon: 'remove' },
]);
self.toolbar = toolbar;
}
createGrid() {
let self = this;
let grid = self.tabbar.cell.attachGrid();
grid.setHeader(`Proto,Local Address,Remote Address,State`);
grid.setColumnIds("proto,laddr,raddr,state");
grid.setColTypes("ro,ro,ro,ro");
grid.setColSorting('str,str,str,str');
grid.setInitWidths("150,300,300,*");
grid.setColAlign("left,left,left,center");
grid.enableMultiselect(true);
grid.init();
self.grid = grid;
}
bindToolbarClickHandler() {
let self = this;
self.toolbar.attachEvent('onClick', (id) => {
switch (id) {
case 'start':
if (self.isWindows == false) {
let inetfiles = [];
['tcp4', 'udp4'].forEach((item) => {
if (self.toolbar.getItemState(item) == true) {
inetfiles.push({
inet: item,
path: INET_FILE_MAPPING[item]
});
};
});
if (inetfiles.length == 0) {
toastr.warning(LANG['msg']['emptyselect'], LANG_T['warning']);
return
}
// 清空 grid
self.grid.clearAll();
inetfiles.map((p) => {
self.core.request(
self.core.filemanager.read_file({
path: p.path,
})
).then((res) => {
let data = res['text'];
self.gridParse(data, p.inet, true);
toastr.success(LANG['success'], LANG_T['success']);
}).catch((err) => {
toastr.error(err, LANG_T['error']);
});
});
} else {
// windows 下
self.grid.clearAll();
['tcp4', 'udp4'].forEach((inettype) => {
if (self.toolbar.getItemState(inettype) == true) {
self.core.request({
_: self.getWinPayload(inettype)
}).then((res) => {
let data = res['text'];
// 清空 grid
let results = data.split('\n');
results.forEach((result, i) => {
let line = result.split("\t");
if (line.length < 4) {
return
}
let connitem = {
proto: (line[0].toUpperCase()) || inettype,
laddr: line[1] || '',
raddr: line[2] || '',
state: (line[3].toUpperCase()) || '-',
};
let rowId = `${inettype}-${i}`;
self.grid.addRow(rowId, "");
self.GridsetRowData(rowId, connitem);
if (connitem.state == "LISTENING" || (inettype.startsWith('udp') && connitem.laddr.startsWith("0.0.0.0"))) {
self.grid.setRowColor(rowId, "#ADF1B9");
}
});
}).catch((err) => {
toastr.error(err, LANG_T['error']);
});
}
});
}
break;
case 'local':
self.createLocalEditor();
break;
case 'clear':
self.grid.clearAll();
break;
default:
break;
}
});
}
getWinPayload(inettype) {
let self = this;
let codes = {
aspx_tcp4: `
try{
var properties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
var i;
var listeners = properties.GetActiveTcpListeners();
for(i in listeners){
var t = listeners[i];
if(t.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork){
Response.Write("TCP\\t"+t.ToString()+"\\t0.0.0.0:0\\tListening\\n");
}
};
var connections = properties.GetActiveTcpConnections();
for(i in connections){
var t = connections[i];
if(t.LocalEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork){
Response.Write("TCP\\t"+t.LocalEndPoint+"\\t"+t.RemoteEndPoint+"\\t"+t.State+"\\n");
}
};
}catch(e){
Response.Write(e);
};
`.replace(/\n\s+/g, ''),
aspx_udp4: `
try{
var properties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
var i;
var listeners = properties.GetActiveUdpListeners();
for(i in listeners){
var t = listeners[i];
if(t.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork){
Response.Write("UDP\\t"+t.ToString()+"\\t0.0.0.0:0\\t-\\n");
}
};
}catch(e){
Response.Write(e);
};
`.replace(/\n\s+/g, ''),
}
return codes[`${self.coreType}_${inettype}`];
}
createLocalEditor() {
let self = this;
let inet_type = "tcp4";
let _win = new WIN({
title: LANG['manual']['title'](INET_FILE_MAPPING[inet_type]),
});
let toolbar = _win.win.attachToolbar();
toolbar.loadStruct([
{ id: 'inet_type_label', type: 'text', text: LANG['manual']['toolbar']['label'] },
{
id: 'inet_type',
type: 'buttonSelect',
mode: 'select',
selected: inet_type,
width: 100,
options: [
{ id: 'tcp4', type: 'button', icon: 'hashtag', text: 'TCP4' },
{ id: 'udp4', type: 'button', icon: 'hashtag', text: 'UDP4' },
]
},
{ id: 'save', type: 'button', icon: 'check', text: LANG['manual']['toolbar']['save'], }
]);
toolbar.attachEvent('onClick', (id) => {
switch (id) {
case 'tcp4':
_win.win.setText(LANG['manual']['title'](INET_FILE_MAPPING[inet_type]));
inet_type = "tcp4";
break;
case 'udp4':
_win.win.setText(LANG['manual']['title'](INET_FILE_MAPPING[inet_type]));
inet_type = "udp4";
break;
case 'save':
// 保存
let data = editor.session.getValue();
if (!data) {
toastr.warning(LANG['msg']['empty'], LANG_T["warning"]);
return
}
self.grid.clearAll();
self.gridParse(data, inet_type, false);
toastr.success(LANG['msg']['parse_finished'], LANG_T['success']);
break;
}
});
// 创建编辑器
let editor;
editor = ace.edit(_win.win.cell.lastChild);
editor.$blockScrolling = Infinity;
editor.setTheme('ace/theme/tomorrow');
editor.session.setMode('ace/mode/text');
editor.session.setUseWrapMode(true);
editor.session.setWrapLimitRange(null, null);
editor.setOptions({
fontSize: '14px',
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true
});
// 编辑器快捷键
editor.commands.addCommand({
name: 'save',
bindKey: {
win: 'Ctrl-S',
mac: 'Command-S'
},
exec: () => {
toolbar.callEvent('onClick', ['save']);
}
});
// 定时刷新
const inter = setInterval(editor.resize.bind(editor), 200);
_win.win.attachEvent('onClose', () => {
clearInterval(inter);
return true;
});
}
gridParse(data, data_type = "tcp4", append = false) {
let self = this;
let result = parseNetFile(data);
result.map((item, i) => {
if (!item) { return };
let connitem = {};
switch (data_type) {
case 'tcp4':
connitem = {
laddr: Inet4Addr(item.laddr),
raddr: Inet4Addr(item.raddr),
status: TCP_STATUS_MAPPING[item.status.toUpperCase()] || item.status,
}
break;
case 'udp4':
connitem = {
laddr: Inet4Addr(item.laddr),
raddr: Inet4Addr(item.raddr),
status: "-",
}
break;
default:
break;
}
let rowId = `${data_type}-${i}`;
self.grid.addRow(rowId, "");
self.GridsetRowData(rowId, {
"proto": data_type,
"laddr": connitem.laddr,
"raddr": connitem.raddr,
"state": connitem.status,
});
if (connitem.status == "LISTEN" || (data_type.startsWith("udp") && connitem.laddr.startsWith("0.0.0.0:"))) {
self.grid.setRowColor(rowId, "#ADF1B9");
}
});
}
GridsetRowData( /*string*/ rowId, /*json*/ rowJson) {
let self = this;
var colsNum = self.grid.getColumnsNum();
for (var index = 0; index < colsNum; index++) {
var colId = self.grid.getColumnId(index);
if (colId && rowJson.hasOwnProperty(colId)) {
self.grid.cells(rowId, index).setValue(rowJson[colId]);
}
}
};
}
const INET_FILE_MAPPING = {
"tcp4": "/proc/net/tcp",
"tcp6": "/proc/net/tcp6",
"udp4": "/proc/net/udp",
"udp6": "/proc/net/udp6",
}
const TCP_STATUS_MAPPING = {
"01": "ESTABLISHED",
"02": "SYN_SENT",
"03": "SYN_RECV",
"04": "FIN_WAIT1",
"05": "FIN_WAIT2",
"06": "TIME_WAIT",
"07": "CLOSE",
"08": "CLOSE_WAIT",
"09": "LAST_ACK",
"0A": "LISTEN",
"0B": "CLOSING",
}
function Inet4Addr(addr) {
let l = addr.split(":");
let ip = Buffer.from(l[0], "hex").reverse().join(".").toString();
let port = parseInt(l[1], 16);
return ip + ":" + port;
}
function parseNetFile(data) {
let ret = [];
data = data.trim()
let datas = data.split("\n")
if (datas[0].indexOf('local_address') != -1) {
datas.shift();
}
datas.forEach((line) => {
let l = line.trim().split(/\s+/);
if (l.length < 10) {
return
}
var laddr = l[1];
var raddr = l[2];
var status = l[3];
var inode = l[9];
ret.push({
laddr: laddr,
raddr: raddr,
status: status,
inode: inode,
});
});
return ret;
}
module.exports = Plugin;