Skip to content

Commit

Permalink
fix: custom ip implementation don't work well in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Jul 7, 2021
1 parent 6791b66 commit b3b45db
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
14 changes: 11 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"intercept-stdout": "0.1.2",
"inversify": "5.1.1",
"inversify-inject-decorators": "3.1.0",
"ip": "^1.1.5",
"jimp": "0.16.1",
"lodash": "4.17.21",
"logrocket": "1.3.0",
Expand Down Expand Up @@ -95,6 +96,7 @@
"@types/fs-extra": "9.0.11",
"@types/i18next-fs-backend": "1.0.0",
"@types/intercept-stdout": "0.1.0",
"@types/ip": "^1.1.0",
"@types/lodash": "4.14.170",
"@types/node": "15.12.5",
"@types/node-fetch": "2.5.10",
Expand Down
32 changes: 3 additions & 29 deletions src/services/libs/url.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
import { networkInterfaces } from 'os';
import { address } from 'ip';
import { defaultServerIP } from '@/constants/urls';
import { logger } from './log';

const nets = networkInterfaces();
/**
* Contains all available ip address.
* ```js
* { en0: [ '192.168.50.112' ] }
* ```
* @docs https://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js
*/
export const ipAddresses: Record<string, string[]> = {};

for (const name in nets) {
for (const net of nets[name]!) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
if (net.family === 'IPv4' && !net.internal) {
if (ipAddresses[name] === undefined) {
ipAddresses[name] = [];
}
ipAddresses[name].push(net.address);
}
}
}

export function getAvailableIPAddress(): string | undefined {
return ipAddresses.en0?.[0] ?? ipAddresses.eth0?.[0];
}

/**
* get wiki address with local machine ip, so QR code will be correct, instead of get `0.0.0.0`
* https://github.com/Jermolene/TiddlyWiki5/issues/5669
* @param originalUrl might be `"http://0.0.0.0:5212/"`
*/
export function getLocalHostUrlWithActualIP(originalUrl: string): string {
const localHostUrlWithActualIP = originalUrl.replace(/(?:\d{1,3}\.){3}\d{1,3}/, getAvailableIPAddress() ?? defaultServerIP);
const localHostUrlWithActualIP = originalUrl.replace(/(?:\d{1,3}\.){3}\d{1,3}/, address('public') ?? defaultServerIP);
logger.debug(
`Current available address: ${JSON.stringify(ipAddresses, undefined, ' ')}\nand getAvailableIPAddress() returns ${getAvailableIPAddress() ?? 'undefined'}
`Current available address: address() returns ${address('public') ?? 'undefined'}
originalUrl: ${originalUrl} , localHostUrlWithActualIP ${localHostUrlWithActualIP}`,
);
return localHostUrlWithActualIP;
Expand Down

0 comments on commit b3b45db

Please sign in to comment.