1
0
mirror of https://github.com/MailHops/mailhops-node.git synced 2025-05-15 11:20:12 -07:00

Updated IPV6 regex

This commit is contained in:
Andrew Van Tassel 2018-06-24 10:45:53 -06:00
parent c2811b7afa
commit 86b12cd5e0
4 changed files with 36 additions and 18 deletions

View File

@ -52,15 +52,16 @@ module.exports = {
//parse the whole email header //parse the whole email header
getIPsFromHeader: function(header){ getIPsFromHeader: function(header){
var receivedHeaders = this.getReceivedHeaders(header) var receivedHeaders = this.getReceivedHeaders(header);
,ips = [] var ips = [];
,regexIp=/(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}$/ var regexIp = /(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}$/;
,regexAllIp = /(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}/g var regexAllIp = /(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}/g;
,regexIPV6 = /(::|(([a-fA-F0-9]{1,4}):){7}(([a-fA-F0-9]{1,4}))|(:(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){1,6}:)|((([a-fA-F0-9]{1,4}):)(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){2}(:([a-fA-F0-9]{1,4})){1,5})|((([a-fA-F0-9]{1,4}):){3}(:([a-fA-F0-9]{1,4})){1,4})|((([a-fA-F0-9]{1,4}):){4}(:([a-fA-F0-9]{1,4})){1,3})|((([a-fA-F0-9]{1,4}):){5}(:([a-fA-F0-9]{1,4})){1,2}))/; var regexIPV6 = /s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*/g;
_.each(receivedHeaders, function(line){ _.each(receivedHeaders, function(line){
//IPV6 check //IPV6 check
line = line.replace('[IPv6:','[');
if(line.match(regexIPV6)){ if(line.match(regexIPV6)){
ips.unshift( line.match(regexIPV6)[0] ); ips.unshift( line.match(regexIPV6)[0] );
return; return;
@ -108,10 +109,10 @@ module.exports = {
}, },
getIPsFromMailParser: function(parsedmail){ getIPsFromMailParser: function(parsedmail){
var ips = [] var ips = [];
,regexIp=/(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}$/ var regexIp = /(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}$/;
,regexAllIp = /(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}/g var regexAllIp = /(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}/g;
,regexIPV6 = /(::|(([a-fA-F0-9]{1,4}):){7}(([a-fA-F0-9]{1,4}))|(:(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){1,6}:)|((([a-fA-F0-9]{1,4}):)(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){2}(:([a-fA-F0-9]{1,4})){1,5})|((([a-fA-F0-9]{1,4}):){3}(:([a-fA-F0-9]{1,4})){1,4})|((([a-fA-F0-9]{1,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(%.+)?s*/g;
if(typeof parsedmail.headers.received == 'undefined') if(typeof parsedmail.headers.received == 'undefined')
return ips; return ips;
@ -119,6 +120,13 @@ module.exports = {
parsedmail.headers.received = [parsedmail.headers.received]; parsedmail.headers.received = [parsedmail.headers.received];
_.each(parsedmail.headers.received,function(line){ _.each(parsedmail.headers.received,function(line){
//IPV6 check
line = line.replace('[IPv6:','[');
if(line.match(regexIPV6)){
ips.unshift( line.match(regexIPV6)[0] );
return;
}
var received_ips = line.match(regexAllIp); var received_ips = line.match(regexAllIp);
if(!received_ips) if(!received_ips)

View File

@ -1,6 +1,6 @@
{ {
"name": "mailhops", "name": "mailhops",
"version": "2.1.1", "version": "2.1.2",
"description": "A nodejs module for interacting with the MailHops API.", "description": "A nodejs module for interacting with the MailHops API.",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
@ -26,7 +26,7 @@
"bugs": { "bugs": {
"url": "https://github.com/mailhops/mailhops-node/issues" "url": "https://github.com/mailhops/mailhops-node/issues"
}, },
"homepage": "https://github.com/mailhops/mailhops-node", "homepage": "https://mailhops.com",
"_npmUser": { "_npmUser": {
"name": "avantassel", "name": "avantassel",
"email": "andrew@andrewvantassel.com" "email": "andrew@andrewvantassel.com"

View File

@ -32,6 +32,14 @@ Received: from ip227-36.wireless.lu.se (ip227-36.wireless.lu.se
[130.235.227.36]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No [130.235.227.36]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No
client certificate requested) by muon.isy.liu.se (Postfix) with ESMTPSA id client certificate requested) by muon.isy.liu.se (Postfix) with ESMTPSA id
D4B3D802; Thu, 3 Sep 2014 16:38:38 -0700 (PDT) D4B3D802; Thu, 3 Sep 2014 16:38:38 -0700 (PDT)
Received: from mail-vk0-x231.google.com ([IPv6:2001:4860:4860::8888])
by smtpin.rzone.de (RZmta 43.10 REPLY)
with ESMTPS id S05391u5CBmerDX
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp384r1 with 384 ECDH bits, eq. 7680 bits RSA))
(Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK))
(Client hostname verification FAILED)
for <cron@mailhops.com>;
Tue, 12 Jun 2018 13:48:40 +0200 (CEST)
Content-Type: multipart/alternative; Content-Type: multipart/alternative;
boundary="===============2767014682564602329==" boundary="===============2767014682564602329=="
MIME-Version: 1.0 MIME-Version: 1.0

View File

@ -79,8 +79,8 @@ describe("main", function(){
var header = fs.readFileSync(__dirname+'/header-test.eml',{ encoding: 'utf8' }); var header = fs.readFileSync(__dirname+'/header-test.eml',{ encoding: 'utf8' });
var ips = mailhops.getIPsFromHeader(header); var ips = mailhops.getIPsFromHeader(header);
it('should return an array of 9 Received IPs', function(done){ it('should return an array of 10 Received IPs', function(done){
assert.equal(ips.length,9); assert.equal(ips.length,10);
done(); done();
}); });
@ -90,8 +90,9 @@ describe("main", function(){
done(); done();
}); });
it('should find 9 Received IPs', function(done){ it('should find 11 Received IPs', function(done){
assert.deepEqual(ips,['130.235.227.36', assert.deepEqual(ips,['2001:4860:4860::8888',
'130.235.227.36',
'130.236.48.25', '130.236.48.25',
'130.235.56.196', '130.235.56.196',
'130.235.56.196', '130.235.56.196',
@ -99,7 +100,8 @@ describe("main", function(){
'127.0.0.1', '127.0.0.1',
'54.157.138.253', '54.157.138.253',
'198.21.5.108', '198.21.5.108',
'2607:fb90:50f:5547:0:46:e46a:bd01']); '2607:fb90:50f:5547:0:46:e46a:bd01'
]);
done(); done();
}); });
@ -108,10 +110,10 @@ describe("main", function(){
done(); done();
}); });
it('should return a 200 response and route of 10 hops', function(done){ it('should return a 200 response and route of 11 hops', function(done){
mailhops.lookup(mailhops.getIPsFromHeader(header), function(err, res, body){ mailhops.lookup(mailhops.getIPsFromHeader(header), function(err, res, body){
assert.equal(res.statusCode,200); assert.equal(res.statusCode,200);
assert.equal(body.response['route'].length,10); assert.equal(body.response['route'].length,11);
done(); done();
}); });
}); });