Skip to content

Commit 854e7d6

Browse files
committed
Merge branch 'ipv6'
2 parents 38297cc + 80a4ebe commit 854e7d6

File tree

4 files changed

+79
-31
lines changed

4 files changed

+79
-31
lines changed

anon.coffee

+35-19
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
#!/usr/bin/env coffee
22

3+
ipv6 = require 'ipv6'
34
Twit = require 'twit'
4-
{Netmask} = require 'netmask'
55
minimist = require 'minimist'
6-
{WikiChanges} = require 'wikichanges'
76
Mustache = require 'mustache'
7+
{WikiChanges} = require 'wikichanges'
88

99
argv = minimist process.argv.slice(2), default:
1010
verbose: false
1111
config: './config.json'
1212

13+
address = (ip) ->
14+
if ':' in ip
15+
i = new ipv6.v6.Address(ip)
16+
else
17+
i = new ipv6.v4.Address(ip)
18+
subnetMask = 96 + i.subnetMask
19+
ip = '::ffff:' + i.toV6Group() + "/" + subnetMask
20+
i = new ipv6.v6.Address(ip)
21+
1322
ipToInt = (ip) ->
14-
octets = (parseInt(s) for s in ip.split('.'))
15-
result = 0
16-
result += n * Math.pow(256, i) for n, i in octets.reverse()
17-
result
23+
i = address(ip)
24+
i.bigInteger()
1825

1926
compareIps = (ip1, ip2) ->
20-
q1 = ipToInt(ip1)
21-
q2 = ipToInt(ip2)
22-
if q1 == q2
27+
r = ipToInt(ip1).compareTo(ipToInt(ip2))
28+
if r == 0
2329
0
24-
else if q1 < q2
25-
-1
26-
else
30+
else if r > 0
2731
1
32+
else
33+
-1
2834

2935
isIpInRange = (ip, block) ->
3036
if Array.isArray block
3137
compareIps(ip, block[0]) >= 0 and compareIps(ip, block[1]) <= 0
3238
else
33-
new Netmask(block).contains ip
39+
a = address(ip)
40+
b = address(block)
41+
a.isInSubnet(b)
3442

3543
isIpInAnyRange = (ip, blocks) ->
3644
blocks.filter((block) -> isIpInRange(ip, block)).length > 0
@@ -50,7 +58,6 @@ loadJson = (path) ->
5058
require path
5159

5260
getStatusLength = (edit, name, template) ->
53-
# returns length of the tweet based on shortened url
5461
# https://support.twitter.com/articles/78124-posting-links-in-a-tweet
5562
fakeUrl = 'http://t.co/BzHLWr31Ce'
5663
status = Mustache.render template, name: name, url: fakeUrl, page: edit.page
@@ -68,9 +75,17 @@ getStatus = (edit, name, template) ->
6875
url: edit.url
6976
page: page
7077

71-
tweet = (account, status) ->
78+
lastChange = {}
79+
isRepeat = (edit) ->
80+
k = "#{edit.wikipedia}"
81+
v = "#{edit.page}:#{edit.user}"
82+
r = lastChange[k] == v
83+
lastChange[k] = v
84+
return r
85+
86+
tweet = (account, status, edit) ->
7287
console.log status
73-
unless argv.noop
88+
unless argv.noop or (account.throttle and isRepeat(edit))
7489
twitter = new Twit account
7590
twitter.post 'statuses/update', status: status, (err) ->
7691
console.log err if err
@@ -82,12 +97,12 @@ inspect = (account, edit) ->
8297
if account.whitelist and account.whitelist[edit.wikipedia] \
8398
and account.whitelist[edit.wikipedia][edit.page]
8499
status = getStatus edit, edit.user, account.template
85-
tweet account, status
100+
tweet account, status, edit
86101
else if account.ranges and edit.anonymous
87102
for name, ranges of account.ranges
88103
if isIpInAnyRange edit.user, ranges
89104
status = getStatus edit, name, account.template
90-
tweet account, status
105+
tweet account, status, edit
91106

92107
main = ->
93108
config = getConfig argv.config
@@ -99,7 +114,8 @@ main = ->
99114
if require.main == module
100115
main()
101116

102-
# export these for testing
117+
# for testing
118+
exports.address = address
103119
exports.compareIps = compareIps
104120
exports.isIpInRange = isIpInRange
105121
exports.isIpInAnyRange = isIpInAnyRange

