Skip to content

Commit

Permalink
Major refactoring 💣 🔥 💀
Browse files Browse the repository at this point in the history
  • Loading branch information
simov committed Jul 25, 2017
1 parent c1698e6 commit bbbd64b
Show file tree
Hide file tree
Showing 19 changed files with 702 additions and 196 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

language: node_js

node_js:
- 8

after_script:
- npm run test-cov
- cat ./coverage/lcov.info | coveralls

sudo: false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) VarnaLab <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@

# varnalab-whois / online
# varnalab-whois-online

[![travis-ci]][travis] [![coveralls-status]][coveralls]

```bash
git clone [email protected]:VarnaLab/varnalab-whois-online.git
cd varnalab-whois-online
npm i --production
```


# mikrotik.json
Expand All @@ -22,20 +30,17 @@
```


# macdb.json
# known.json

```json
{
"known": [],
"unknown": []
}
[]
```

# unknown.json

# cronjob.env.sh

Set NodeJS `VERSION` and `/path/to`

```json
[]
```

# whois-online.sh

Expand All @@ -45,15 +50,15 @@ Set all `/path/to` locations
# @varnalab/cli

```bash
npm install -g @varnalab/cli
npm i -g @varnalab/cli
```


# crontab

```bash
# periodic update curl -k https://json.varnalab.org/services/whois-active.json on every 5 minutes
*/5 * * * * (. /path/to/cronjob.env.sh; /path/to/whois-online.sh; )
# update on every 5 minutes
*/5 * * * * /path/to/whois-online.sh
```


Expand Down Expand Up @@ -94,3 +99,10 @@ server {
- https://json.varnalab.org/services/whois-known.json
- https://json.varnalab.org/services/whois-unknown.json
- https://json.varnalab.org/services/whois-online.json


[travis-ci]: https://img.shields.io/travis/varnalab/varnalab-whois-online/master.svg?style=flat-square (Build Status - Travis CI)
[coveralls-status]: https://img.shields.io/coveralls/varnalab/varnalab-whois-online.svg?style=flat-square (Test Coverage - Coveralls)

[travis]: https://travis-ci.org/varnalab/varnalab-whois-online
[coveralls]: https://coveralls.io/github/varnalab/varnalab-whois-online
76 changes: 76 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env node

var argv = require('minimist')(process.argv.slice(2))

if (argv.help) {
console.log('--known /path/to/known.json')
console.log('--unknown /path/to/unknown.json')
process.exit()
}

if (!argv.known) {
console.error('Specify /path/to/known.json')
process.exit()
}

if (!argv.unknown) {
console.error('Specify /path/to/unknown.json')
process.exit()
}

var fs = require('fs')
var path = require('path')

var fpath = {
known: path.resolve(process.cwd(), argv.known),
unknown: path.resolve(process.cwd(), argv.unknown)
}
var config = {
known: require(fpath.known),
unknown: require(fpath.unknown)
}
var online = require('../lib/online')()
var unknown = require('../lib/unknown')()
var output = require('../lib/output')()
var resolve = require('../lib/mac-resolve')()


;(() => {
var data = ''
process.stdin.on('data', (chunk) => {
data += chunk
})
process.stdin.on('end', (chunk) => {
try {
var json = JSON.parse(data)
execute(json)
}
catch (err) {
process.exit()
}
})
})()


function execute (json) {

var active = online.filter(config, json)

unknown.update(config, active, resolve, () => {

fs.writeFileSync(
fpath.unknown, JSON.stringify(config.unknown, null, 2),
'utf8'
)

unknown.active(config, active)
var whois = output.whois(config, active)

// used in varnalab.github.io
console.log(JSON.stringify(whois.online))
// used in varnalab.slack.com
console.log(JSON.stringify({attachments: whois.active}))
console.log(JSON.stringify({attachments: whois.known}))
console.log(JSON.stringify({attachments: whois.unknown}))
})
}
8 changes: 0 additions & 8 deletions cronjob.env.sh

This file was deleted.

60 changes: 60 additions & 0 deletions lib/mac-resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

var https = require('https')


module.exports = (config) => {
var options = config || {
method: 'GET',
protocol: 'https:',
host: 'api.macvendors.com',
agent: new https.Agent({
keepAlive: true,
maxSockets: 1,
}),
timeout: 3000,
}

var client = require(options.protocol.replace(':', ''))

function get (mac, done) {
var req = client.request(Object.assign({}, options, {path: '/' + mac}))

req.on('error', (err) => {
done(err)
})

req.on('timeout', () => {
req.abort()
})

req.on('response', (res) => {
var body = ''
res.on('error', (err) => {
done(err)
})
res.on('data', (chunk) => {
body += chunk
})
res.on('end', () => {
done(null, (body === 'Vendor not found\n') ? false : body)
})
})

req.end()
}

var resolve = (list, done) => {
;(function next (index) {
if (index === list.length) {
done()
return
}
get(list[index].mac.slice(0, 8), (err, vendor) => {
list[index].vendor = vendor === undefined ? null : vendor
next(++index)
})
})(0)
}

return resolve
}
33 changes: 33 additions & 0 deletions lib/online.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

module.exports = () => {

var filter = (config, json) => {
var known = {}
var unknown = []

json.active.forEach((active) => {
var found = config.known
.filter((user) => user.mac.indexOf(active.mac) !== -1)
// better match (if needed)
// .filter((user) => user.mac.some((mac) =>
// mac.replace(/[-:\s]/g, '').toLowerCase() ===
// active.mac.replace(/[-:\s]/g, '').toLowerCase()
// ))

if (found.length) {
var user = found[0]
known[user.id] = user
}
else {
unknown.push(active)
}
})

return {
known: Object.keys(known).map((key) => known[key]),
unknown
}
}

return {filter}
}
53 changes: 53 additions & 0 deletions lib/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

module.exports = () => {
var slack = (known, unknown) => [
{
text: known
.reduce((attachment, user) => (
attachment +=
(
user.slack
? '<https://varnalab.slack.com/team/' +
user.slack + '|@' + user.slack + '> '
: ''
) +
'_' + user.name + '_\n',
attachment
), ''),
mrkdwn_in: ['text']
},
{
text: unknown
.reduce((attachment, device) => (
attachment += '_' + device.host + '_\n',
attachment
), ''),
mrkdwn_in: ['text']
}
]

var online = (known, unknown) => ({
known: known.map((user) => user.id),
unknown: unknown.map((device) => ({host: device.host, vendor: device.vendor}))
})

var whois = (config, active) => (
(
empty =
active.known.length || active.unknown.length
) => ({
// used in varnalab.github.io
online: empty
? online(active.known, active.unknown)
: [],
// used in varnalab.slack.com
active: empty
? slack(active.known, active.unknown)
: {text: '_Няма никой_', mrkdwn_in: ['text']},
known: slack(config.known, []),
unknown: slack([], config.unknown),
})
)()

return {slack, online, whois}
}
Loading

0 comments on commit bbbd64b

Please sign in to comment.