Skip to content

Commit

Permalink
get rid of 'node-ip' package
Browse files Browse the repository at this point in the history
  • Loading branch information
TheodoreKrypton committed Jun 30, 2024
1 parent cc1a39c commit d759415
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"dotenv": "^16.3.1",
"express": "^4.19.2",
"input": "^1.0.1",
"ip": "^1.1.9",
"js-yaml": "^4.1.0",
"jsonwebtoken": "^9.0.2",
"telegraf": "^4.15.0",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import express from 'express';
import fs from 'fs';

import ip from 'ip';
import { exit } from 'process';
import { v2 as webdav } from 'webdav-server';
import yargs from 'yargs';
Expand All @@ -15,6 +14,7 @@ import { config, createConfig, loadConfig } from './config';
import { BusinessError } from './errors/base';
import { managerServer, startBot } from './server/manager';
import { webdavServer } from './server/webdav';
import { getIPAddress } from './utils/ip-address';
import { Logger } from './utils/logger';

const { argv }: any = yargs(hideBin(process.argv))
Expand Down Expand Up @@ -62,7 +62,7 @@ const { argv }: any = yargs(hideBin(process.argv))
masterApp.listen(port, host);

if (host === '0.0.0.0' || host === '::') {
host = ip.address();
host = getIPAddress('IPv4');
}
Logger.info(`${name} is running on ${host}:${port}${path}`);
};
Expand Down
23 changes: 23 additions & 0 deletions src/utils/ip-address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os from 'os';

type IpFamily = 'IPv4' | 'IPv6';

const loopback = (family?: IpFamily): string => {
return family === 'IPv4' ? '127.0.0.1' : 'fe80::1';
};

export const getIPAddress = (family: IpFamily): string => {
const interfaces = os.networkInterfaces();

const all = Object.keys(interfaces)
.map((nic) => {
const addresses = interfaces[nic].filter(
(details) => details.family === family && !details.internal,
);

return addresses.length ? addresses[0].address : undefined;
})
.filter(Boolean);
console.log(all);
return all.length ? all[0] : loopback(family);
};
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2120,11 +2120,6 @@ ip-address@^9.0.5:
jsbn "1.1.0"
sprintf-js "^1.1.3"

ip@^1.1.9:
version "1.1.9"
resolved "https://registry.yarnpkg.com/ip/-/ip-1.1.9.tgz#8dfbcc99a754d07f425310b86a99546b1151e396"
integrity sha512-cyRxvOEpNHNtchU3Ln9KC/auJgup87llfQpQ+t5ghoC/UhL16SWzbueiCsdTnWmqAWl7LadfuwhlqmtOaqMHdQ==

[email protected]:
version "1.9.1"
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.9.1.tgz#bff38543eeb8984825079ff3a2a8e6cbd46781b3"
Expand Down

0 comments on commit d759415

Please sign in to comment.