From 31b2bf7b51efc25a61cb720a3d83f667b6e0e579 Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Sat, 19 Jul 2014 18:18:47 -0400 Subject: [PATCH 01/14] initial cut at ipv6 support. refs #29 --- anon.coffee | 15 +++++++++------ package.json | 6 +++--- test.coffee | 11 ++++++++++- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/anon.coffee b/anon.coffee index 10b185c..a30dd08 100755 --- a/anon.coffee +++ b/anon.coffee @@ -1,7 +1,7 @@ #!/usr/bin/env coffee +ipv6 = require 'ipv6' Twit = require 'twit' -{Netmask} = require 'netmask' minimist = require 'minimist' {WikiChanges} = require 'wikichanges' Mustache = require 'mustache' @@ -11,10 +11,11 @@ argv = minimist process.argv.slice(2), default: config: './config.json' ipToInt = (ip) -> - octets = (parseInt(s) for s in ip.split('.')) - result = 0 - result += n * Math.pow(255, i) for n, i in octets.reverse() - result + if ':' in ip + a = new ipv6.v6.Address(ip) + else + a = new ipv6.v4.Address(ip) + return a.bigInteger().intValue() compareIps = (ip1, ip2) -> q1 = ipToInt(ip1) @@ -30,7 +31,9 @@ isIpInRange = (ip, block) -> if Array.isArray block compareIps(ip, block[0]) >= 0 and compareIps(ip, block[1]) <= 0 else - new Netmask(block).contains ip + a = new ipv6.v4.Address(ip) + b = new ipv6.v4.Address(block) + a.isInSubnet(b) isIpInAnyRange = (ip, blocks) -> blocks.filter((block) -> isIpInRange(ip, block)).length > 0 diff --git a/package.json b/package.json index e87002c..e8e1e8b 100644 --- a/package.json +++ b/package.json @@ -6,11 +6,11 @@ "version": "0.0.3", "dependencies": { "twit": "latest", - "wikichanges": ">=0.2.3", + "wikichanges": ">=0.2.4", "coffee-script": "latest", "minimist": "latest", - "netmask": "latest", - "mustache": "latest" + "mustache": "latest", + "ipv6": "latest" }, "devDependencies": { "mocha": "latest", diff --git a/test.coffee b/test.coffee index da6df8c..a0a8d2b 100644 --- a/test.coffee +++ b/test.coffee @@ -8,7 +8,7 @@ isIpInAnyRange = anon.isIpInAnyRange describe 'anon', -> - describe "compareIps", -> + describe "compareIps ipv4", -> it 'equal', -> assert.equal 0, compareIps '1.1.1.1', '1.1.1.1' @@ -17,6 +17,15 @@ describe 'anon', -> it 'less than', -> assert.equal -1, compareIps '1.1.1.1', '1.1.1.2' + describe "compareIps ipv6", -> + + it 'equal', -> + assert.equal 0, compareIps '2601:8:b380:3f3:540b:fdbf:bc5:a6bf', '2601:8:b380:3f3:540b:fdbf:bc5:a6bf' + it 'greater than', -> + assert.equal 0, compareIps '2601:8:b380:3f3:540b:fdbf:bc5:a6bf', '2600:8:b380:3f3:540b:fdbf:bc5:a6bf' + it 'less than', -> + assert.equal 0, compareIps '2600:8:b380:3f3:540b:fdbf:bc5:a6bf', '2601:8:b380:3f3:540b:fdbf:bc5:a6bf' + describe 'isIpInRange', -> it 'ip in range', -> From 70082cadc238ce7a376278c32ca7bf6ec94d989d Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Sat, 19 Jul 2014 22:05:32 -0400 Subject: [PATCH 02/14] needed to use compareTo --- anon.coffee | 20 +++++++++++--------- test.coffee | 13 ++++++++++--- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/anon.coffee b/anon.coffee index a30dd08..018d59e 100755 --- a/anon.coffee +++ b/anon.coffee @@ -12,20 +12,21 @@ argv = minimist process.argv.slice(2), default: ipToInt = (ip) -> if ':' in ip - a = new ipv6.v6.Address(ip) + i = new ipv6.v6.Address(ip) else - a = new ipv6.v4.Address(ip) - return a.bigInteger().intValue() + i = new ipv6.v4.Address(ip) + i.bigInteger() compareIps = (ip1, ip2) -> - q1 = ipToInt(ip1) - q2 = ipToInt(ip2) - if q1 == q2 + i1 = ipToInt(ip1) + i2 = ipToInt(ip2) + r = i1.compareTo(i2) + if r == 0 0 - else if q1 < q2 - -1 - else + else if r > 0 1 + else + -1 isIpInRange = (ip, block) -> if Array.isArray block @@ -81,6 +82,7 @@ inspect = (account, edit) -> for name, ranges of account.ranges if isIpInAnyRange edit.user, ranges status = getStatus edit, name, account.template + console.log edit.user tweet account, status main = -> diff --git a/test.coffee b/test.coffee index a0a8d2b..b7a6a4a 100644 --- a/test.coffee +++ b/test.coffee @@ -12,8 +12,10 @@ describe 'anon', -> it 'equal', -> assert.equal 0, compareIps '1.1.1.1', '1.1.1.1' + it 'greater than', -> assert.equal 1, compareIps '1.1.1.2', '1.1.1.1' + it 'less than', -> assert.equal -1, compareIps '1.1.1.1', '1.1.1.2' @@ -21,10 +23,12 @@ describe 'anon', -> it 'equal', -> assert.equal 0, compareIps '2601:8:b380:3f3:540b:fdbf:bc5:a6bf', '2601:8:b380:3f3:540b:fdbf:bc5:a6bf' + it 'greater than', -> - assert.equal 0, compareIps '2601:8:b380:3f3:540b:fdbf:bc5:a6bf', '2600:8:b380:3f3:540b:fdbf:bc5:a6bf' + assert.equal 1, compareIps '2600:8:b380:3f3:540b:fdbf:bc5:a6bf', '2600:8:b380:3f3:540b:fdbf:bc5:a6be' + it 'less than', -> - assert.equal 0, compareIps '2600:8:b380:3f3:540b:fdbf:bc5:a6bf', '2601:8:b380:3f3:540b:fdbf:bc5:a6bf' + assert.equal -1, compareIps '2600:8:b380:3f3:540b:fdbf:bc5:a6be', '2601:8:b380:3f3:540b:fdbf:bc5:a6bf' describe 'isIpInRange', -> @@ -43,6 +47,9 @@ describe 'anon', -> it 'ip is not in cidr range', -> assert.isFalse isIpInRange '123.123.123.123', '123.123.123.122/32' + it 'ipv6 in range', -> + assert.isTrue isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:0000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + describe 'isIpInAnyRange', -> r1 = ['1.1.1.0', '1.1.1.5'] @@ -57,7 +64,7 @@ describe 'anon', -> it 'ip not in any ranges', -> assert.isFalse isIpInAnyRange '1.1.1.6', [r1, r2] - describe 'IP Range Error (#12)', -> + describe 'ip range error (#12)', -> it 'false positive not in ranges', -> assert.isFalse isIpInAnyRange '199.19.250.20', [["199.19.16.0", "199.19.27.255"], ["4.42.247.224", "4.42.247.255"]] From 3b2d5de71d8745022de7dfa061e3ef6e259204f2 Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Sat, 19 Jul 2014 23:17:51 -0400 Subject: [PATCH 03/14] simplified a bit --- anon.coffee | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/anon.coffee b/anon.coffee index 018d59e..6480acb 100755 --- a/anon.coffee +++ b/anon.coffee @@ -18,9 +18,7 @@ ipToInt = (ip) -> i.bigInteger() compareIps = (ip1, ip2) -> - i1 = ipToInt(ip1) - i2 = ipToInt(ip2) - r = i1.compareTo(i2) + r = ipToInt(ip1).compareTo(ipToInt(ip2)) if r == 0 0 else if r > 0 From c8f55afcfbe8584163b999e5fd3bf68aa8708920 Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Sat, 19 Jul 2014 23:30:07 -0400 Subject: [PATCH 04/14] a few more ipv6 changes --- test.coffee | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test.coffee b/test.coffee index b7a6a4a..a10c331 100644 --- a/test.coffee +++ b/test.coffee @@ -50,6 +50,15 @@ describe 'anon', -> it 'ipv6 in range', -> assert.isTrue isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:0000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + it 'ipv6 not in range', -> + assert.isFalse isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:1000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + + it 'ipv4 in ipv6 range', -> + assert.isTrue isIpInRange '127.0.0.1', ['0000:0000:0000:0000:0000:0000:0000:0000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + + it 'ipv4 not in ipv6 range', -> + assert.isFalse isIpInRange '127.0.0.1', ['0000:0000:F000:0000:0000:0000:0000:0000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + describe 'isIpInAnyRange', -> r1 = ['1.1.1.0', '1.1.1.5'] @@ -87,5 +96,3 @@ describe 'anon', -> template = "{{page}} edited by {{name}} {{&url}}" result = getStatus edit, name, template assert.isTrue result.length <= 140 - - From c99b4caa15152038de301c219174114b90bb185e Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Sat, 19 Jul 2014 23:32:34 -0400 Subject: [PATCH 05/14] cosmetic change --- anon.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/anon.coffee b/anon.coffee index 6480acb..d9c44ef 100755 --- a/anon.coffee +++ b/anon.coffee @@ -3,8 +3,8 @@ ipv6 = require 'ipv6' Twit = require 'twit' minimist = require 'minimist' -{WikiChanges} = require 'wikichanges' Mustache = require 'mustache' +{WikiChanges} = require 'wikichanges' argv = minimist process.argv.slice(2), default: verbose: false From 075a07b7c072c62a0e817ec3a8f367b77c29e336 Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Mon, 21 Jul 2014 06:52:13 -0400 Subject: [PATCH 06/14] needed to create appropriate type of address. refs #29 --- anon.coffee | 7 +++++-- test.coffee | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/anon.coffee b/anon.coffee index d9c44ef..de9eba8 100755 --- a/anon.coffee +++ b/anon.coffee @@ -10,11 +10,14 @@ argv = minimist process.argv.slice(2), default: verbose: false config: './config.json' -ipToInt = (ip) -> +address = (ip) -> if ':' in ip i = new ipv6.v6.Address(ip) else i = new ipv6.v4.Address(ip) + +ipToInt = (ip) -> + i = address(ip) i.bigInteger() compareIps = (ip1, ip2) -> @@ -30,7 +33,7 @@ isIpInRange = (ip, block) -> if Array.isArray block compareIps(ip, block[0]) >= 0 and compareIps(ip, block[1]) <= 0 else - a = new ipv6.v4.Address(ip) + a = address(ip) b = new ipv6.v4.Address(block) a.isInSubnet(b) diff --git a/test.coffee b/test.coffee index 8591c44..f72e837 100644 --- a/test.coffee +++ b/test.coffee @@ -59,6 +59,13 @@ describe 'anon', -> it 'ipv4 not in ipv6 range', -> assert.isFalse isIpInRange '127.0.0.1', ['0000:0000:F000:0000:0000:0000:0000:0000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + it 'ipv6 in ipv4 cidr', -> + assert.isTrue isIpInRange '0:0:0:0:0:ffff:8e33:1', '142.51.0.0/16' + + it 'ipv6 not in ipv4 cidr', -> + assert.isFalse isIpInRange '2A02:908:DF50:2380:74c0:e1e1:7039:2281', '142.51.0.0/16' + + describe 'isIpInAnyRange', -> r1 = ['1.1.1.0', '1.1.1.5'] @@ -73,12 +80,6 @@ describe 'anon', -> it 'ip not in any ranges', -> assert.isFalse isIpInAnyRange '1.1.1.6', [r1, r2] - describe 'ip range error (#12)', -> - - it 'false positive not in ranges', -> - assert.isFalse isIpInAnyRange '199.19.250.20', [["199.19.16.0", "199.19.27.255"], ["4.42.247.224", "4.42.247.255"]] - assert.isFalse isIpInAnyRange '39.255.255.148', [["40.0.0.0", "40.127.255.255"], ["40.144.0.0", "40.255.255.255"]] - describe 'getStatus', -> it 'works', -> @@ -97,3 +98,9 @@ describe 'anon', -> template = "{{page}} edited by {{name}} {{&url}}" result = getStatus edit, name, template assert.isTrue result.length <= 140 + + describe 'ip range error (#12)', -> + + it 'false positive not in ranges', -> + assert.isFalse isIpInAnyRange '199.19.250.20', [["199.19.16.0", "199.19.27.255"], ["4.42.247.224", "4.42.247.255"]] + assert.isFalse isIpInAnyRange '39.255.255.148', [["40.0.0.0", "40.127.255.255"], ["40.144.0.0", "40.255.255.255"]] From b64cd1b18ffbaf2f01b5752f247b2c49f488eea8 Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Wed, 23 Jul 2014 16:38:30 +0000 Subject: [PATCH 07/14] added throttle config option, which prevents tweeting repeated edits of same page by the same user. fixes #38 --- anon.coffee | 19 ++++++++++++++----- config.json.template | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/anon.coffee b/anon.coffee index de9eba8..44db651 100755 --- a/anon.coffee +++ b/anon.coffee @@ -64,9 +64,19 @@ getStatus = (edit, name, template) -> url: edit.url page: page -tweet = (account, status) -> +lastChange = {} +isRepeat = (edit) -> + k = "#{edit.wikipedia}" + v = "#{edit.page}:#{edit.user}" + r = lastChange[k] == v + if r + console.log "found repeat:", k, v + lastChange[k] = v + return r + +tweet = (account, status, edit) -> console.log status - unless argv.noop + unless argv.noop or (account.throttle and isRepeat(edit)) twitter = new Twit account twitter.post 'statuses/update', status: status, (err) -> console.log err if err @@ -78,13 +88,12 @@ inspect = (account, edit) -> if account.whitelist and account.whitelist[edit.wikipedia] \ and account.whitelist[edit.wikipedia][edit.page] status = getStatus edit, edit.user, account.template - tweet account, status + tweet account, status, edit else if account.ranges and edit.anonymous for name, ranges of account.ranges if isIpInAnyRange edit.user, ranges status = getStatus edit, name, account.template - console.log edit.user - tweet account, status + tweet account, status, edit main = -> config = getConfig argv.config diff --git a/config.json.template b/config.json.template index f38cdff..76e5403 100644 --- a/config.json.template +++ b/config.json.template @@ -6,7 +6,7 @@ "consumer_secret": "", "access_token": "", "access_token_secret": "", - "template": "{{page}} Wikipedia article edited anonymously by {{name}} {{&url}}", + "template": "{{page}} Wikipedia article edited anonymously from {{name}} {{&url}}", "ranges": { "US House of Representatives": [ "143.231.0.0/16", From e442523491491efbfabd983290285fc9676eaf1b Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Wed, 23 Jul 2014 16:52:00 +0000 Subject: [PATCH 08/14] no need to log --- anon.coffee | 2 -- 1 file changed, 2 deletions(-) diff --git a/anon.coffee b/anon.coffee index 44db651..82ec627 100755 --- a/anon.coffee +++ b/anon.coffee @@ -69,8 +69,6 @@ isRepeat = (edit) -> k = "#{edit.wikipedia}" v = "#{edit.page}:#{edit.user}" r = lastChange[k] == v - if r - console.log "found repeat:", k, v lastChange[k] = v return r From b824cc1f67241e1da31abc435f59254e8cd8b207 Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Mon, 28 Jul 2014 04:27:54 -0400 Subject: [PATCH 09/14] ipv6 in ipv4 CIDR working now. refs #29 --- anon.coffee | 4 +++- test.coffee | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/anon.coffee b/anon.coffee index 8e9d966..ff1167a 100755 --- a/anon.coffee +++ b/anon.coffee @@ -34,7 +34,9 @@ isIpInRange = (ip, block) -> compareIps(ip, block[0]) >= 0 and compareIps(ip, block[1]) <= 0 else a = address(ip) - b = new ipv6.v4.Address(block) + b = address(block) + if not a.v4 and b.v4 + b = new ipv6.v6.Address('::ffff:' + block) a.isInSubnet(b) isIpInAnyRange = (ip, blocks) -> diff --git a/test.coffee b/test.coffee index f72e837..8bfb027 100644 --- a/test.coffee +++ b/test.coffee @@ -48,7 +48,7 @@ describe 'anon', -> assert.isFalse isIpInRange '123.123.123.123', '123.123.123.122/32' it 'ipv6 in range', -> - assert.isTrue isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:0000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + assert.isTrue isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:0000', '0000:0000:0000:0000:0000:0000:0000:0002'] it 'ipv6 not in range', -> assert.isFalse isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:1000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] @@ -59,13 +59,15 @@ describe 'anon', -> it 'ipv4 not in ipv6 range', -> assert.isFalse isIpInRange '127.0.0.1', ['0000:0000:F000:0000:0000:0000:0000:0000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + it 'ipv6 in ipv6 cidr', -> + assert.isTrue isIpInRange '0000:0000:0000:0000:0000:0000:1000:0005', '0000:0000:0000:0000:0000:0000:1000:0000/112' + it 'ipv6 in ipv4 cidr', -> - assert.isTrue isIpInRange '0:0:0:0:0:ffff:8e33:1', '142.51.0.0/16' + assert.isTrue isIpInRange '0:0:0:0:0:0:8e33:1', '142.51.0.0/16' it 'ipv6 not in ipv4 cidr', -> assert.isFalse isIpInRange '2A02:908:DF50:2380:74c0:e1e1:7039:2281', '142.51.0.0/16' - describe 'isIpInAnyRange', -> r1 = ['1.1.1.0', '1.1.1.5'] From f723f1c58ecb0e6afecc054a627990ca4f7ef428 Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Mon, 28 Jul 2014 04:57:26 -0400 Subject: [PATCH 10/14] moved test around --- test.coffee | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/test.coffee b/test.coffee index 8bfb027..19f9f07 100644 --- a/test.coffee +++ b/test.coffee @@ -45,7 +45,7 @@ describe 'anon', -> assert.isTrue isIpInRange '123.123.123.123', '123.123.0.0/16' it 'ip is not in cidr range', -> - assert.isFalse isIpInRange '123.123.123.123', '123.123.123.122/32' + assert.isFalse isIpInRange '123.123.124.1', '123.123.123.0/24' it 'ipv6 in range', -> assert.isTrue isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:0000', '0000:0000:0000:0000:0000:0000:0000:0002'] @@ -81,7 +81,11 @@ describe 'anon', -> it 'ip not in any ranges', -> assert.isFalse isIpInAnyRange '1.1.1.6', [r1, r2] - + + it 'false positive not in ranges #12', -> + assert.isFalse isIpInAnyRange '199.19.250.20', [["199.19.16.0", "199.19.27.255"], ["4.42.247.224", "4.42.247.255"]] + assert.isFalse isIpInAnyRange '39.255.255.148', [["40.0.0.0", "40.127.255.255"], ["40.144.0.0", "40.255.255.255"]] + describe 'getStatus', -> it 'works', -> @@ -100,9 +104,3 @@ describe 'anon', -> template = "{{page}} edited by {{name}} {{&url}}" result = getStatus edit, name, template assert.isTrue result.length <= 140 - - describe 'ip range error (#12)', -> - - it 'false positive not in ranges', -> - assert.isFalse isIpInAnyRange '199.19.250.20', [["199.19.16.0", "199.19.27.255"], ["4.42.247.224", "4.42.247.255"]] - assert.isFalse isIpInAnyRange '39.255.255.148', [["40.0.0.0", "40.127.255.255"], ["40.144.0.0", "40.255.255.255"]] From fdde6e1fab72ebb0d548f7734202d4a5ecddb4e6 Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Mon, 28 Jul 2014 06:07:06 -0400 Subject: [PATCH 11/14] need to convert ipv4 mask to ipv6 mask --- anon.coffee | 4 +++- test.coffee | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/anon.coffee b/anon.coffee index 6da189b..30cc7bc 100755 --- a/anon.coffee +++ b/anon.coffee @@ -36,7 +36,9 @@ isIpInRange = (ip, block) -> a = address(ip) b = address(block) if not a.v4 and b.v4 - b = new ipv6.v6.Address('::ffff:' + block) + mask = 96 + b.subnetMask + block = '::' + b.toV6Group() + "/" + mask + b = new ipv6.v6.Address(block) a.isInSubnet(b) isIpInAnyRange = (ip, blocks) -> diff --git a/test.coffee b/test.coffee index 19f9f07..d5c5182 100644 --- a/test.coffee +++ b/test.coffee @@ -30,7 +30,7 @@ describe 'anon', -> it 'less than', -> assert.equal -1, compareIps '2600:8:b380:3f3:540b:fdbf:bc5:a6be', '2601:8:b380:3f3:540b:fdbf:bc5:a6bf' - describe 'isIpInRange', -> + describe 'isIpInRange ipv4', -> it 'ip in range', -> assert.isTrue isIpInRange '123.123.123.123', ['123.123.123.0', '123.123.123.255'] @@ -47,6 +47,8 @@ describe 'anon', -> it 'ip is not in cidr range', -> assert.isFalse isIpInRange '123.123.124.1', '123.123.123.0/24' + describe 'isIpInRange ipv6', -> + it 'ipv6 in range', -> assert.isTrue isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:0000', '0000:0000:0000:0000:0000:0000:0000:0002'] @@ -66,7 +68,7 @@ describe 'anon', -> assert.isTrue isIpInRange '0:0:0:0:0:0:8e33:1', '142.51.0.0/16' it 'ipv6 not in ipv4 cidr', -> - assert.isFalse isIpInRange '2A02:908:DF50:2380:74c0:e1e1:7039:2281', '142.51.0.0/16' + assert.isFalse isIpInRange '0:0:0:0:0:0:8e34:1', '142.51.0.0/16' describe 'isIpInAnyRange', -> From 19be2b75e8456421b4a8a66af4ee4e72416b806f Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Mon, 28 Jul 2014 06:15:30 -0400 Subject: [PATCH 12/14] doc --- anon.coffee | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/anon.coffee b/anon.coffee index 30cc7bc..a678276 100755 --- a/anon.coffee +++ b/anon.coffee @@ -35,9 +35,11 @@ isIpInRange = (ip, block) -> else a = address(ip) b = address(block) + # if we are comparing a v6 address to an v4 cidr range we need to + # convert the v4 cidr to a v6 cidr if not a.v4 and b.v4 - mask = 96 + b.subnetMask - block = '::' + b.toV6Group() + "/" + mask + subnetMask = 96 + b.subnetMask + block = '::' + b.toV6Group() + "/" + subnetMask b = new ipv6.v6.Address(block) a.isInSubnet(b) From d5516dc99205e98413260080a097fdb11274f6de Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Mon, 28 Jul 2014 06:24:12 -0400 Subject: [PATCH 13/14] corrected ipv6 in range tests --- test.coffee | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test.coffee b/test.coffee index d5c5182..cd0a773 100644 --- a/test.coffee +++ b/test.coffee @@ -53,22 +53,22 @@ describe 'anon', -> assert.isTrue isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:0000', '0000:0000:0000:0000:0000:0000:0000:0002'] it 'ipv6 not in range', -> - assert.isFalse isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:1000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + assert.isFalse isIpInRange '0000:0000:0000:0000:0000:0000:0000:0001', ['0000:0000:0000:0000:0000:0000:0000:0002', '0000:0000:0000:0000:0000:0000:0000:0003'] it 'ipv4 in ipv6 range', -> - assert.isTrue isIpInRange '127.0.0.1', ['0000:0000:0000:0000:0000:0000:0000:0000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + assert.isTrue isIpInRange '127.0.0.1', ['0:0:0:0:0:ffff:7f00:1', '0:0:0:0:0:ffff:7f00:2'] it 'ipv4 not in ipv6 range', -> - assert.isFalse isIpInRange '127.0.0.1', ['0000:0000:F000:0000:0000:0000:0000:0000', 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'] + assert.isFalse isIpInRange '127.0.0.3', ['0:0:0:0:0:ffff:7f00:1', '0:0:0:0:0:ffff:7f00:2'] it 'ipv6 in ipv6 cidr', -> assert.isTrue isIpInRange '0000:0000:0000:0000:0000:0000:1000:0005', '0000:0000:0000:0000:0000:0000:1000:0000/112' it 'ipv6 in ipv4 cidr', -> - assert.isTrue isIpInRange '0:0:0:0:0:0:8e33:1', '142.51.0.0/16' + assert.isTrue isIpInRange '0:0:0:0:0:ffff:8e33:1', '142.51.0.0/16' it 'ipv6 not in ipv4 cidr', -> - assert.isFalse isIpInRange '0:0:0:0:0:0:8e34:1', '142.51.0.0/16' + assert.isFalse isIpInRange '0:0:0:0:0:ffff:8e34:1', '142.51.0.0/16' describe 'isIpInAnyRange', -> From 80a4ebeae54486c23eca12ff0d61c8e9b1da69d2 Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Mon, 28 Jul 2014 20:08:32 -0400 Subject: [PATCH 14/14] convert ipv4 and ipv6 ip addresses to ipv6.v6.Address --- anon.coffee | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/anon.coffee b/anon.coffee index a678276..eaadb17 100755 --- a/anon.coffee +++ b/anon.coffee @@ -15,6 +15,9 @@ address = (ip) -> i = new ipv6.v6.Address(ip) else i = new ipv6.v4.Address(ip) + subnetMask = 96 + i.subnetMask + ip = '::ffff:' + i.toV6Group() + "/" + subnetMask + i = new ipv6.v6.Address(ip) ipToInt = (ip) -> i = address(ip) @@ -35,12 +38,6 @@ isIpInRange = (ip, block) -> else a = address(ip) b = address(block) - # if we are comparing a v6 address to an v4 cidr range we need to - # convert the v4 cidr to a v6 cidr - if not a.v4 and b.v4 - subnetMask = 96 + b.subnetMask - block = '::' + b.toV6Group() + "/" + subnetMask - b = new ipv6.v6.Address(block) a.isInSubnet(b) isIpInAnyRange = (ip, blocks) -> @@ -61,7 +58,6 @@ loadJson = (path) -> require path getStatusLength = (edit, name, template) -> - # returns length of the tweet based on shortened url # https://support.twitter.com/articles/78124-posting-links-in-a-tweet fakeUrl = 'http://t.co/BzHLWr31Ce' status = Mustache.render template, name: name, url: fakeUrl, page: edit.page @@ -118,7 +114,8 @@ main = -> if require.main == module main() -# export these for testing +# for testing +exports.address = address exports.compareIps = compareIps exports.isIpInRange = isIpInRange exports.isIpInAnyRange = isIpInAnyRange