check for localhost ip and ignore link-local v6, i think you need to specify an interface
| @@ -28,6 +28,7 @@ import multer from 'multer'; | |||
| 28 | import responseTime from 'response-time'; | 28 | import responseTime from 'response-time'; |
| 29 | import helmet from 'helmet'; | 29 | import helmet from 'helmet'; |
| 30 | import bodyParser from 'body-parser'; | 30 | import bodyParser from 'body-parser'; |
| 31 | import { getMatch, IPMatch, IPSubnetwork, IPRange, matches } from 'ip-matching'; | ||
| 31 | 32 | ||
| 32 | // net related library imports | 33 | // net related library imports |
| 33 | import fetch from 'node-fetch'; | 34 | import fetch from 'node-fetch'; |
| @@ -383,26 +384,35 @@ function getSessionCookieAge() { | |||
| 383 | 384 | ||
| 384 | async function getHasIP() { | 385 | async 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'); | ||
| 388 | 392 | ||
| 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 | } |
| 397 | 404 | ||
| 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 | } |
| 407 | 417 | ||
| 408 | app.use(cookieSession({ | 418 | app.use(cookieSession({ |
| @@ -930,11 +940,13 @@ async function startHTTPorHTTPS(useIPv6, useIPv4) { | |||
| 930 | async function startServer() { | 940 | async 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; |
| 934 | 944 | ||
| 935 | 945 | ||
| 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 | } |
| 948 | 960 | ||
| 949 | 961 | ||
| 962 | hasIPv4 = listen ? hasIPv4NonLocal : hasIPv4Local; | ||
| 950 | if (enableIPv4 === 'auto') { | 963 | if (enableIPv4 === 'auto') { |
| 951 | useIPv4 = hasIPv4; | 964 | useIPv4 = hasIPv4; |
| 952 | } | 965 | } |