Skip to content

Commit

Permalink
Merge branch 'ipv6'
Browse files Browse the repository at this point in the history
  • Loading branch information
edsu committed Jul 29, 2014
2 parents 38297cc + 80a4ebe commit 854e7d6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 31 deletions.
54 changes: 35 additions & 19 deletions anon.coffee
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
#!/usr/bin/env coffee

ipv6 = require 'ipv6'
Twit = require 'twit'
{Netmask} = require 'netmask'
minimist = require 'minimist'
{WikiChanges} = require 'wikichanges'
Mustache = require 'mustache'
{WikiChanges} = require 'wikichanges'

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

address = (ip) ->
if ':' in 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) ->
octets = (parseInt(s) for s in ip.split('.'))
result = 0
result += n * Math.pow(256, i) for n, i in octets.reverse()
result
i = address(ip)
i.bigInteger()

compareIps = (ip1, ip2) ->
q1 = ipToInt(ip1)
q2 = ipToInt(ip2)
if q1 == q2
r = ipToInt(ip1).compareTo(ipToInt(ip2))
if r == 0
0
else if q1 < q2
-1
else
else if r > 0
1
else
-1

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 = address(ip)
b = address(block)
a.isInSubnet(b)

isIpInAnyRange = (ip, blocks) ->
blocks.filter((block) -> isIpInRange(ip, block)).length > 0
Expand All @@ -50,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
Expand All @@ -68,9 +75,17 @@ 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
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
Expand All @@ -82,12 +97,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
tweet account, status
tweet account, status, edit

main = ->
config = getConfig argv.config
Expand All @@ -99,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
Expand Down
2 changes: 1 addition & 1 deletion config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 40 additions & 8 deletions test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@ isIpInAnyRange = anon.isIpInAnyRange

describe 'anon', ->

describe "compareIps", ->
describe "compareIps ipv4", ->

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'

describe 'isIpInRange', ->
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 1, compareIps '2600:8:b380:3f3:540b:fdbf:bc5:a6bf', '2600:8:b380:3f3:540b:fdbf:bc5:a6be'

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 ipv4', ->

it 'ip in range', ->
assert.isTrue isIpInRange '123.123.123.123', ['123.123.123.0', '123.123.123.255']
Expand All @@ -32,7 +45,30 @@ 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'

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']

it 'ipv6 not in range', ->
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', ['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.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:ffff:8e33:1', '142.51.0.0/16'

it 'ipv6 not in ipv4 cidr', ->
assert.isFalse isIpInRange '0:0:0:0:0:ffff:8e34:1', '142.51.0.0/16'

describe 'isIpInAnyRange', ->

Expand All @@ -47,10 +83,8 @@ 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', ->
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"]]

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


0 comments on commit 854e7d6

Please sign in to comment.