Skip to content

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

10.1 KB
Raw Blame
/**
 * PORTS TRACKER - BASE COLLECTOR
 *
 * This is the foundation for all platform-specific collectors.
 * Each collector is responsible for gathering system information,
 * applications, ports, and virtual machines from a specific platform.
 */

const { Logger } = require("../lib/logger");
const { SimpleTTLCache } = require("../utils/cache");

class BaseCollector {
  /**
   * Create a new collector instance
   * @param {Object} config Configuration options
   */
  constructor(config = {}) {
    this.config = config;
    this.debug = config.debug || false;
    this.platform = "generic";
    this.platformName = "Generic Platform";
    
  this.logger = new Logger(this.platformName, { debug: this.debug });
  this._cache = new SimpleTTLCache();
  this.cacheDisabled = process.env.DISABLE_CACHE === 'true' || config.disableCache === true;
  this.defaultCacheTTL = parseInt(process.env.COLLECTOR_CACHE_TTL_MS || '30000', 10);
  }

  /**
   * Get basic system information
   * @returns {Promise<Object>} System information
   */
  async getSystemInfo() {
    throw new Error("Method not implemented: getSystemInfo()");
  }

  /**
   * Get list of running applications/containers
   * @returns {Promise<Array>} List of applications
   */
  async getApplications() {
    throw new Error("Method not implemented: getApplications()");
  }

  /**
   * Get list of open ports
   * @returns {Promise<Array>} List of port entries
   */
  async getPorts() {
    throw new Error("Method not implemented: getPorts()");
  }

  /**
   * Get list of virtual machines
   * @returns {Promise<Array>} List of VMs
   */
  async getVMs() {
    throw new Error("Method not implemented: getVMs()");
  }

  /**
   * Get all data from this collector
   * @returns {Promise<Object>} All collected data
   */
  async collectAll() {
    if (
      this.collect &&
      typeof this.collect === "function" &&
      this.collect !== BaseCollector.prototype.collect
    ) {
      return await this.collect();
    }

    try {
      const [systemInfo, applications, ports, vms] = await Promise.allSettled([
        this.getSystemInfo(),
        this.getApplications(),
        this.getPorts(),
        this.getVMs(),
      ]);

      return {
        platform: this.platform,
        platformName: this.platformName,
        systemInfo: systemInfo.status === "fulfilled" ? systemInfo.value : null,
        applications:
          applications.status === "fulfilled" ? applications.value : [],
        ports: ports.status === "fulfilled" ? ports.value : [],
        vms: vms.status === "fulfilled" ? vms.value : [],
        timestamp: new Date().toISOString(),
        errors: {
          systemInfo:
            systemInfo.status === "rejected" ? systemInfo.reason.message : null,
          applications:
            applications.status === "rejected"
              ? applications.reason.message
              : null,
          ports: ports.status === "rejected" ? ports.reason.message : null,
          vms: vms.status === "rejected" ? vms.reason.message : null,
        },
      };
    } catch (error) {
      this.logger.error('Error collecting all data', { err: error });

      return {
        platform: this.platform,
        platformName: this.platformName,
        systemInfo: null,
        applications: [],
        ports: [],
        vms: [],
        timestamp: new Date().toISOString(),
        errors: {
          general: error.message,
        },
      };
    }
  }

  /**
   * Store detection information for API access
   * @param {Object} info Detection information
   */
  setDetectionInfo(info) {
    this.detectionInfo = info;
  }

  /**
   * Check compatibility (to be implemented by subclasses)
   * @returns {Promise<number>} Confidence score 0-100
   */
  async isCompatible() {
    return 0;
  }

  /**
   * Normalize a port entry to ensure consistent format
   * @param {Object} entry Raw port entry
   * @returns {Object} Normalized port entry
   */
  normalizePortEntry(entry) {
    let hostIp = entry.host_ip || "0.0.0.0";
    if (hostIp === "::" || hostIp === "[::]" || hostIp === "*") {
      hostIp = "0.0.0.0";
    }
    const parsedPid = parseInt(entry.pid, 10);
    const pid = Number.isNaN(parsedPid) ? null : parsedPid;
    const pids = Array.isArray(entry.pids)
      ? entry.pids
          .map((candidatePid) => parseInt(candidatePid, 10))
          .filter((candidatePid) => !Number.isNaN(candidatePid) && candidatePid > 0)
      : pid
        ? [pid]
        : [];
    const primaryPid = pid || pids[0] || null;
    return {
      source: entry.source || this.platform,
      owner: entry.owner || "unknown",
      protocol: entry.protocol || "tcp",
      host_ip: hostIp,
      host_port: parseInt(entry.host_port, 10) || 0,
      pid: primaryPid,
      pids,
      target: entry.target || null,
      container_id: entry.container_id || null,
      vm_id: entry.vm_id || null,
      app_id: entry.app_id || null,
      compose_project: entry.compose_project || null,
      compose_service: entry.compose_service || null,
      created: entry.created || null,
      internal: entry.internal || false,
    };
  }

