generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
certgen.js
61 lines (50 loc) · 1.88 KB
/
certgen.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
const mdns = require('mdns');
const { AndroidRemote } = require('androidtv-remote');
const Readline = require('readline');
let browser = mdns.createBrowser(mdns.tcp('googlecast'), { resolverSequence: [
mdns.rst.DNSServiceResolve(),
'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({ families: [0] }),
mdns.rst.makeAddressesUnique(),
]});
let line = Readline.createInterface({ input: process.stdin, output: process.stdout });
async function generateCert(ip) {
let options = {
pairing_port: 6467,
remote_port: 6466,
name: 'androidtv-remote',
};
let androidRemote = new AndroidRemote(ip, options);
androidRemote.on('secret', () => {
line.question('Enter code displayed on screen : ', async (code) => {
androidRemote.sendCode(code);
});
});
androidRemote.on('error', error => {
console.error('Warning - Error Encountered :', error);
});
androidRemote.on('unpaired', () => {
console.error('Warning - Device Disconnected');
});
androidRemote.on('ready', async () => {
console.log('Code successful, pending certificate...');
await new Promise(resolve => setTimeout(resolve, 2000));
let cert = androidRemote.getCertificate();
console.log('Certificate:');
console.log('');
console.log(cert);
console.log('');
console.log('Paste the above certificate in the config for this accessory as \'tvCert\' property');
process.exit();
});
await androidRemote.start();
}
console.log('Ensure your device is turned on and you can see the display');
line.question('Device Name (copy from config) : ', name => {
console.log('Searching for device...');
browser.on('serviceUp', service => {
if (service.txtRecord.fn.toLowerCase() === name.toLowerCase()) {
generateCert(service.addresses.find(address => (address.match(/\./g) || []).length === 3));
}
});
browser.start();
});