Projekt

Obecné

Profil

Stáhnout (1.34 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
#!/usr/bin/env node
2
3
var mdns = require('./')()
4
var path = require('path')
5
var os = require('os')
6
7
var announcing = process.argv.indexOf('--announce') > -1
8
9
if (process.argv.length < 3) {
10
  console.error('Usage: %s <hostname>', path.basename(process.argv[1]))
11
  process.exit(1)
12
}
13
var hostname = process.argv[2]
14
15
if (announcing) {
16
  var ip = getIp()
17
  mdns.on('query', function (query) {
18
    query.questions.forEach(function (q) {
19
      if (q.name === hostname) {
20
        console.log('Responding %s -> %s', q.name, ip)
21
        mdns.respond({
22
          answers: [{
23
            type: 'A',
24
            name: q.name,
25
            data: ip
26
          }]
27
        })
28
      }
29
    })
30
  })
31
} else {
32
  mdns.on('response', function (response) {
33
    response.answers.forEach(function (answer) {
34
      if (answer.name === hostname) {
35
        console.log(answer.data)
36
        process.exit()
37
      }
38
    })
39
  })
40
41
  mdns.query(hostname, 'A')
42
43
  // Give responses 3 seconds to respond
44
  setTimeout(function () {
45
    console.error('Hostname not found')
46
    process.exit(1)
47
  }, 3000)
48
}
49
50
function getIp () {
51
  var networks = os.networkInterfaces()
52
  var found = '127.0.0.1'
53
54
  Object.keys(networks).forEach(function (k) {
55
    var n = networks[k]
56
    n.forEach(function (addr) {
57
      if (addr.family === 'IPv4' && !addr.internal) {
58
        found = addr.address
59
      }
60
    })
61
  })
62
63
  return found
64
}