-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add * Phase 1 * Add * Add * Update index.ts * Add * Last fix * Remove * Update shiinobi.ts * Update index.ts * Add
- Loading branch information
1 parent
51d6ed5
commit b168fff
Showing
4 changed files
with
120 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |