Skip to content

Commit

Permalink
Expose Fedatrion port (#3974)
Browse files Browse the repository at this point in the history
* Add

* Phase 1

* Add

* Add

* Update index.ts

* Add

* Last fix

* Remove

* Update shiinobi.ts

* Update index.ts

* Add
  • Loading branch information
baseplate-admin authored Aug 13, 2024
1 parent 51d6ed5 commit b168fff
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 10 deletions.
88 changes: 82 additions & 6 deletions seeder/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion seeder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"@electron-toolkit/utils": "^3.0.0",
"cors": "^2.8.5",
"electron-updater": "^6.1.7",
"express": "^4.19.2"
"express": "^4.19.2",
"local-devices": "^4.0.0"
},
"devDependencies": {
"@coreproject-moe/icons": "^0.0.13",
Expand Down
4 changes: 4 additions & 0 deletions seeder/src/main/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ app.get("/", (_, res) => {
res.send("Hello World!");
});

app.get("/shiinobi-healthcheck", (_, res) => {
res.send("We are friends");
});

export { app };
35 changes: 32 additions & 3 deletions seeder/src/main/utils/port.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
import net, { AddressInfo } from "node:net";
import find from "local-devices";

// Range : 49152 – 65535
const PORT_START = 49152,
PORT_END = 65535;

export function get_free_port() {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const server = net.createServer();
server.listen(0, () => {
const port = (server.address() as AddressInfo)?.port; // Default to 0 if address is not AddressInfo
let port = Math.floor(Math.random() * (PORT_START - PORT_END + 1)) + PORT_START;

// Try to listen on a random port within the range
server.listen(port, () => {
port = (server.address() as AddressInfo)?.port;
server.close(() => resolve(port));
});

// Handle errors (e.g., port already in use)
server.on("error", () => {
// Retry with another port if the current one is unavailable
get_free_port().then(resolve).catch(reject);
});
});
}
export async function get_all_devices_running_shiinobi() {
let friends = new Array<string>();
const devices = await find();

for (const device of devices) {
for (let i = PORT_START; i < PORT_END; i++) {
const URL = `https://${device.ip}/shiinobi-healthcheck`;
const res = await fetch(URL);
const text = await res.text();
if (text === "We are friends") {
friends.push(device.ip);
}
}
}
}

0 comments on commit b168fff

Please sign in to comment.