Compare commits
10 Commits
49435277bb
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| ebf5479250 | |||
| 11f3d01a55 | |||
| b0c9a10da8 | |||
| 502957a3e7 | |||
| 1bdd45660e | |||
| e2f2074cea | |||
| df6b97b679 | |||
| 5fcb7d316f | |||
| b14ca0b7be | |||
| 73f58a62b6 |
+87
-125
@@ -3,7 +3,6 @@ const path = require('path');
|
|||||||
const exec = require("child_process").execSync;
|
const exec = require("child_process").execSync;
|
||||||
|
|
||||||
var config = null;
|
var config = null;
|
||||||
const leaseTime = 3600;
|
|
||||||
|
|
||||||
function clone(obj) {
|
function clone(obj) {
|
||||||
return JSON.parse(JSON.stringify(obj));
|
return JSON.parse(JSON.stringify(obj));
|
||||||
@@ -48,6 +47,7 @@ async function readFile(file, { format = 'utf8' } = {}) {
|
|||||||
async function loadConfig({ configPath = __dirname, configName = 'config.json' } = {}) {
|
async function loadConfig({ configPath = __dirname, configName = 'config.json' } = {}) {
|
||||||
let configDefault = {
|
let configDefault = {
|
||||||
"instancesPath": "",
|
"instancesPath": "",
|
||||||
|
"ip": ""
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -66,46 +66,78 @@ async function saveConfig({ configPath = __dirname, configName = 'config.json' }
|
|||||||
await writeFile(cleanPath(configPath), cleanPath(configName), JSON.stringify(config, null, 4));
|
await writeFile(cleanPath(configPath), cleanPath(configName), JSON.stringify(config, null, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPorts(instancesJSON) {
|
|
||||||
portList = [];
|
|
||||||
|
|
||||||
instances = JSON.parse(instancesJSON);
|
function run(command, consoleOutput = false) {
|
||||||
for (const instance of instances) {
|
let convertOutput = exec(command, function (error, stdout, stderr) {
|
||||||
|
if (error) {
|
||||||
|
if (consoleOutput) {
|
||||||
|
console.log(`error: ${error.message}`);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (stderr) {
|
||||||
|
if (consoleOutput) {
|
||||||
|
console.log(`stderr: ${stderr}`);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (consoleOutput) {
|
||||||
|
console.log(`stdout: ${stdout}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return convertOutput.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
await loadConfig();
|
||||||
|
|
||||||
|
const instancesJSON = await readFile(config.instancesPath);
|
||||||
|
let instancesPorts = "";
|
||||||
|
const upnpPortList = run('upnpc -l');
|
||||||
|
// console.log(upnpPortList);
|
||||||
|
|
||||||
|
for (const instance of JSON.parse(instancesJSON)) {
|
||||||
if (instance.DeploymentArgs['GenericModule.App.Ports'] != undefined) {
|
if (instance.DeploymentArgs['GenericModule.App.Ports'] != undefined) {
|
||||||
|
|
||||||
instancePorts = JSON.parse(instance.DeploymentArgs['GenericModule.App.Ports']);
|
|
||||||
|
|
||||||
// 0 - TCP; 1 = UDP; 2 - Both
|
// 0 - TCP; 1 = UDP; 2 - Both
|
||||||
for (const instancePort of instancePorts) {
|
for (const instancePort of JSON.parse(instance.DeploymentArgs['GenericModule.App.Ports'])) {
|
||||||
|
let Name = `AMP ${instance.FriendlyName} ${instancePort.Name}`;
|
||||||
|
let Number = instancePort.Port;
|
||||||
|
instancesPorts += `${Name}\n`;
|
||||||
|
|
||||||
|
if (!upnpPortList.includes(Name)) {
|
||||||
switch (instancePort.Protocol) {
|
switch (instancePort.Protocol) {
|
||||||
case 0:
|
case 0:
|
||||||
portList.push({
|
try {
|
||||||
Name: `${instance.FriendlyName} ${instancePort.Name}`,
|
run(`upnpc -e "${Name}" -a ${config.ip} ${Number} ${Number} TCP`);
|
||||||
Protocol: "TCP",
|
console.log(`Port added: "${Name}" ${Number} TCP`);
|
||||||
Number: instancePort.Port
|
} catch {
|
||||||
});
|
console.log(`Port failed: "${Name}" ${Number} TCP`);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
portList.push({
|
try {
|
||||||
Name: `${instance.FriendlyName} ${instancePort.Name}`,
|
run(`upnpc -e "${Name}" -a ${config.ip} ${Number} ${Number} UDP`);
|
||||||
Protocol: "UDP",
|
console.log(`Port added: "${Name}" ${Number} UDP`);
|
||||||
Number: instancePort.Port
|
} catch {
|
||||||
});
|
console.log(`Port failed: "${Name}" ${Number} UDP`);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
portList.push({
|
try {
|
||||||
Name: `${instance.FriendlyName} ${instancePort.Name}`,
|
run(`upnpc -e "${Name}" -a ${config.ip} ${Number} ${Number} TCP`);
|
||||||
Protocol: "TCP",
|
console.log(`Port added: "${Name}" ${Number} TCP`);
|
||||||
Number: instancePort.Port
|
} catch {
|
||||||
});
|
console.log(`Port failed: "${Name}" ${Number} TCP`);
|
||||||
|
}
|
||||||
portList.push({
|
try {
|
||||||
Name: `${instance.FriendlyName} ${instancePort.Name}`,
|
run(`upnpc -e "${Name}" -a ${config.ip} ${Number} ${Number} UDP`);
|
||||||
Protocol: "UDP",
|
console.log(`Port added: "${Name}" ${Number} UDP`);
|
||||||
Number: instancePort.Port
|
} catch {
|
||||||
});
|
console.log(`Port failed: "${Name}" ${Number} UDP`);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -115,106 +147,36 @@ function getPorts(instancesJSON) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return portList;
|
if (instance.DeploymentArgs['FileManagerPlugin.SFTP.SFTPPortNumber'] != undefined) {
|
||||||
|
let Name = `AMP ${instance.FriendlyName} SFTP`;
|
||||||
|
let Number = instance.DeploymentArgs['FileManagerPlugin.SFTP.SFTPPortNumber'];
|
||||||
|
instancesPorts += `${Name}\n`;
|
||||||
|
|
||||||
|
if (!upnpPortList.includes(Name)) {
|
||||||
|
try {
|
||||||
|
run(`upnpc -e "${Name}" -a ${config.ip} ${Number} ${Number} TCP`);
|
||||||
|
console.log(`Port added: "${Name}" ${Number} TCP`);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`Port failed: "${Name}" ${Number} TCP`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUPnPPorts(){
|
|
||||||
let convertOutput = exec(`upnpc -l`, function (error, stdout, stderr) {
|
|
||||||
if (error) {
|
|
||||||
log(`error: ${error.message}`);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (stderr) {
|
|
||||||
log(`stderr: ${stderr}`);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
log(`stdout: ${stdout}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
log(convertOutput.toString());
|
|
||||||
|
|
||||||
return `upnpc: miniupnpc library test client, version 2.2.8.
|
|
||||||
(c) 2005-2024 Thomas Bernard.
|
|
||||||
More information at https://miniupnp.tuxfamily.org/ or http://miniupnp.free.fr/
|
|
||||||
|
|
||||||
List of UPNP devices found on the network :
|
|
||||||
desc: http://192.168.0.10:80/description.xml
|
|
||||||
st: urn:schemas-upnp-org:device:basic:1
|
|
||||||
|
|
||||||
desc: http://192.168.0.10:80/description.xml
|
|
||||||
st: uuid:2f402f80-da50-11e1-9b23-001788a03071
|
|
||||||
|
|
||||||
desc: http://192.168.0.10:80/description.xml
|
|
||||||
st: upnp:rootdevice
|
|
||||||
|
|
||||||
desc: http://192.168.0.1:1900/rootDesc.xml
|
|
||||||
st: urn:schemas-upnp-org:device:InternetGatewayDevice:1
|
|
||||||
|
|
||||||
Found valid IGD : http://192.168.0.1:1900/ctl/IPConn
|
|
||||||
Local LAN ip address : 192.168.0.107
|
|
||||||
Connection Type : IP_Routed
|
|
||||||
Status : Connected, uptime=572779s, LastConnectionError : ERROR_NONE
|
|
||||||
Time started : Thu Nov 20 23:02:36 2025
|
|
||||||
MaxBitRateDown : 8388608 bps (8.3 Mbps) MaxBitRateUp 4194304 bps (4.1 Mbps)
|
|
||||||
ExternalIPAddress = 78.9.235.162
|
|
||||||
i protocol exPort->inAddr:inPort description remoteHost leaseTime
|
|
||||||
0 TCP 11342->192.168.0.69:32400 'Plex Media Server' '' 380785
|
|
||||||
1 TCP 41341->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
2 UDP 47097->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
3 TCP 11343->192.168.0.150:32400 'Plex Media Server' '' 454675
|
|
||||||
4 TCP 11344->192.168.0.69:32400 'Plex Media Server' '' 454708
|
|
||||||
5 TCP 11345->192.168.0.150:32400 'Plex Media Server' '' 454764
|
|
||||||
6 TCP 11346->192.168.0.69:32400 'Plex Media Server' '' 454769
|
|
||||||
7 TCP 11347->192.168.0.150:32400 'Plex Media Server' '' 455502
|
|
||||||
8 TCP 49808->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
9 UDP 44219->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
10 TCP 11348->192.168.0.69:32400 'Plex Media Server' '' 458461
|
|
||||||
11 TCP 47632->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
12 UDP 43787->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
13 TCP 11349->192.168.0.150:32400 'Plex Media Server' '' 459603
|
|
||||||
14 TCP 11350->192.168.0.69:32400 'Plex Media Server' '' 459617
|
|
||||||
15 TCP 43654->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
16 UDP 45931->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
17 TCP 11353->192.168.0.150:32400 'Plex Media Server' '' 496311
|
|
||||||
18 TCP 44341->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
19 UDP 46373->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
20 TCP 11354->192.168.0.69:32400 'Plex Media Server' '' 496317
|
|
||||||
21 TCP 30000->192.168.0.107:30000 'Empyrion' '' 0
|
|
||||||
22 UDP 30000->192.168.0.107:30000 'Empyrion' '' 0
|
|
||||||
23 UDP 30001->192.168.0.107:30001 'Empyrion' '' 0
|
|
||||||
24 UDP 30002->192.168.0.107:30002 'Empyrion' '' 0
|
|
||||||
25 UDP 30003->192.168.0.107:30003 'Empyrion' '' 0
|
|
||||||
26 TCP 30001->192.168.0.107:30001 'Empyrion' '' 0
|
|
||||||
27 TCP 30002->192.168.0.107:30002 'Empyrion' '' 0
|
|
||||||
28 TCP 41655->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
29 UDP 46348->192.168.0.150:63077 'qBittorrent/5.1.4' '' 0
|
|
||||||
30 TCP 11355->192.168.0.150:32400 'NAT-PMP 11355 tcp' '' 18608
|
|
||||||
31 TCP 11356->192.168.0.69:32400 'Plex Media Server' '' 537039
|
|
||||||
32 TCP 11357->192.168.0.150:32400 'Plex Media Server' '' 537058
|
|
||||||
33 TCP 11358->192.168.0.69:32400 'Plex Media Server' '' 537065
|
|
||||||
34 UDP 41642->192.168.0.110:41641 'NAT-PMP 41642 udp' '' 5043
|
|
||||||
35 UDP 41641->192.168.0.69:41641 'NAT-PMP 41641 udp' '' 5288
|
|
||||||
36 UDP 22437->192.168.0.69:22437 'Parsec' '' 1434
|
|
||||||
37 UDP 22438->192.168.0.69:22438 'Parsec' '' 1434
|
|
||||||
38 UDP 22439->192.168.0.69:22439 'Parsec' '' 1434
|
|
||||||
39 UDP 22440->192.168.0.69:22440 'Parsec' '' 1434
|
|
||||||
40 UDP 31437->192.168.0.69:31437 'Parsec' '' 1445
|
|
||||||
41 TCP 11361->192.168.0.20:11361 'NAT-PMP 11361 tcp' '' 3158
|
|
||||||
42 UDP 11361->192.168.0.20:11361 'NAT-PMP 11361 udp' '' 3158
|
|
||||||
43 TCP 63077->192.168.0.69:63077 'NAT-PMP 63077 tcp' '' 3361
|
|
||||||
44 UDP 63077->192.168.0.69:63077 'NAT-PMP 63077 udp' '' 3361
|
|
||||||
45 TCP 36439->192.168.0.69:36439 'NAT-PMP 36439 tcp' '' 3504
|
|
||||||
46 UDP 36439->192.168.0.69:36439 'NAT-PMP 36439 udp' '' 3503
|
|
||||||
47 TCP 30003->192.168.0.107:30003 'Empyrion' '' 69`
|
|
||||||
}
|
}
|
||||||
|
// Delete all AMP
|
||||||
|
// for (const portMap of upnpPortList.split('\n')) {
|
||||||
|
// if (portMap.includes("AMP")) {
|
||||||
|
// let found = portMap.match(/ *\d+ +(\w+) +(\d+).*/);
|
||||||
|
// run(`upnpc -d ${found[2]} ${found[1]}`);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
async function main() {
|
for (const portMap of upnpPortList.split('\n')) {
|
||||||
await loadConfig();
|
if (portMap.includes("AMP") && !instancesPorts.includes(portMap.split("'")[1])) {
|
||||||
|
let found = portMap.match(/ *\d+ +(\w+) +(\d+).*/);
|
||||||
portList = getPorts(await readFile(config.instancesPath));
|
console.log(`Remove ${found[2]} ${found[1]}`);
|
||||||
console.log(portList);
|
run(`upnpc -d ${found[2]} ${found[1]}`);
|
||||||
|
}
|
||||||
upnpPorts = getUPnPPorts()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
Reference in New Issue
Block a user