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