-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathanon.js
executable file
·307 lines (270 loc) · 7.79 KB
/
anon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env node
const fs = require('fs')
const Twit = require('twit')
const async = require('async')
const minimist = require('minimist')
const Mastodon = require('mastodon')
const Mustache = require('mustache')
const puppeteer = require('puppeteer')
const {WikiChanges} = require('wikichanges')
const {Address4, Address6} = require('ip-address')
const argv = minimist(process.argv.slice(2), {
default: {
verbose: false,
config: './config.json'
}
})
function address(ip) {
if (Array.from(ip).includes(':')) {
return new Address6(ip)
} else {
i = new Address4(ip)
const subnetMask = 96 + i.subnetMask
ip = `::ffff:${i.toGroup6()}/${subnetMask}`
return new Address6(ip)
}
}
function ipToInt(ip) {
return address(ip).bigInteger()
}
function compareIps(ip1, ip2) {
const r = ipToInt(ip1).compareTo(ipToInt(ip2))
if (r === 0) {
return 0
} else if (r > 0) {
return 1
} else {
return -1
}
}
function isIpInRange(ip, block) {
if (Array.isArray(block)) {
return (compareIps(ip, block[0]) >= 0) && (compareIps(ip, block[1]) <= 0)
} else {
const a = address(ip)
const b = address(block)
return a.isInSubnet(b)
}
}
function isIpInAnyRange(ip, blocks) {
for (let block of Array.from(blocks)) {
if (isIpInRange(ip, block)) {
return true
}
}
return false
}
function getConfig(path) {
const config = loadJson(path)
// see if ranges are externally referenced as a separate .json files
if (config.accounts) {
for (let account of Array.from(config.accounts)) {
if (typeof account.ranges === 'string') {
account.ranges = loadJson(account.ranges)
}
}
}
console.log("loaded config from", path)
return config
}
function loadJson(path) {
if ((path[0] !== '/') && (path.slice(0, 2) !== './')) {
path = `./${path}`
}
return require(path)
}
function getStatusLength(edit, name, template) {
// https://support.twitter.com/articles/78124-posting-links-in-a-tweet
const fakeUrl = 'https://t.co/BzHLWr31Ce'
const status = Mustache.render(template, {name, url: fakeUrl, page: edit.page})
return status.length
}
function getStatus(edit, name, template) {
let page = edit.page
const len = getStatusLength(edit, name, template)
if (len > 280) {
const newLength = edit.page.length - (len - 279)
page = edit.page.slice(0, +newLength + 1 || undefined)
}
return Mustache.render(template, {
name,
url: edit.url,
page
})
}
const lastChange = {}
function isRepeat(edit) {
const k = `${edit.wikipedia}`
const v = `${edit.page}:${edit.user}`
const r = lastChange[k] === v
lastChange[k] = v
return r
}
async function takeScreenshot(url) {
// write the screenshot to this file
const filename = Date.now() + '.png'
const browser = await puppeteer.launch({
args: ['--no-sandbox'],
headless: true,
defaultViewport: {
width: 1024,
height: 768,
}
})
const page = await browser.newPage()
await page.goto(url, {waitUntil: 'networkidle0'})
// get the diff portion of the page
const box = await page.evaluate(() => {
const element = document.querySelector('table.diff.diff-contentalign-left')
// need to unpack values because DOMRect object isn't returnable
const {x, y, width, height} = element.getBoundingClientRect()
return {x, y, width, height}
})
// resize viewport in case the diff element isn't fully visible
await page.setViewport({
width: 1024,
height: parseInt(box.y + box.height + 20)
})
// take the screenshot!
await page.screenshot({
path: filename,
clip: box
})
await browser.close()
return filename
}
async function sendStatus(account, status, edit) {
console.log(status)
if (!argv.noop && (!account.throttle || !isRepeat(edit))) {
// wait a couple seconds and get a screenshot of the diff
await new Promise(r => setTimeout(r, 2000));
const screenshot = await takeScreenshot(edit.url)
// Mastodon
if (account.mastodon) {
const mastodon = new Mastodon(account.mastodon)
const response = await mastodon.post('media', {file: fs.createReadStream(screenshot)})
if (!response.data.id) {
console.log('error uploading screenshot to mastodon')
return
}
await mastodon.post(
'statuses',
{'status': status, media_ids: [response.data.id]},
err => {
if (err) {
console.log(`mastodon post failed: ${err}`)
}
}
)
}
// Twitter
if (account.access_token) {
const twitter = new Twit(account)
const b64content = fs.readFileSync(screenshot, {encoding: 'base64'})
// upload the screenshot to twitter
twitter.post('media/upload', {media_data: b64content}, function(err, data, response) {
if (err) {
console.log(err)
return
}
// add alt text for the media, for use by screen readers
const mediaIdStr = data.media_id_string
const altText = "Screenshot of edit to " + edit.page
const metaParams = {media_id: mediaIdStr, alt_text: {text: altText}}
twitter.post('media/metadata/create', metaParams, function(err, data, response) {
if (err) {
console.log('metadata upload for twitter screenshot alt text failed with error', err)
}
const params = {
'status': status,
'media_ids': [mediaIdStr]
}
twitter.post('statuses/update', params, function(err) {
if (err) {
console.log(err)
}
})
})
})
}
// screenshot no longer needed
fs.unlinkSync(screenshot)
}
}
function inspect(account, edit) {
if (edit.url) {
if (account.whitelist && account.whitelist[edit.wikipedia]
&& account.whitelist[edit.wikipedia][edit.page]) {
status = getStatus(edit, edit.user, account.template)
sendStatus(account, status, edit)
} else if (account.ranges && edit.anonymous) {
for (let name in account.ranges) {
const ranges = account.ranges[name]
if (isIpInAnyRange(edit.user, ranges)) {
status = getStatus(edit, name, account.template)
sendStatus(account, status, edit)
}
}
}
}
}
function checkConfig(config, error) {
if (config.accounts) {
return async.each(config.accounts, canTweet, error)
} else {
return error("missing accounts stanza in config")
}
}
function canTweet(account, error) {
if (!account.access_token) {
error(null)
} else {
try {
const twitter = new Twit(account)
const a = account['access_token']
return twitter.get('search/tweets', {q: 'cats'}, function(err, data, response) {
if (err) {
error(err + " for access_token " + a)
} else if (!response.headers['x-access-level'] ||
(response.headers['x-access-level'].substring(0,10) !== 'read-write')) {
error(`no read-write permission for access token ${a}`)
} else {
error(null)
}
})
} catch (err) {
error(`unable to create twitter client for account: ${account}`)
}
}
}
function main() {
const config = getConfig(argv.config)
return checkConfig(config, function(err) {
if (!err) {
const wikipedia = new WikiChanges({ircNickname: config.nick})
return wikipedia.listen(edit => {
if (argv.verbose) {
console.log(JSON.stringify(edit))
}
Array.from(config.accounts).map((account) =>
inspect(account, edit))
})
} else {
return console.log(err)
}
})
}
if (require.main === module) {
main()
}
module.exports = {
main,
address,
ipToInt,
compareIps,
isIpInRange,
isIpInAnyRange,
getConfig,
getStatus,
takeScreenshot
}