check for localhost ip and ignore link-local v6, i think you need to specify an interface

1a1ab1d18a5f0a586616326fff29f1de091d1317

BPplays <andymalbp@gmail.com>

1 files changed, +19 -6Showing whitespace changes
server.js+19 -6
@@ -28,6 +28,7 @@ import multer from 'multer';
2828import responseTime from 'response-time';
2929import helmet from 'helmet';
3030import bodyParser from 'body-parser';
31+import { getMatch, IPMatch, IPSubnetwork, IPRange, matches } from 'ip-matching';
3132
3233// net related library imports
3334import fetch from 'node-fetch';
@@ -383,26 +384,35 @@ function getSessionCookieAge() {
383384
384385async function getHasIP() {
385386 let hasIPv6 = false;
387+ let hasIPv6Local = false;
386388 let hasIPv4 = false;
389+ let hasIPv4Local = false;
387390 const interfaces = os.networkInterfaces();
391+ const linkLocalV6 = getMatch('fe80::/10');
388392
389393 for (const iface of Object.values(interfaces)) {
390394 if (iface === undefined) {
391395 continue;
392396 }
393397 for (const info of iface) {
394398 if (info.family === 'IPv6' && !linkLocalV6.matches(info.address)) {
395399 hasIPv6 = true;
400+ if (info.internal === true) {
401+ hasIPv6Local = true;
402+ }
396403 }
397404
398405 if (info.family === 'IPv4') {
399406 hasIPv4 = true;
407+ if (info.internal === true) {
408+ hasIPv4Local = true;
409+ }
400410 }
401411 if (hasIPv6 && hasIPv4 && hasIPv6Local && hasIPv4Local) break;
402412 }
403413 if (hasIPv6 && hasIPv4 && hasIPv6Local && hasIPv4Local) break;
404414 }
405415 return [hasIPv6, hasIPv4, hasIPv6Local, hasIPv4Local];
406416}
407417
408418app.use(cookieSession({
@@ -930,11 +940,13 @@ async function startHTTPorHTTPS(useIPv6, useIPv4) {
930940async function startServer() {
931941 let useIPv6 = (enableIPv6 === true);
932942 let useIPv4 = (enableIPv4 === true);
933943 let hasIPv6, hasIPv4, hasIPv6Local, hasIPv4Local, hasIPv6NonLocal, hasIPv4NonLocal;
934944
935945
936946 if (enableIPv6 === 'auto' || enableIPv4 === 'auto') {
937947 [hasIPv6hasIPv6NonLocal, hasIPv4hasIPv4NonLocal, hasIPv6Local, hasIPv4Local] = await getHasIP();
948+
949+ hasIPv6 = listen ? hasIPv6NonLocal : hasIPv6Local;
938950 if (enableIPv6 === 'auto') {
939951 useIPv6 = hasIPv6;
940952 }
@@ -947,6 +959,7 @@ async function startServer() {
947959 }
948960
949961
962+ hasIPv4 = listen ? hasIPv4NonLocal : hasIPv4Local;
950963 if (enableIPv4 === 'auto') {
951964 useIPv4 = hasIPv4;
952965 }