Created
May 30, 2023 13:24
-
-
Save qgustavor/3a7798606fb8109c22609780115b71ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as dnssd from 'npm:ya-dns-sd' | |
const timeout = Number(Deno.args[0]) || 30 | |
// Check if it's needed and prompt | |
const connectedDevices = new TextDecoder().decode(await Deno.run({ | |
cmd: ['adb', 'devices'], | |
stdout: 'piped' | |
}).output()).split(/\r?\n/g).filter(e => e).slice(1).map(e => e.split('\t')[0]) | |
if (connectedDevices.length > 0) { | |
console.log('Already connected to ' + connectedDevices.join(' ')) | |
console.log('Press y to disconnect and start scanning') | |
if (!confirm()) Deno.exit() | |
await Deno.run({ | |
cmd: ['adb', 'disconnect'] | |
}).status() | |
} | |
const controller = new AbortController() | |
const timer = setTimeout(() => { | |
console.error(`Nothing found after ${timeout} seconds`) | |
controller.abort() | |
Deno.exit(1) | |
}, timeout * 1000) | |
for await ( | |
const service of dnssd.browse({ | |
multicastInterface: new dnssd.MulticastInterface(), | |
service: { | |
protocol: 'tcp', | |
type: 'adb-tls-connect', | |
}, | |
signal: controller.signal | |
}) | |
) { | |
if (!service.isActive) continue | |
const ipPortPair = service.host + ':' + service.port | |
console.log('Connecting to', ipPortPair) | |
await Deno.run({ | |
cmd: ['adb', 'connect', ipPortPair] | |
}).status() | |
clearTimeout(timer) | |
Deno.exit(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check port scanning version here: https://gist.github.com/qgustavor/64141a41907db78401a41b4c494f91b7