-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·110 lines (100 loc) · 2.9 KB
/
app.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
const express = require('express');
const morgan = require('morgan');
const mustache_express = require('mustache-express');
const path = require('path');
const fs = require('fs');
const filesize = require('filesize');
const { exec } = require('child_process');
const find = require('local-devices');
const nodePortScanner = require('node-port-scanner');
const port = 6969;
const static_files_path = "/Users/garrepi/Desktop/ps4";
const ps4_ip = "192.168.1.125";
const local_ip = "192.168.1.166";
const app = express();
app.use(morgan('combined'));
app.use(express.urlencoded());
app.engine('html', mustache_express());
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.get('/', function (req, res) {
get_devices().then(devices => {
res.render('splash',
{
"pkgs": get_pkgs(),
"devices": devices
}
);
})
});
app.post('/install', function(req, res) {
const filepath = req.body.filepath;
const dirname = path.dirname(filepath);
app.use(express.static(dirname));
const filename = path.basename(filepath);
ps4_install(filename, res);
});
app.listen(port, function () {
console.log(`PS4 PKG sender listening on port ${port} serving files from ${static_files_path}`);
});
function get_dirs_with_pkgs() {
const pkgs = get_pkgs();
const dirs = {};
for(var i = 0, l = pkgs.length; i < l; ++i){
dirs[pkgs[i].dir] = true;
}
return Object.keys(dirs);
}
function get_devices() {
return find()
devices.forEach(device => {
nodePortScanner(device.ip, [12800])
.then(results => {
d.push(results)
})
.catch(error => {
console.log(error);
});
})
}
function get_pkgs() {
const walkSync = function(dir, filelist) {
const files = fs.readdirSync(dir);
files.forEach(function(file) {
filepath = dir + '/' + file;
const stat = fs.statSync(filepath);
if (stat.isDirectory()) {
filelist = walkSync(filepath, filelist);
} else if (path.extname(file).toLowerCase() === '.pkg') {
filelist.push({
filepath: filepath,
dir: path.dirname(filepath),
name: path.basename(filepath),
size: filesize(stat.size)
});
}
});
return filelist;
};
return walkSync(static_files_path, []);
}
function ps4_install(filename, res) {
const pkg_uri = `http://${local_ip}:${port}/${encodeURI(filename)}`;
const ps4_api_uri = `http://${ps4_ip}:12800/api/install`;
const curl_command = `curl -v "${ps4_api_uri}" --data '{"type":"direct","packages":["${pkg_uri}"]}'`;
res.write(curl_command);
console.log(curl_command);
exec(curl_command, (err, stdout, stderr) => {
if (err) {
res.write(err);
res.end();
console.error(err);
return;
}
res.write(`stdout: ${stdout}`);
console.log(`stdout: ${stdout}`);
res.write(`stderr: ${stderr}`);
console.log(`stderr: ${stderr}`);
res.end();
});
}