-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
193 lines (165 loc) · 6.24 KB
/
server.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
var fs = require('fs');
var https = require('https');
var express = require('express');
var socketio = require('socket.io');
var cmd = require('child_process');
var app = express();
var request = require('request')
var progress = require('request-progress');
var formidable = require('formidable');
var async = require('async');
var options = {
key: fs.readFileSync('./private.key', 'utf8'),
cert: fs.readFileSync('./certificate.pem', 'utf8')
};
//var server = https.createServer(options, app).listen(443,"192.168.1.38");
var server = app.listen(80,"192.168.43.204");
var io = socketio.listen(server);
app.get("/*",function(req,res){
var file = __dirname+req.path;
fs.exists(file,function(exists){
if(exists){
res.sendFile(file);
console.log(file);
}else{
//res.redirect("/");
console.log(file+":NOT EXISTE");
}
});
});
io.on('connection', function (socket) {
console.log("new Client : "+socket.request.connection.remoteAddress);
socket.on("download",function(file){
console.log("start ; "+file.name);
//var file = fs.createWriteStream("./files/"+file.name);
socket.emit("showLoader","");
var get = request.get(
{url: file.url
, encoding: null // Force Request to return the data as Buffer
, headers:
{ Authorization: 'Bearer ' + file.token}
}
, function done (err, res ,body) {
fs.writeFile('./files/'+file.name, body, function (err) {
if(!err){
cmd.exec("ls -1 ./files/",function(error,stdout,stderr){
if(!error){
var files = stdout.split("\n");
socket.emit("files",files);
}
});
socket.emit("hideLoader","");
}
if(err){
console.log("ERROOR");
socket.emit("hideLoader","");
}
})
}
);
progress(get).on('progress', function (state) {
console.log(state);
if(state.percentage)
socket.emit("progress",state);
});
});
socket.on("getFiles",function(){
cmd.exec("ls -1 ./files/",function(error,stdout,stderr){
if(!error){
var files = stdout.split("\n");
socket.emit("files",files);
}
});
});
socket.on("removeFile",function(file){
cmd.exec("rm './files/"+file+"'",function(error, stdout, stderr){});
});
socket.on("showFile",function(file){
cmd.exec("xdg-open './files/"+file+"'",function(error, stdout, stderr){
setTimeout(function(){
cmd.exec("xdotool key Escape F11 F5",function(error, stdout, stderr){});
},500);
});
});
socket.on("freeLaser",function(data){
cmd.exec("sh laser.sh "+data.x+" "+data.y,function(error, stdout, stderr){});
});
socket.on("keyStrok",function(key){
var commande = "xdotool key "+key;
cmd.exec(commande,function(error,stdout,stderr){});
});
socket.on("slider",function(direction){
var commande = "xdotool click "+direction;
cmd.exec(commande,function(error,stdout,stderr){});
});
socket.on("tap",function(click){
var commande = "xdotool click "+click;
cmd.exec(commande,function(error,stdout,stderr){});
})
socket.on("touchMove",function(data){
var commande = "xdotool mousemove_relative -- "+data.x+" "+data.y;
cmd.exec(commande,function(error,stdout,stderr){});
})
socket.on("laserMove",function(data){
var commande = "bash laser.sh "+data.x+" "+data.y;
cmd.exec(commande,function(error, stdout, stderr){});
});
var free = true;
var old = '';
socket.on("input",function(val){
if(free)
async.series([
function(callback){
free = false;
if(val.length<old.length){
async.each(
old.slice(val.length-old.length),
function(e,callback){
var commande = "xdotool key BackSpace";
cmd.exec(commande,function(error, stdout, stderr){
if(error) return callback('error');
callback(null);
});
},
function(err){
callback('fin',null);
})
}else{
callback(null,null);
}
},
function(callback){
var commande = "xdotool type '"+val.slice(old.length-val.length)+"'";
cmd.exec(commande,function(error, stdout, stderr){
if(error) return callback('error');
callback(null);
});
}
],function(err,data){
free = true;
old = val;
})
});
socket.on("laser",function(action){
if(action == "show"){
cmd.exec("./show-lazer.sh",function(error, stdout, stderr){});
}else{
cmd.exec("./kill-lazer.sh",function(error, stdout, stderr){});
}
});
});
app.post('/files', function(req, res){
var form = new formidable.IncomingForm();
form.multiples = true;
form.uploadDir = __dirname+"/files/";
form.on('file', function(field, file) {
fs.rename(file.path,form.uploadDir+file.name);
});
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
form.on('end', function() {
res.end('success');
});
form.parse(req);
});