-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyper-server-bonjour.js
More file actions
executable file
·116 lines (107 loc) · 2.89 KB
/
hyper-server-bonjour.js
File metadata and controls
executable file
·116 lines (107 loc) · 2.89 KB
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
/**
* Hyper Server Reboot
*
* Trying out self-discovery over TCP using Bonjour Protocol
*/
import os from "node:os";
import { execSync } from "node:child_process";
import { createServer } from "node:http";
import { Bonjour } from "bonjour-service";
/**
* Get local IP address
* @returns {String}
*/
const getLocalIP = () => {
const list = os.networkInterfaces();
for(const name in list) {
const device = list[name];
for(const info of device) {
if(info.family === "IPv4" && !info.internal) {
return info.address;
}
}
}
return false;
};
/**
* Get random unused port between the givenn range
* @param {Number} from
* @param {Number} to
* @returns {Number[]}
*/
const getUnusedPorts = (from = 3000, to = 65535, limit = null, shuffle = false) => {
let cmd = "";
switch(os.platform()) {
case "darwin":
cmd = `netstat -anv | grep LISTEN | awk '{print $4}' | cut -d'.' -f5 | sort -u`;
break;
case "win32":
cmd = `netstat -ano | findstr LISTENING | awk '{print $2}' | cut -d':' -f2 | sort /unique`;
break;
case "linux":
cmd = `ss -Htan | awk '{print $4}' | cut -d':' -f2 | sort -u`;
break;
}
let unusedPorts = [];
let count = 0;
try {
const usedPorts = execSync(cmd)
.toString()
.split("\n")
.map(Number)
.filter(Boolean);
for(let port = from; port <= to; port++) {
if (limit && count >= limit) {
break;
}
if (!usedPorts.includes(port)) {
unusedPorts.push(port);
count++;
}
}
if (shuffle) {
const randomIndex = Math.floor(Math.random() * unusedPorts.length);
return [unusedPorts[randomIndex]];
}
}
catch (error) {
console.error(error);
}
return unusedPorts;
};
/**
* Get a random unused port between the given range
* @param {Number} from
* @param {Number} to
* @returns {Number}
*/
const getRandomUnusedPort = (from = 3000, to = 65535) => {
const ports = getUnusedPorts(from, to, 1, true);
return ports.length && ports[0];
};
const HOSTNAME = "0.0.0.0";
const LOCAL_IP = getLocalIP();
const PORT = getRandomUnusedPort(3000, 9000);
// Create a simple web server
const server = createServer((request, response) => {
console.log(`New request to the web server: ${request.url}`);
response.writeHead(200, { 'Content-Type': "text/plain" });
response.end("RAVERS GONNA RAVE!");
});
// Bind to a port and start listening
server.listen(PORT, HOSTNAME, () => {
console.log(`Started Web Server on: ${"0.0.0.0" !== HOSTNAME ? HOSTNAME : LOCAL_IP}:${PORT}`);
});
// Start up a Bonjour Service
const bonjour = new Bonjour();
// Advertise itself as "hyper-server"
bonjour.publish({
name: "hyper-server",
type: "http",
port: PORT,
host: LOCAL_IP
});
// Lookup instances like self
bonjour.find({ type: "http" }, service => {
console.log(`Discovered ${service.name} on: http://${service.host}:${service.port}`);
});