ssb_metadata.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const fs = require('fs');
  2. const os = require('os');
  3. const path = require('path');
  4. const pkg = require('./package.json');
  5. const config = require('./ssb_config');
  6. const updater = require('../backend/updater.js');
  7. let printed = false;
  8. let checkedForUpdate = false;
  9. function getModules() {
  10. const nodeModulesPath = path.resolve(__dirname, 'node_modules');
  11. try {
  12. return fs.readdirSync(nodeModulesPath)
  13. .filter(m => fs.existsSync(path.join(nodeModulesPath, m, 'package.json')));
  14. } catch {
  15. return [];
  16. }
  17. }
  18. const colors = {
  19. blue: '\x1b[38;5;33m',
  20. yellow: '\x1b[38;5;226m',
  21. orange: '\x1b[38;5;214m',
  22. cyan: '\x1b[36m',
  23. reset: '\x1b[0m'
  24. };
  25. async function checkForUpdate() {
  26. if (checkedForUpdate) return;
  27. checkedForUpdate = true;
  28. const updateFlagPath = path.join(__dirname, '../server/.update_required');
  29. if (fs.existsSync(updateFlagPath)) {
  30. fs.unlinkSync(updateFlagPath);
  31. }
  32. await updater.getRemoteVersion();
  33. }
  34. async function printMetadata(mode, modeColor = colors.cyan, httpPort = 3000, httpHost = 'localhost', offline = false, isPublic = false) {
  35. if (printed) return;
  36. printed = true;
  37. const modules = getModules();
  38. const version = pkg.version;
  39. const name = pkg.name;
  40. const logLevel = config.logging?.level || 'info';
  41. const publicKey = config.keys?.public || '';
  42. const hasHttp = httpPort !== null && httpPort !== false;
  43. const httpUrl = hasHttp ? `http://${httpHost}:${httpPort}` : '';
  44. const oscLink = hasHttp ? `\x1b]8;;${httpUrl}\x07${httpUrl}\x1b]8;;\x07` : '';
  45. const ssbPort = config.connections?.incoming?.net?.[0]?.port || config.port || 8008;
  46. const localDiscovery = config.local === true;
  47. const hops = config.conn?.hops ?? config.friends?.hops ?? 2;
  48. console.log("=========================");
  49. console.log(`Running mode: ${modeColor}${mode}${colors.reset}`);
  50. console.log("=========================");
  51. console.log(`- Package: ${colors.blue}${name} ${colors.yellow}[Version: ${version}]${colors.reset}`);
  52. if (hasHttp) console.log(`- URL: ${colors.cyan}${oscLink}${colors.reset}`);
  53. console.log(`- Oasis ID: [ ${colors.orange}@${publicKey}${colors.reset} ]`);
  54. console.log("- Logging Level:", logLevel);
  55. const ifaces = os.networkInterfaces();
  56. const isOnline = Object.values(ifaces).some(list =>
  57. list && list.some(i => !i.internal && i.family === 'IPv4')
  58. );
  59. console.log(`- Protocol (port): ${ssbPort}`);
  60. console.log(`- LAN broadcasting (UDP): ${localDiscovery ? 'enabled' : 'disabled'}`);
  61. console.log(`- Replication (hops): ${hops}`);
  62. console.log(`- Mode: ${isOnline ? 'online' : 'offline'}`);
  63. console.log("");
  64. console.log("=========================");
  65. console.log("Modules loaded: [", modules.length, "]");
  66. console.log("=========================");
  67. // Check for updates
  68. await checkForUpdate();
  69. console.log("=========================");
  70. }
  71. module.exports = {
  72. printMetadata,
  73. colors
  74. };