source: trip-planner-front/node_modules/multicast-dns/cli.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.3 KB
Line 
1#!/usr/bin/env node
2
3var mdns = require('./')()
4var path = require('path')
5var os = require('os')
6
7var announcing = process.argv.indexOf('--announce') > -1
8
9if (process.argv.length < 3) {
10 console.error('Usage: %s <hostname>', path.basename(process.argv[1]))
11 process.exit(1)
12}
13var hostname = process.argv[2]
14
15if (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
50function 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}
Note: See TracBrowser for help on using the repository browser.