Skip to content

An open source, self-hosted, real-time port monitoring and discovery tool.

fix: refactor IP resolution logic & improve action feedback handling in frontend ui views

Mostafa Wahied25c6128b7494
diff --git a/backend/collectors/truenas_collector.js b/backend/collectors/truenas_collector.js
index 81f77c7..e25d469 100644
--- a/backend/collectors/truenas_collector.js
+++ b/backend/collectors/truenas_collector.js
@@ -50,7 +50,7 @@ class TrueNASCollector extends BaseCollector {
);
}
- // Important ports by service (with correct protocols)
+ // Important ports by service (with protocols)
this.importantPorts = {
51820: { service: "WireGuard", protocol: "udp" },
51821: { service: "WireGuard-UI", protocol: "tcp" },
@@ -473,13 +473,13 @@ class TrueNASCollector extends BaseCollector {
* @returns {string} Resolved IP address
*/
_resolveHostIP(host_ip) {
+ if (host_ip === "0.0.0.0" || host_ip === "::") {
+ return host_ip;
+ }
+
switch (host_ip) {
case "*":
- case "0.0.0.0":
- case "::":
- const resolvedIP = this._getServerIP();
- this.log(`Resolved wildcard '${host_ip}' to: ${resolvedIP}`);
- return resolvedIP;
+ return "0.0.0.0";
case "127.0.0.1":
case "localhost":
return "127.0.0.1";
@@ -489,105 +489,60 @@ class TrueNASCollector extends BaseCollector {
}
/**
- * Get the actual server IP address with TrueNAS IX dedicated IP support
- * @returns {string} Server IP address
+ * @returns {string} Server IP address, now always '0.0.0.0' for wildcard.
*/
_getServerIP() {
+ // Try to get the server IP from the server's own configuration
try {
const os = require("os");
const networkInterfaces = os.networkInterfaces();
- const truenasAppInterfaces = Object.keys(networkInterfaces).filter(
- (name) =>
- name.startsWith("ix-") ||
- name.startsWith("truenas-") ||
- name.includes("app-") ||
- name.includes("scale-")
- );
- for (const interfaceName of truenasAppInterfaces) {
- const addresses = networkInterfaces[interfaceName];
- for (const addr of addresses) {
- if (!addr.internal && addr.family === "IPv4") {
- this.log(
- `Found TrueNAS app interface ${interfaceName}: ${addr.address}`
- );
- return addr.address;
- }
- }
- }
- const mainInterfaces = ["enp", "eth", "ens", "em"]
- .map((prefix) =>
- Object.keys(networkInterfaces).find((name) => name.startsWith(prefix))
- )
- .filter(Boolean);
- for (const interfaceName of mainInterfaces) {
- const addresses = networkInterfaces[interfaceName];
- for (const addr of addresses) {
- if (!addr.internal && addr.family === "IPv4") {
- if (
- addr.address.startsWith("10.") ||
- addr.address.startsWith("192.168.") ||
- (addr.address.startsWith("172.") &&
- !addr.address.startsWith("172.1"))
- ) {
- this.log(
- `Found main interface ${interfaceName}: ${addr.address}`
- );
+
+ // Look for primary network interfaces (not Docker bridges or virtual interfaces)
+ const primaryInterfaces = ['eth0', 'enp0s3', 'enp0s8', 'ens33', 'ens160', 'em0'];
+
+ for (const interfaceName of primaryInterfaces) {
+ if (networkInterfaces[interfaceName]) {
+ const addresses = networkInterfaces[interfaceName];
+ for (const addr of addresses) {
+ if (!addr.internal && addr.family === 'IPv4') {
+ this.log(`Found primary interface ${interfaceName}: ${addr.address}`);
return addr.address;
}
}
}
}
- for (const interfaceName in networkInterfaces) {
- if (
- interfaceName.startsWith("docker") ||
- interfaceName.startsWith("br-") ||
- interfaceName === "bridge"
- ) {
+
+ // Fallback: look for any non-internal, non-Docker interface
+ for (const [interfaceName, addresses] of Object.entries(networkInterfaces)) {
+ // Skip Docker, bridge, and virtual interfaces
+ if (interfaceName.startsWith('docker') ||
+ interfaceName.startsWith('br-') ||
+ interfaceName.startsWith('veth') ||
+ interfaceName === 'lo') {
continue;
}
- const addresses = networkInterfaces[interfaceName];
+
for (const addr of addresses) {
- if (!addr.internal && addr.family === "IPv4") {
- if (
- !addr.address.startsWith("172.16.") &&
- !addr.address.startsWith("172.17.") &&
- !addr.address.startsWith("172.18.") &&
- !addr.address.startsWith("172.19.")
- ) {
- this.log(
- `Found non-Docker interface ${interfaceName}: ${addr.address}`
- );
+ if (!addr.internal && addr.family === 'IPv4') {
+ // Prefer private network IPs
+ if (addr.address.startsWith('192.168.') ||
+ addr.address.startsWith('10.') ||
+ (addr.address.startsWith('172.') &&
+ parseInt(addr.address.split('.')[1]) >= 16 &&
+ parseInt(addr.address.split('.')[1]) <= 31)) {
+ this.log(`Found suitable interface ${interfaceName}: ${addr.address}`);
return addr.address;
}
}
}
}
- const dockerInterfaces = Object.keys(networkInterfaces).filter(
- (name) =>
- name.startsWith("docker") ||
- name.startsWith("br-") ||
- name === "bridge"
- );
- for (const interfaceName of dockerInterfaces) {
- const addresses = networkInterfaces[interfaceName];
- for (const addr of addresses) {
- if (
- !addr.internal &&
- addr.family === "IPv4" &&
- !addr.address.startsWith("172.17.")
- ) {
- this.log(
- `Fallback Docker bridge interface ${interfaceName}: ${addr.address}`
- );
- return addr.address;
- }
- }
- }
+
+ this.logWarn("Could not determine server IP, falling back to 0.0.0.0");
+ return "0.0.0.0";
} catch (error) {
- this.logError("Error resolving server IP:", error.message);
+ this.logError("Error determining server IP:", error.message);
+ return "0.0.0.0";
}
- this.log("Could not resolve server IP, using localhost");
- return "127.0.0.1";
}
/**
@@ -628,7 +583,7 @@ class TrueNASCollector extends BaseCollector {
let resolvedHostIP;
if (address === "0.0.0.0" || address === "::") {
- resolvedHostIP = this._getServerIP();
+ resolvedHostIP = "0.0.0.0";
} else {
resolvedHostIP = address;
}
@@ -1703,7 +1658,7 @@ class TrueNASCollector extends BaseCollector {
this.log("=== End Network Debug ===");
const context = this._detectNetworkContext();
this.log("Network context:", context);
- const resolvedIP = this._getServerIP();
+ const resolvedIP = "0.0.0.0";
this.log(`Final resolved IP: ${resolvedIP}`);
} catch (error) {
this.logError("Error debugging network interfaces:", error.message);
diff --git a/backend/index.js b/backend/index.js
index 2de7203..f60e5fe 100644
--- a/backend/index.js
+++ b/backend/index.js
@@ -365,36 +365,58 @@ app.get("/api/servers/:id/scan", async (req, res) => {
);
}
- const peerResponse = await fetch(peerScanUrl, { timeout: 15000 });
-
- if (!peerResponse.ok) {
- let errorBody = "Peer responded with an error.";
- try {
- errorBody = await peerResponse.text();
- } catch (e) {
- /* ignore */
- }
- console.warn(
- `[Server] WARN: [GET /api/servers/${serverId}/scan] Peer server at ${server.url} responded with status ${peerResponse.status}. Body: ${errorBody}`
- );
- return res.status(peerResponse.status).json({
- error: `Peer server scan failed with status ${peerResponse.status}`,
- details: errorBody,
- serverId: serverId,
- peerUrl: server.url,
+ const controller = new AbortController();
+ const timeoutId = setTimeout(() => controller.abort(), 15000);
+
+ try {
+ const peerResponse = await fetch(peerScanUrl, {
+ signal: controller.signal
});
- }
+ clearTimeout(timeoutId);
+
+ if (!peerResponse.ok) {
+ let errorBody = "Peer responded with an error.";
+ try {
+ errorBody = await peerResponse.text();
+ } catch (e) {
+ /* ignore */
+ }
+ console.warn(
+ `[Server] WARN: [GET /api/servers/${serverId}/scan] Peer server at ${server.url} responded with status ${peerResponse.status}. Body: ${errorBody}`
+ );
+ return res.status(peerResponse.status).json({
+ error: `Peer server scan failed with status ${peerResponse.status}`,
+ details: errorBody,
+ serverId: serverId,
+ peerUrl: server.url,
+ });
+ }
- const peerScanData = await peerResponse.json();
- if (currentDebug) {
+ const peerScanData = await peerResponse.json();
+ if (currentDebug) {
+ console.log(
+ `[Server] DEBUG: [GET /api/servers/${serverId}/scan] Successfully received scan data from peer ${serverId}`
+ );
+ }
console.log(
- `[Server] DEBUG: [GET /api/servers/${serverId}/scan] Successfully received scan data from peer ${serverId}`
+ `[Server] INFO: [GET /api/servers/${serverId}/scan] Successfully scanned remote peer. Peer: ${server.label} (${serverId})`
);
+ return res.json(peerScanData);
+ } catch (fetchError) {
+ clearTimeout(timeoutId);
+ if (fetchError.name === 'AbortError') {
+ console.error(
+ `[Server] ERROR: [GET /api/servers/${serverId}/scan] Timeout after 15s communicating with peer ${server.label} at ${server.url}`
+ );
+ return res.status(408).json({
+ error: "Request timeout - peer server took too long to respond",
+ details: "Connection timed out after 15 seconds",
+ serverId: serverId,
+ peerUrl: server.url,
+ });
+ }
+ throw fetchError;
}
- console.log(
- `[Server] INFO: [GET /api/servers/${serverId}/scan] Successfully scanned remote peer. Peer: ${server.label} (${serverId})`
- );
- return res.json(peerScanData);
} catch (fetchError) {
console.error(
`[Server] ERROR: [GET /api/servers/${serverId}/scan] Failed to fetch scan data from peer ${server.label} at ${server.url}:`,
diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx
index 55abcc5..22c1fa5 100644
--- a/frontend/src/App.jsx
+++ b/frontend/src/App.jsx
@@ -32,8 +32,10 @@ export default function App() {
const [draftNote, setDraftNote] = useState("");
const [actionFeedback, setActionFeedback] = useState({
- type: null,
- id: null,
+ copy: null,
+ edit: null,
+ hide: null,
+ unhide: null,
});
const [isDarkMode, setIsDarkMode] = useState(() => {
@@ -904,9 +906,15 @@ export default function App() {
navigator.clipboard
.writeText(urlToCopy)
.then(() => {
- setActionFeedback({ type: "copy", id: keyOf(server.id, p) });
+ setActionFeedback(prev => ({
+ ...prev,
+ copy: { id: keyOf(server.id, p) }
+ }));
setTimeout(
- () => setActionFeedback({ type: null, id: null }),
+ () => setActionFeedback(prev => ({
+ ...prev,
+ copy: null
+ })),
1500
);
})
@@ -1059,8 +1067,14 @@ export default function App() {
try {
const successful = document.execCommand("copy");
if (successful) {
- setActionFeedback({ type: "copy", id: keyOf(serverId, port) });
- setTimeout(() => setActionFeedback({ type: null, id: null }), 1500);
+ setActionFeedback(prev => ({
+ ...prev,
+ copy: { id: keyOf(serverId, port) }
+ }));
+ setTimeout(() => setActionFeedback(prev => ({
+ ...prev,
+ copy: null
+ })), 1500);
} else {
prompt("Copy this URL:", text);
}
diff --git a/frontend/src/components/server/ActionButton.jsx b/frontend/src/components/server/ActionButton.jsx
index 9297297..0b65187 100644
--- a/frontend/src/components/server/ActionButton.jsx
+++ b/frontend/src/components/server/ActionButton.jsx
@@ -16,7 +16,7 @@ export function ActionButton({
const isLoading =
loading &&
feedback &&
- feedback.key === itemKey &&
+ feedback.id === itemKey &&
feedback.status === "loading";
const sizeClasses = {
@@ -29,13 +29,13 @@ export function ActionButton({
sm: "h-3.5 w-3.5",
};
- const baseClasses = "relative rounded-md transition-colors";
+ const baseClasses = "relative rounded-md transition-all duration-200 transform";
const colorClasses = wasClicked
- ? "bg-green-100 text-green-700 dark:bg-green-800/50 dark:text-green-300"
+ ? "bg-green-100 text-green-700 dark:bg-green-800/50 dark:text-green-300 scale-105"
: isLoading
- ? "bg-blue-100 text-blue-600 dark:bg-blue-800/50 dark:text-blue-300 cursor-not-allowed"
- : "bg-slate-100 text-slate-600 hover:bg-slate-200 dark:bg-slate-700 dark:text-slate-300 dark:hover:bg-slate-600";
+ ? "bg-blue-100 text-blue-600 dark:bg-blue-800/50 dark:text-blue-300 cursor-not-allowed animate-pulse"
+ : "bg-slate-100 text-slate-600 hover:bg-slate-200 hover:scale-105 active:scale-95 dark:bg-slate-700 dark:text-slate-300 dark:hover:bg-slate-600";
return (
<button
@@ -47,8 +47,8 @@ export function ActionButton({
<span
className={
wasClicked || isLoading
- ? "opacity-0"
- : "opacity-100 transition-opacity"
+ ? "opacity-0 transition-opacity duration-150"
+ : "opacity-100 transition-opacity duration-150"
}
>
<Icon className={iconSizeClasses[size]} />
@@ -61,8 +61,8 @@ export function ActionButton({
)}
{wasClicked && (
- <span className={`absolute inset-0 flex items-center justify-center`}>
- <Check className={iconSizeClasses[size]} />
+ <span className="absolute inset-0 flex items-center justify-center">
+ <Check className={`${iconSizeClasses[size]} text-green-600 dark:text-green-400`} />
</span>
)}
</button>
diff --git a/frontend/src/components/server/PortCard.jsx b/frontend/src/components/server/PortCard.jsx
index 4e72049..0b4bc81 100644
--- a/frontend/src/components/server/PortCard.jsx
+++ b/frontend/src/components/server/PortCard.jsx
@@ -50,19 +50,18 @@ function PortCardComponent({
const shouldHighlight = !!searchTerm;
let hostForUi;
- if (
- serverId &&
- serverId !== "local" &&
- serverUrl &&
- (port.host_ip === "0.0.0.0" || port.host_ip === "127.0.0.1")
- ) {
- try {
- hostForUi = new URL(serverUrl).hostname;
- } catch {
+ if (port.host_ip === "0.0.0.0" || port.host_ip === "127.0.0.1") {
+ if (serverId === "local") {
+ hostForUi = window.location.hostname;
+ } else if (serverUrl) {
+ try {
+ hostForUi = new URL(serverUrl).hostname;
+ } catch {
+ hostForUi = "localhost";
+ }
+ } else {
hostForUi = "localhost";
}
- } else if (port.host_ip === "0.0.0.0" || port.host_ip === "127.0.0.1") {
- hostForUi = "localhost";
} else {
hostForUi = port.host_ip;
}
diff --git a/frontend/src/components/server/PortGridItem.jsx b/frontend/src/components/server/PortGridItem.jsx
index df9dd8a..72fd071 100644
--- a/frontend/src/components/server/PortGridItem.jsx
+++ b/frontend/src/components/server/PortGridItem.jsx
@@ -49,19 +49,18 @@ export function PortGridItem({
const shouldHighlight = !!searchTerm;
let hostForUi;
- if (
- serverId &&
- serverId !== "local" &&
- serverUrl &&
- (port.host_ip === "0.0.0.0" || port.host_ip === "127.0.0.1")
- ) {
- try {
- hostForUi = new URL(serverUrl).hostname;
- } catch {
+ if (port.host_ip === "0.0.0.0" || port.host_ip === "127.0.0.1") {
+ if (serverId === "local") {
+ hostForUi = window.location.hostname;
+ } else if (serverUrl) {
+ try {
+ hostForUi = new URL(serverUrl).hostname;
+ } catch {
+ hostForUi = "localhost";
+ }
+ } else {
hostForUi = "localhost";
}
- } else if (port.host_ip === "0.0.0.0" || port.host_ip === "127.0.0.1") {
- hostForUi = "localhost";
} else {
hostForUi = port.host_ip;
}
diff --git a/frontend/src/components/server/PortTableRow.jsx b/frontend/src/components/server/PortTableRow.jsx
index 84290f4..5fa16f1 100644
--- a/frontend/src/components/server/PortTableRow.jsx
+++ b/frontend/src/components/server/PortTableRow.jsx
@@ -49,19 +49,18 @@ function PortTableRowComponent({
const shouldHighlight = !!searchTerm;
let hostForUi;
- if (
- serverId &&
- serverId !== "local" &&
- serverUrl &&
- (port.host_ip === "0.0.0.0" || port.host_ip === "127.0.0.1")
- ) {
- try {
- hostForUi = new URL(serverUrl).hostname;
- } catch {
+ if (port.host_ip === "0.0.0.0" || port.host_ip === "127.0.0.1") {
+ if (serverId === "local") {
+ hostForUi = window.location.hostname;
+ } else if (serverUrl) {
+ try {
+ hostForUi = new URL(serverUrl).hostname;
+ } catch {
+ hostForUi = "localhost";
+ }
+ } else {
hostForUi = "localhost";
}
- } else if (port.host_ip === "0.0.0.0" || port.host_ip === "127.0.0.1") {
- hostForUi = "localhost";
} else {
hostForUi = port.host_ip;
}