Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
schettn committed Jun 26, 2024
1 parent ea4a006 commit ee10d0b
Showing 1 changed file with 29 additions and 50 deletions.
79 changes: 29 additions & 50 deletions src/services/network-scanner.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { logger } from "@getcronit/pylon";
import ip from "ip";

import {
connect as connectInsecure,
createServer as createInsecureServer,
} from "net";
import { connect as connectSecure } from "tls";
import * as http from "http";
import * as https from "https";

type NetworkScannerDiscovery = Array<{
host: string;
Expand Down Expand Up @@ -56,7 +53,7 @@ export class NetworkScanner {

console.log("Scanning networks...");

console.log("RES", await Promise.all(promises));
await Promise.all(promises);

console.log("Network scan complete");

Expand All @@ -75,56 +72,38 @@ export class NetworkScanner {
private async checkPort(host: string, port: number): Promise<void> {
const connect = (isSecure: boolean) => {
return new Promise<boolean>((resolve) => {
const socket = isSecure
? connectSecure({
host,
port,
rejectUnauthorized: false,
})
: connectInsecure({
host,
port,
});

let timeoutId: Timer;

socket.setTimeout(1000);

socket.on(isSecure ? "secureConnect" : "connect", () => {
this.results[host].push({ port, isSecure });
console.log("Res", this.results[host]);

socket.destroy();
resolve(true);
clearTimeout(timeoutId);
console.log(`Port ${port} is open on ${host}`);
});

socket.on("timeout", () => {
console.log("Timeout", host, port);
socket.destroy();
const request = (isSecure ? https : http).request(
{
host,
port,
method: "HEAD", // Use HEAD method for faster response without full content
timeout: 1000, // Timeout in milliseconds
rejectUnauthorized: false, // Ignore SSL certificate errors
},
(response) => {
// If the response is received, the port is open
console.log(`Port ${port} is open on ${host}`);
this.results[host].push({ port, isSecure });
resolve(true);
}
);

request.on("error", (error) => {
// If an error occurs or the connection times out, the port is closed
console.error(`Port ${port} is closed on ${host}: ${error.message}`);
resolve(false);
clearTimeout(timeoutId);

console.log(`Port ${port} is closed on ${host}`);
});

socket.on("error", (msg) => {
console.log("Error", msg, host, port);
socket.destroy();
request.on("timeout", () => {
// If the request times out, consider the port closed
console.error(
`Timeout occurred while testing port ${port} on ${host}`
);
request.abort();
resolve(false);
clearTimeout(timeoutId);

console.log(`Port ${port} is closed on ${host}`);
});

// Set the timeoutId to clear the setTimeout on connection or error
timeoutId = setTimeout(() => {
console.log(`Timeout occurred (setTimeout) on ${host}:${port}`);
socket.end();
socket.destroy();
resolve(false);
}, 5000);
request.end();
});
};

Expand Down

0 comments on commit ee10d0b

Please sign in to comment.