-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (205 loc) · 6.28 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
const PORT = 3000;
var express = require( 'express' );
var multer = require( 'multer' );
var exphbs = require( 'express-handlebars' );
var cors = require('cors');
var exec = require('child_process').exec;
var filesize = require('file-size');
var notifications = require( 'freedesktop-notifications' ) ;
var path = require('path');
var os = require('os');
//var bodyParser = require('body-parser');
require( 'string.prototype.startswith' );
var upload = createStorage();
function createStorage(dir) {
if (dir === "" || dir == undefined)
dir = path.join(os.homedir(), "Downloads");
currentDir = dir;
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, dir)
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})
return multer({ storage: storage })
}
var app = express();
var avahiService = null;
var deviceName = os.hostname();
app.use(cors());
//app.use(bodyParser.json({limit: '1mb'}));
app.use(express.static(__dirname + '/paperplane-frontend'));
app.get( '/peers', function( req, res, next ){
getPeers(function (services) {
return res.json(services);
});
});
app.get( '/enable', function( req, res, next ){
publishService(function (msg) {
return res.json(msg);
});
});
app.get( '/disable', function( req, res, next ){
hideService(function (msg) {
return res.json(msg);
});
});
// Usage: host/set?dir=/home/user/Downloads
// dir can also be ~/Downloads
// if dir is not set this query will response with the current dir
// Response: {status: "status msg", dir: "currentdir"}
app.get( '/setDir', function( req, res, next ){
setStorageDir(req.query.dir, function (msg) {
return res.json(msg);
});
});
// Usage: host/changeName?name=Jeff's Laptop
// if name is not set this query will response with the current name
// Response: {status: "status msg", name: "currentName"}
app.get( '/setName', function( req, res, next ){
setName(req.query.name, function (msg) {
return res.json(msg);
});
});
// Usage: host/getIp
// Response: {status: "status msg", ip: {deviceName: "0.0.0.0", secoundDevice: "1.2.3.4"}}
app.get( '/getIp', function( req, res, next ){
getIpAdresses(function (msg) {
return res.json(msg);
});
});
app.post( '/upload', upload.single('file'), function( req, res, next ) {
showNotification({origin: "Somebody", name: req.file.originalname, size: filesize(req.file.size)});
console.log(req.file);
return res.status( 200 ).send( req.file );
});
app.listen( PORT, function() {
console.log( 'Express server listening on port 3000' );
});
function setStorageDir(dir, cb) {
//get the home dir
//os.homedir();
if (dir === "" || dir === undefined) {
cb({"status": "", "dir" : currentDir});
}
else if (path.isAbsolute(dir) || dir.startsWith('~')) {
if (dir.startsWith('~')) {
dir = path.join(os.homedir(), dir.substr(1, dir.length));
}
upload = createStorage(dir);
if (cb)
cb({"status": "changed", "dir" : dir});
}
else if (cb)
cb({"status": "The path has to be absolute."});
}
function setName(name, cb) {
if (name === "" || name === undefined) {
cb({"status": "", "name" : deviceName});
}
else {
deviceName = name;
if (cb)
cb({"status": "changed", "name" : name});
}
//else if (cb)
// cb({"status": "Not a valid device name."});
}
function getPeers(cb) {
var avahiBrowse = 'avahi-browse -lrpt _http._tcp'
var services = [];
var child = exec(avahiBrowse, function (error, stdout, stderr) {
var lines = stdout.split("\n");
for (var i = 0; i < lines.length; i++) {
if (lines[i].includes("=") && lines[i].includes("IPv4") && lines[i].includes("paperplane")) {
var info = lines[i].split(";");
var obj = {};
obj.name = info[6];
obj.ip = info[7];
obj.port = info[8];
obj.msg = info[9];
services.push(obj)
}
}
if (error !== null) {
console.log('exec error: ' + error);
console.log('stderr: ' + stderr);
}
if (cb !== null)
cb(services);
});
}
publishService(function msg(){});
function publishService(cb) {
//shell comand: avahi-publish -s paperplane _http._tcp 3000 "Download this files"
if (avahiService !== null) {
avahiService.kill('SIGTERM');
avahiService = null;
}
var avahiPublish = "avahi-publish -s paperplane _http._tcp " + PORT + " 'Download this files'"
avahiService = exec(avahiPublish, function (error, stdout, stderr) {
console.log(stdout);
if (error !== null) {
console.log('exec error: ' + error);
console.log('stderr: ' + stderr);
}
});
cb({"status": "enabled"});
}
function hideService(cb) {
if (avahiService !== null) {
avahiService.kill('SIGTERM');
avahiService = null;
}
cb({"status": "disabled"});
}
function showNotification(file) {
var notif = notifications.createNotification( {
summary: 'Incoming file' ,
body: file.origin + ' is sending you the file ' + file.name + " (" + file.size.human('si') + ")" ,
icon: __dirname + '/log.png' ,
"sound-file": __dirname + '/hiss.wav' ,
actions: {
default: '' ,
cancel: 'Cancel' ,
accept: 'Accept'
}
} ) ;
notif.on( 'action' , function( action ) {
console.log("Action '%s' was clicked!" , action) ;
switch (action) {
case 'Cancel' : console.log("Should remove file");
break;
case 'Accept' : console.log("Should do nothing");
break;
}
} ) ;
notif.on( 'close' , function( closedBy ) {
console.log( "Closed by: '%s'" , closedBy ) ;
} ) ;
notif.push() ;
}
function getIpAdresses(cb) {
var ifaces = os.networkInterfaces();
var addresses = {};
Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
addresses[ifname + ':' + alias] = iface.address;
} else {
// this interface has only one ipv4 adress
addresses[ifname] = iface.address;
}
alias++;
});
});
cb({"status": "", 'ip' : addresses});
}