config.json.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"consumer_secret": "",
77
"access_token": "",
88
"access_token_secret": "",
9-
"template": "{{page}} Wikipedia article edited anonymously by {{name}} {{&url}}",
9+
"template": "{{page}} Wikipedia article edited anonymously from {{name}} {{&url}}",
1010
"ranges": {
1111
"US House of Representatives": [
1212
"143.231.0.0/16",

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"version": "0.0.3",
77
"dependencies": {
88
"twit": "latest",
9-
"wikichanges": ">=0.2.3",
9+
"wikichanges": ">=0.2.4",
1010
"coffee-script": "latest",
1111
"minimist": "latest",
12-
"netmask": "latest",
13-
"mustache": "latest"
12+
"mustache": "latest",
13+
"ipv6": "latest"
1414
},
1515
"devDependencies": {
1616
"mocha": "latest",

test.coffee

+40-8
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,29 @@ isIpInAnyRange = anon.isIpInAnyRange
88

99
describe 'anon', ->
1010

11-
describe "compareIps", ->
11+
describe "compareIps ipv4", ->
1212

1313
it 'equal', ->
1414
assert.equal 0, compareIps '1.1.1.1', '1.1.1.1'
15+
1516
it 'greater than', ->
1617
assert.equal 1, compareIps '1.1.1.2', '1.1.1.1'
18+
1719
it 'less than', ->
1820
assert.equal -1, compareIps '1.1.1.1', '1.1.1.2'
1921

20-
describe 'isIpInRange', ->
22+
describe "compareIps ipv6", ->
23+
24+
it 'equal', ->
25+
assert.equal 0, compareIps '2601:8:b380:3f3:540b:fdbf:bc5:a6bf', '2601:8:b380:3f3:540b:fdbf:bc5:a6bf'
26+
27+
it 'greater than', ->
28+
assert.equal 1, compareIps '2600:8:b380:3f3:540b:fdbf:bc5:a6bf', '2600:8:b380:3f3:540b:fdbf:bc5:a6be'
29+
30+
it 'less than', ->
31+
assert.equal -1, compareIps '2600:8:b380:3f3:540b:fdbf:bc5:a6be', '2601:8:b380:3f3:540b:fdbf:bc5:a6bf'
32+
33+
describe 'isIpInRange ipv4', ->
2134

2235
it 'ip in range', ->
2336
assert.isTrue isIpInRange '123.123.123.123', ['123.123.123.0', '123.123.123.255']
@@ -32,7 +45,30 @@ describe 'anon', ->
3245
assert.isTrue isIpInRange '123.123.123.123', '123.123.0.0/16'
3346

3447
it 'ip is not in cidr range', ->
35-
assert.isFalse isIpInRange '123.123.123.123', '123.123.123.122/32'
48+
assert.isFalse isIpInRange '123.123.124.1', '123.123.123.0/24'
49+
50+
describe 'isIpInRange ipv6', ->
51+
52+
it 'ipv6 in range', ->
53+
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']
54+
55+
it 'ipv6 not in range', ->
56+
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']
57+
58+
it 'ipv4 in ipv6 range', ->
59+
assert.isTrue isIpInRange '127.0.0.1', ['0:0:0:0:0:ffff:7f00:1', '0:0:0:0:0:ffff:7f00:2']
60+
61+
it 'ipv4 not in ipv6 range', ->
62+
assert.isFalse isIpInRange '127.0.0.3', ['0:0:0:0:0:ffff:7f00:1', '0:0:0:0:0:ffff:7f00:2']
63+
64+
it 'ipv6 in ipv6 cidr', ->
65+
assert.isTrue isIpInRange '0000:0000:0000:0000:0000:0000:1000:0005', '0000:0000:0000:0000:0000:0000:1000:0000/112'
66+
67+
it 'ipv6 in ipv4 cidr', ->
68+
assert.isTrue isIpInRange '0:0:0:0:0:ffff:8e33:1', '142.51.0.0/16'
69+
70+
it 'ipv6 not in ipv4 cidr', ->
71+
assert.isFalse isIpInRange '0:0:0:0:0:ffff:8e34:1', '142.51.0.0/16'
3672

3773
describe 'isIpInAnyRange', ->
3874

@@ -47,10 +83,8 @@ describe 'anon', ->
4783

4884
it 'ip not in any ranges', ->
4985
assert.isFalse isIpInAnyRange '1.1.1.6', [r1, r2]
50-
51-
describe 'IP Range Error (#12)', ->
5286

53-
it 'false positive not in ranges', ->
87+
it 'false positive not in ranges #12', ->
5488
assert.isFalse isIpInAnyRange '199.19.250.20', [["199.19.16.0", "199.19.27.255"], ["4.42.247.224", "4.42.247.255"]]
5589
assert.isFalse isIpInAnyRange '39.255.255.148', [["40.0.0.0", "40.127.255.255"], ["40.144.0.0", "40.255.255.255"]]
5690

@@ -72,5 +106,3 @@ describe 'anon', ->
72106
template = "{{page}} edited by {{name}} {{&url}}"
73107
result = getStatus edit, name, template
74108
assert.isTrue result.length <= 140
75-
76-

0 commit comments

Comments
 (0)