-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (112 loc) · 3.14 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
var express = require('express');
var http = require('http');
var path = require('path');
var url = require('url');
var bodyParser = require('body-parser');
var swig = require('swig');
var checkIp = require('./lib/checkip');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
//环境变量
app.set('port', 8301);
app.use(express.static(path.join(__dirname, 'static')));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// 数组存放当前有哪些localhost被启动
var feList = [];
// feList-example:[{
// ip: xxx,
// port: xxx,
// name: xxx,
// demos: [{
// path: desc
// },{}]
// }]
//
app.get('/nemo', function(req, res) {
var html = swig.compileFile('views/index.html')({ feList: feList })
res.send(html);
});
app.get('/demo', function(req, res) {
var html = swig.compileFile('views/pages/demo.html')()
res.send(html);
});
app.get('*', function(req, res) {
res.redirect('/nemo')
})
var sendSocketMsg = function() {}
io.sockets.on('connection', function(socket) {
sendSocketMsg = function() {
var html = swig.compileFile('views/pages/nav.html')({ feList: feList })
socket.emit('updateList', html);
}
});
// 用于获取并解析已开启的localhost并放入feList
var ipTimeoutObj = {};
app.post('/getBird', function(req, res) {
var repeat = false;
var ipStr = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
var ip = ipStr.match(/\d+\.\d+\.\d+\.\d+/)[0];
if (req.body.port && req.body.name) {
res.send('your info is ok');
} else {
res.send('please send right data');
return false;
}
// ip去重
feList.forEach(function(one) {
if (one.ip === ip) {
repeat = true;
}
})
// 把demos的key和value转成path和desc
var demos = [];
var j = 0;
for (var i in req.body.demos) {
demos[j] = {};
demos[j].path = regPath(i);
demos[j].desc = req.body.demos[i];
j++;
}
// 添加localhost信息
if (!repeat) {
feList.push({
ip: ip || req.body.ip,
ipType: checkIp(ip),
port: req.body.port,
name: req.body.name,
demos: demos
})
// socket更新页面列表
sendSocketMsg();
}
// 过30秒后删除。如果没有每隔20秒收到东西说明那个localhost挂了
clearTimeout(ipTimeoutObj[ip])
ipTimeoutObj[ip] = setTimeout(function() {
removeBy(feList, 'ip', ip);
sendSocketMsg();
}, 30000)
});
// 启动及端口
server.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
function removeBy(arr, key, val) {
arr.forEach(function(one) {
if (one[key] === val) {
arr.splice(arr.indexOf(one), 1);
}
})
}
function regPath(path) {
var first = path.toString()[0];
if (first === '/') {
return path.substr(1);
} else {
return path;
}
}