diff --git a/lib/index.ts b/lib/index.ts index 896a19cb6..edef861a1 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,12 +1,18 @@ import { serve } from '@hono/node-server'; import logger from '@/utils/logger'; +import { getLocalhostAddress } from '@/utils/common-utils'; import { config } from '@/config'; import app from '@/app'; const port = config.connect.port; +const hostIPList = getLocalhostAddress(); logger.info(`🎉 RSSHub is running on port ${port}! Cheers!`); logger.info('💖 Can you help keep this open source project alive? Please sponsor 👉 https://docs.rsshub.app/support'); +logger.info(`🔗 Local: 👉 http://localhost:${port}`); +for (const ip of hostIPList) { + logger.info(`🔗 Network: 👉 http://${ip}:${port}`); +} const server = serve({ fetch: app.fetch, diff --git a/lib/utils/common-utils.test.ts b/lib/utils/common-utils.test.ts index c85ae85d5..a13f9a261 100644 --- a/lib/utils/common-utils.test.ts +++ b/lib/utils/common-utils.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { toTitleCase, collapseWhitespace, convertDateToISO8601 } from '@/utils/common-utils'; +import { toTitleCase, collapseWhitespace, convertDateToISO8601, getLocalhostAddress } from '@/utils/common-utils'; describe('common-utils', () => { it('toTitleCase', () => { @@ -34,4 +34,8 @@ describe('common-utils', () => { expect(collapseWhitespace('a string already collapsed')).toBe('a string already collapsed'); expect(collapseWhitespace(' \n a lot of whitespaces and \n\n\n\n linebreaks \n\n ')).toBe('a lot of whitespaces and linebreaks'); }); + + it('getLocalhostAddress', () => { + expect(getLocalhostAddress()).toBeInstanceOf(Array); + }); }); diff --git a/lib/utils/common-utils.ts b/lib/utils/common-utils.ts index 29f792c8e..6ad4120d0 100644 --- a/lib/utils/common-utils.ts +++ b/lib/utils/common-utils.ts @@ -1,5 +1,6 @@ import { parseDate } from '@/utils/parse-date'; import title from 'title'; +import os from 'os'; // convert a string into title case const toTitleCase = (str: string) => title(str); @@ -31,4 +32,14 @@ const getSubPath = (ctx) => { return subPath; }; -export { toTitleCase, collapseWhitespace, convertDateToISO8601, getSubPath }; +const getLocalhostAddress = () => { + const interfaces = os.networkInterfaces(); + const address = Object.keys(interfaces) + .flatMap((name) => interfaces[name] ?? []) + .filter((iface) => iface?.family === 'IPv4' && !iface.internal) + .map((iface) => iface?.address) + .filter(Boolean); + return address; +}; + +export { toTitleCase, collapseWhitespace, convertDateToISO8601, getSubPath, getLocalhostAddress };