2017-01-29 15 views
1

Express provides the request IP address via req.ip (wo req die Anforderung), aber für IPv4-Adressen es IPv6 notation verwendet, zum Beispiel:IPv4 Anfrage IP IPv6 statt IPv4 macht Notation

::ffff:1.2.3.4 

Ich bin glücklich, dass es mit keinem IPv6 unterstützt Aufwendig, aber für UI-Zwecke möchte ich IPv4-Adressen in IPv4-Notation ("1.2.3.4") anzeigen, während natürlich immer noch IPv6-Adressen unterstützt werden.

Kann ich Express zur Verwendung der reinen IPv4-Notation für IPv4-Anforderungsadressen erhalten?

Antwort

2

Nun, ich fand einen Weg, es mit der ipaddr.js module zu tun, die ich über this answer gefunden habe. Mit diesem Modul kann überprüft werden, ob die IPv6-Adresse eine IPv4-Adresse ist, und ggf. konvertieren. Zum Beispiel:

var ip = require('ipaddr.js') 

function cleanupAddress (str) { 
    // if it's a valid ipv6 address, and if its a mapped ipv4 address, 
    // then clean it up. otherwise return the original string. 
    if (ip.IPv6.isValid(str)) { 
     var addr = ip.IPv6.parse(str); 
     if (addr.isIPv4MappedAddress()) 
      return addr.toIPv4Address().toString(); 
    } 
    return str 
} 

console.log(cleanupAddress('1.2.3.4')); 
console.log(cleanupAddress('::ffff:1.2.3.4')); 
console.log(cleanupAddress('::ffff:102:304')); 
console.log(cleanupAddress('0:0:0:0:0:ffff:1.2.3.4')); 
console.log(cleanupAddress('::1')); 
console.log(cleanupAddress('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); 

Ausgänge:

1.2.3.4 
1.2.3.4 
1.2.3.4 
1.2.3.4 
::1 
2001:0db8:85a3:0000:0000:8a2e:0370:7334 

Was ist das, was ich werde. Es macht mir nichts aus, eine weitere Abhängigkeit hinzuzufügen, also scheint das in Ordnung zu sein.

Angenommen, das Modul ist korrekt implementiert, sollte dies vollständige Unterstützung für IPv4- und IPv6-konforme Adressen jeglicher Form bieten.


Hier ist ein vollständigeres Test für Neugierige:

var ip = require('ipaddr.js') 

function test (str) { 
    console.log(str); 
    console.log(' IPv4.isValid:', ip.IPv4.isValid(str)); 
    console.log(' IPv6.isValid:', ip.IPv6.isValid(str)); 
    if (ip.IPv6.isValid(str)) { 
     var addr = ip.IPv6.parse(str); 
     console.log(' IPv6.parse.toString:', addr.toString()); 
     console.log(' IPv6.isIPv4MappedAddress:', addr.isIPv4MappedAddress()); 
     if (addr.isIPv4MappedAddress()) { 
      console.log(' IPv6.toIPv4Address.toString:', addr.toIPv4Address().toString()); 
     } 
    } 
} 

test('1.2.3.4') 
test('::ffff:1.2.3.4') 
test('0:0:0:0:0:ffff:1.2.3.4') 
test('::1') 
test('2001:0db8:85a3:0000:0000:8a2e:0370:7334') 

Welche Ausgänge:

1.2.3.4 
    IPv4.isValid: true 
    IPv6.isValid: false 
::ffff:1.2.3.4 
    IPv4.isValid: false 
    IPv6.isValid: true 
    IPv6.parse.toString: ::ffff:102:304 
    IPv6.isIPv4MappedAddress: true 
    IPv6.toIPv4Address.toString: 1.2.3.4 
0:0:0:0:0:ffff:1.2.3.4 
    IPv4.isValid: false 
    IPv6.isValid: true 
    IPv6.parse.toString: ::ffff:102:304 
    IPv6.isIPv4MappedAddress: true 
    IPv6.toIPv4Address.toString: 1.2.3.4 
::1 
    IPv4.isValid: false 
    IPv6.isValid: true 
    IPv6.parse.toString: ::1 
    IPv6.isIPv4MappedAddress: false 
2001:0db8:85a3:0000:0000:8a2e:0370:7334 
    IPv4.isValid: false 
    IPv6.isValid: true 
    IPv6.parse.toString: 2001:db8:85a3::8a2e:370:7334 
    IPv6.isIPv4MappedAddress: false