-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
211 lines (178 loc) · 5.98 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
var fs = require('fs')
var ejs = require('ejs')
var url = require('url')
var http = require('http')
var path = require('path')
var express = require('express')
var uglify = require('uglify-js')
var compress = require('compression')
var bodyParser = require('body-parser')
var cookieParser = require('cookie-parser')
var jsondiffpatch = require('jsondiffpatch').create({
textDiff: {
minLength: 256
}
})
var config = require('../config')
/**
* JSInspector server and socket server
* @type {express}
*/
var app = new express()
var server = http.Server(app)
var io = require('socket.io')(server, { log: false })
var tmpDir = config.tmp_dir
!fs.existsSync(tmpDir) && fs.mkdirSync(tmpDir)
/**
* Enalbe gzip
*/
app.use(compress())
var sourceCaches = {}
var publicDir = path.join(__dirname, '../public')
function loadFile(p, minify) {
var sourceCode = fs.readFileSync(p, 'utf-8')
if (minify) {
sourceCode = uglify.minify(
sourceCode,
{
fromString: true,
mangle: true,
compress: true
}
).code
}
return sourceCode
}
if (!config.isDev) {
var files = fs.readdirSync(publicDir)
process.stdout.write('\nPreloading files ..')
files.forEach(function (f) {
if (/\.js$/.test(f)) {
var source = loadFile(path.join(publicDir, f), config.enable_mini)
sourceCaches['/' + f] = source
process.stdout.write('.')
}
})
console.log('')
}
app.get('/*.js', function (req, res, next) {
var fpath = path.join(publicDir, '.' + req.path)
if (!fs.existsSync(fpath)) return res.status(404).send(req.path + ' not found !')
var sourceCode = ''
if (sourceCaches[req.path])
sourceCode = sourceCaches[req.path]
else {
sourceCode = loadFile(fpath, config.enable_mini)
if (!config.isDev) {
sourceCaches[req.path] = sourceCode
}
}
if (req.path == '/inspector.js') sourceCode = ejs.render(sourceCode, {
serverTime: +new Date
})
res.send(sourceCode)
})
app.use(express.static(path.join(__dirname, '../public')))
/**
* Global CORS
**/
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin ? req.headers.origin : '*')
res.setHeader('Access-Control-Allow-Methods', 'PUT,POST,HEAD,GET,OPTIONS,PATCH')
next()
})
app.use(cookieParser())
app.enable('etag')
app.use(require('./routes/inspector'))
app.use(require('./routes/client'))
app.get('/', function (req, res) {
res.send(fs.readFileSync(path.join(__dirname, '../public/dashboard.html'), 'utf-8'))
})
var clients = {}
app.get('/clients', function (req, res, next) {
res.send(clients)
})
app.get('/preview/:cid', function (req, res, next) {
var fpath = path.join(tmpDir, 'client_'+ req.params.cid + '.json')
if (!fs.existsSync(fpath)) return res.status(404).send(req.path + ' not found !')
var tmpData = fs.readFileSync(fpath, 'utf-8')
tmpData = JSON.parse(tmpData)
res.send(tmpData.html)
})
var inspectorSocket = io.of('/inpsector')
var clientSocket = io.of('/client')
var deviceSocket = io.of('/device')
inspectorSocket.on('connection', function(socket) {
socket.on('inspector:input', function(data) {
clientSocket.emit('client:inject:' + data.id, data.payload)
})
})
clientSocket.on('connection', function(socket) {
var socketId
socket.on('client:init', function (payload) {
var clientId = socketId = payload.cid
var packetId = payload.pid
var data = payload.data
var file = path.join(tmpDir, 'client_' + clientId + '.json')
// connect session
clients[clientId] = data.browser
// save as base document data
fs.writeFileSync(file, JSON.stringify(data), 'utf-8')
// tell inspector
inspectorSocket.emit('server:inspector:' + clientId, data)
socket.emit('server:answer:init:' + clientId, {
cid: clientId,
pid: packetId
})
// device connect
deviceSocket.emit('device:update')
})
socket.on('disconnect', function () {
if (socketId) delete clients[socketId]
// device disconnect
deviceSocket.emit('device:update')
})
socket.on('client:update', function (payload) {
var clientId = payload.cid
var packetId = payload.pid
var data = payload.data
var file = path.join(tmpDir, 'client_' + clientId + '.json')
// syncData is used to tell inspector what is happening of client
var tmpData = fs.readFileSync(file, 'utf-8')
var syncData
function fail () {
socket.emit('server:answer:init:' + clientId, {
cid: clientId,
pid: packetId,
error: { code: 4000, message: 'Base HTML not found!' }
})
}
syncData = JSON.parse(JSON.stringify(data))
if (tmpData) {
tmpData = JSON.parse(tmpData)
if (!data.browser) syncData.browser = data.browser = tmpData.browser
// patching html text
if (data.delta && !data.html && tmpData.html) {
// full amount release, currently I can't support delta release
syncData.html = data.html = jsondiffpatch.patch(tmpData.html, data.delta);
delete data.delta;
} else if (!data.html && tmpData.html) {
// match when receive the data packet only the meta
data.html = tmpData.html;
} else if (!data.html) {
return fail()
}
// save newest inspect data
fs.writeFileSync(file, JSON.stringify(data), 'utf-8')
// tell inspector
inspectorSocket.emit('server:inspector:' + clientId, syncData)
socket.emit('server:answer:update:' + clientId, {
cid: clientId,
pid: packetId
})
} else {
return fail()
}
})
})
module.exports = server