Created
July 11, 2020 01:06
-
-
Save cuuupid/df8d7f00afea77c2d265ac104b24c01b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https') | |
const webhook = "YOUR WEBHOOK HERE" | |
const log = message => new Promise((done, _) => { | |
const request = https.request(webhook, { | |
method: 'POST', | |
header: { | |
'Content-Type': 'application/json' | |
} | |
}, done) | |
// bind error to console.error so it's not halting | |
request.on('error', console.error) | |
request.write(JSON.stringify({ | |
text: message | |
})) | |
request.end() | |
}) | |
const TOKEN = "YOUR TOKEN HERE" | |
const lookup = ip => new Promise((done, _) => { | |
const request = https.request({ | |
method: "GET", | |
hostname: "reveal.clearbit.com", | |
path: `/v1/companies/find?ip=${ip}`, | |
headers: { | |
"authorization": `Basic ${TOKEN}` | |
} | |
}, res => { | |
let profile = "" | |
res.on('data', d => profile += d) | |
res.on('end', () => done(JSON.parse(profile))) | |
}) | |
// bind error to console.error so it's not halting | |
request.on('error', console.error) | |
request.end() | |
}) | |
const PIXEL = | |
"\x42\x4d\x3c\x00\x00\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00" + | |
"\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x18\x00\x00\x00" + | |
"\x00\x00\x06\x00\x00\x00\x27\x00\x00\x00\x27\x00\x00\x00\x00\x00" + | |
"\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00"; | |
exports.handler = async event => { | |
const response = { | |
statusCode: 500, | |
body: JSON.stringify({ | |
error: 'An error occurred.' | |
}), | |
} | |
try { | |
const ip = event['requestContext']['http']['sourceIp'] | |
const userAgent = event['requestContext']['http']['userAgent'] | |
if (userAgent.includes('github-camo')) { | |
response.body = PIXEL; | |
response.statusCode = 404; | |
await log(`Someone visited my GitHub profile!`) | |
return response; | |
} | |
const profile = await lookup(ip).catch(console.error) | |
if (profile && profile.company) { | |
const name = profile.company.name | |
const domain = profile.company.domain | |
await log(`${name} (${domain}) visited!`); | |
} | |
response.body = PIXEL | |
response.statusCode = 200 | |
return response | |
} catch (e) { | |
console.error(e) | |
return response | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment