-
Notifications
You must be signed in to change notification settings - Fork 3
/
repeater.js
executable file
·147 lines (132 loc) · 3.27 KB
/
repeater.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
#!/usr/bin/env node
const dgram = require('dgram')
const args = require('yargs')
.usage('$0: Multicast Datagram Repeater')
.option('group', {
describe: 'Multicast group (IPv4addr:port)',
type: 'string',
demandOption: true,
requiresArg: true
})
.option('uport', {
describe: 'Unicast port number',
type: 'number',
demandOption: true,
requiresArg: true
})
.option('send', {
describe: 'Adresses to send to',
type: 'array',
requiresArg: true
})
.option('gcp', {
describe: 'Get send addresses from GCP (zone:group)',
type: 'string',
requiresArg: true
})
.option('azure', {
describe: 'Get send addresses from Azure (resource group)',
type: 'string',
requiresArg: true
})
.option('vmss', {
describe: 'Get the name of a VM Scale Set',
type: 'string',
requiresArg: false
})
.option('vmssTag', {
describe: 'Get the VMSS Scale Sets tag key and value',
type: 'array',
requiresArg: false
})
.option('aws', {
describe: 'Get send addresses from AWS (tag)',
type: 'string',
requiresArg: true
})
.option('interval', {
describe: 'Polling interval in seconds for GCP/Azure/AWS',
type: 'number',
requiresArg: true,
default: 60
})
.option('azureRetry', {
describe: 'Retry interval in seconds for Azure',
type: 'number',
requiresArg: false,
default: 7
})
.option('verbose', {
describe: 'Log each incoming datagram',
alias: 'v',
type: 'boolean',
default: false
})
.argv
const mcast = args.group.split(':')
if (mcast.length !== 2) {
throw new Error('No multicast address and port.')
}
const group = mcast[0]
const mport = mcast[1]
if (mport === args.uport) {
throw new Error('Multicast port and unicast port must be distinct.')
}
const sendTo = {}
if (args.send) {
args.send.forEach((addr) => {
const pieces = addr.split(':')
if (pieces.length === 2) {
sendTo[pieces[0]] = pieces[1]
} else {
sendTo[addr] = args.uport
}
})
}
if (args.gcp) {
require('./gcp.js')(args, sendTo)
}
if (args.azure) {
require('./azure.js')(args, sendTo)
}
if (args.aws) {
require('./aws.js')(args, sendTo)
}
const msock = dgram.createSocket({ type: 'udp4', reuseAddr: true })
const usock = dgram.createSocket({ type: 'udp4', reuseAddr: true })
msock.on('error', (err) => {
console.log(`msock error:\n${err.stack}`)
msock.close()
})
usock.on('error', (err) => {
console.log(`usock error:\n${err.stack}`)
usock.close()
})
msock.on('listening', () => {
const address = msock.address()
msock.addMembership(group)
console.log(`listen on ${address.address}:${address.port} group ${group}`)
})
msock.on('message', (msg, rinfo) => {
if (rinfo.port === args.uport) {
return
}
if (args.verbose) {
console.log(`mc from ${rinfo.address}:${rinfo.port} ${rinfo.size} B`)
}
for (let sendAddr of Object.keys(sendTo)) {
msock.send(msg, sendTo[sendAddr], sendAddr)
}
})
usock.on('listening', () => {
const address = usock.address()
console.log(`listen on ${address.address}:${address.port} unicast`)
})
usock.on('message', (msg, rinfo) => {
if (args.verbose) {
console.log(`uc from ${rinfo.address}:${rinfo.port} ${rinfo.size} B`)
}
usock.send(msg, mport, group)
})
msock.bind(mport)
usock.bind(args.uport)