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 -6Ignore whitespace
server.js+19 -6
@@ -28,6 +28,7 @@ import multer from 'multer';
28import responseTime from 'response-time';28import responseTime from 'response-time';
29import helmet from 'helmet';29import helmet from 'helmet';
30import bodyParser from 'body-parser';30import bodyParser from 'body-parser';
31import { getMatch, IPMatch, IPSubnetwork, IPRange, matches } from 'ip-matching';
3132
32// net related library imports33// net related library imports
33import fetch from 'node-fetch';34import fetch from 'node-fetch';
@@ -383,26 +384,35 @@ function getSessionCookieAge() {
383384
384async function getHasIP() {385async function getHasIP() {
385 let hasIPv6 = false;386 let hasIPv6 = false;
387 let hasIPv6Local = false;
386 let hasIPv4 = false;388 let hasIPv4 = false;
389 let hasIPv4Local = false;
387 const interfaces = os.networkInterfaces();390 const interfaces = os.networkInterfaces();
391 const linkLocalV6 = getMatch('fe80::/10');
388392
389 for (const iface of Object.values(interfaces)) {393 for (const iface of Object.values(interfaces)) {
390 if (iface === undefined) {394 if (iface === undefined) {
391 continue;395 continue;
392 }396 }
393 for (const info of iface) {397 for (const info of iface) {
394 if (info.family === 'IPv6') {398 if (info.family === 'IPv6' && !linkLocalV6.matches(info.address)) {
395 hasIPv6 = true;399 hasIPv6 = true;
400 if (info.internal === true) {
401 hasIPv6Local = true;
402 }
396 }403 }
397404
398 if (info.family === 'IPv4') {405 if (info.family === 'IPv4') {
399 hasIPv4 = true;406 hasIPv4 = true;
407 if (info.internal === true) {
408 hasIPv4Local = true;
409 }
400 }410 }
401 if (hasIPv6 && hasIPv4) break;411 if (hasIPv6 && hasIPv4 && hasIPv6Local && hasIPv4Local) break;
402 }412 }
403 if (hasIPv6 && hasIPv4) break;413 if (hasIPv6 && hasIPv4 && hasIPv6Local && hasIPv4Local) break;
404 }414 }
405 return [hasIPv6, hasIPv4];415 return [hasIPv6, hasIPv4, hasIPv6Local, hasIPv4Local];
406}416}
407417
408app.use(cookieSession({418app.use(cookieSession({
@@ -930,11 +940,13 @@ async function startHTTPorHTTPS(useIPv6, useIPv4) {
930async function startServer() {940async function startServer() {
931 let useIPv6 = (enableIPv6 === true);941 let useIPv6 = (enableIPv6 === true);
932 let useIPv4 = (enableIPv4 === true);942 let useIPv4 = (enableIPv4 === true);
933 let hasIPv6, hasIPv4;943 let hasIPv6, hasIPv4, hasIPv6Local, hasIPv4Local, hasIPv6NonLocal, hasIPv4NonLocal;
934944
935945
936 if (enableIPv6 === 'auto' || enableIPv4 === 'auto') {946 if (enableIPv6 === 'auto' || enableIPv4 === 'auto') {
937 [hasIPv6, hasIPv4] = await getHasIP();947 [hasIPv6NonLocal, hasIPv4NonLocal, hasIPv6Local, hasIPv4Local] = await getHasIP();
948
949 hasIPv6 = listen ? hasIPv6NonLocal : hasIPv6Local;
938 if (enableIPv6 === 'auto') {950 if (enableIPv6 === 'auto') {
939 useIPv6 = hasIPv6;951 useIPv6 = hasIPv6;
940 }952 }
@@ -947,6 +959,7 @@ async function startServer() {
947 }959 }
948960
949961
962 hasIPv4 = listen ? hasIPv4NonLocal : hasIPv4Local;
950 if (enableIPv4 === 'auto') {963 if (enableIPv4 === 'auto') {
951 useIPv4 = hasIPv4;964 useIPv4 = hasIPv4;
952 }965 }