  /**
   * Info logging helper - always shows important operational information
   * @param {...any} args Arguments to log
   */
  logInfo(...args) {
    this.logger.info(...args);
  }

  /**
   * Debug logging helper - only shows when debug=true
   * @param {...any} args Arguments to log
   */
  log(...args) {
    this.logger.debug(...args);
  }

  /**
   * Error logging helper - always shows errors
   * @param {...any} args Arguments to log
   */
  logError(...args) {
    this.logger.error(...args);
  }

  /**
   * Warning logging helper - always shows warnings
   * @param {...any} args Arguments to log
   */
  logWarn(...args) {
    this.logger.warn(...args);
  }

  /**
   * Generic TTL cache helper for collectors
   * @param {string} key
   * @param {Function} fetchFn async -> value
   * @param {Object} options { ttlMs, forceRefresh }
   */
  async cacheGetOrSet(key, fetchFn, { ttlMs, forceRefresh } = {}) {
    if (this.cacheDisabled) {
      if (this.debug) this.log(`Cache disabled; bypassing for ${key}`);
      return fetchFn();
    }
    const effectiveTTL = typeof ttlMs === 'number' ? ttlMs : this.defaultCacheTTL;
    const namespacedKey = `${this.platform}:${key}`;
    if (!forceRefresh) {
      const cached = this._cache.get(namespacedKey);
      if (cached !== undefined) {
        if (this.debug) this.log(`Cache hit: ${namespacedKey}`);
        return cached;
      }
      if (this.debug) this.log(`Cache miss: ${namespacedKey}`);
    } else if (this.debug) {
      this.log(`Force refresh: ${namespacedKey}`);
    }
    const fresh = await fetchFn();
    this._cache.set(namespacedKey, fresh, effectiveTTL);
    return fresh;
  }

  clearCache(key) {
    const namespacedKey = `${this.platform}:${key}`;
    this._cache.delete(namespacedKey);
  }

  clearAllCache() {
    this._cache.clear();
  }

  /**
   * Extract Docker Swarm / Compose label metadata from a container's label map.
   * @param {Object} labels  The container's Labels object (may be null/undefined)
   * @param {string} containerName  Fallback owner name when no swarm service is present
   * @returns {{ swarmServiceName: string|null, stackNamespace: string|null,
   *             composeProject: string|null, composeService: string|null,
   *             effectiveOwner: string }}
   */
  _extractSwarmLabels(labels, containerName) {
    const swarmServiceName = labels?.['com.docker.swarm.service.name'] || null;
    const stackNamespace   = labels?.['com.docker.stack.namespace']    || null;
    const composeProject   = labels?.['com.docker.compose.project']    || stackNamespace || null;
    let composeService = labels?.['com.docker.compose.service'] || null;
    if (!composeService && swarmServiceName) {
      composeService = (stackNamespace && swarmServiceName.startsWith(stackNamespace + '_'))
        ? swarmServiceName.slice(stackNamespace.length + 1)
        : swarmServiceName;
    }
    return {
      swarmServiceName,
      stackNamespace,
      composeProject,
      composeService,
      effectiveOwner: swarmServiceName || containerName,
    };
  }

  /**
   * Get Docker Swarm service ports (ingress and host publish modes).
   * These ports are managed at the service layer and do not appear in
   * container-level port bindings, so they require separate discovery.
   * Requires `this.dockerApi` to be set by the subclass constructor.
   * @returns {Promise<Array>} Swarm service port entries
   * @private
   */
  async _getSwarmServicePorts() {
    try {
      const systemInfo = await this.dockerApi.getSystemInfo();
      if (systemInfo?.Swarm?.LocalNodeState !== 'active') {
        return [];
      }

      const services = await this.dockerApi.listServices();
      const portEntries = [];

      for (const service of services) {
        const serviceId = service.ID;
        const serviceName = service.Spec?.Name || serviceId;
        const labels = service.Spec?.Labels || {};
        const stackNamespace = labels['com.docker.stack.namespace'] || null;

        let composeService = serviceName;
        if (stackNamespace && serviceName.startsWith(stackNamespace + '_')) {
          composeService = serviceName.slice(stackNamespace.length + 1);
        }

        const ports = service.Endpoint?.Ports || [];
        for (const port of ports) {
          if (!port.PublishedPort) continue;

          portEntries.push(
            this.normalizePortEntry({
              source: 'docker',
              owner: serviceName,
              protocol: (port.Protocol || 'tcp').toLowerCase(),
              host_ip: '0.0.0.0',
              host_port: port.PublishedPort,
              target: `${serviceId.substring(0, 12)}:${port.TargetPort}`,
              container_id: null,
              app_id: serviceName,
              compose_project: stackNamespace,
              compose_service: composeService,
              pids: [],
            })
          );
        }
      }

      this.logInfo(`Collected ${portEntries.length} Swarm service ports`);
      return portEntries;
    } catch (err) {
      this.logWarn('Failed to get Swarm service ports:', err.message);
      return [];
    }
  }
}

module.exports = BaseCollector;