diff --git a/lib/usbmux/index.js b/lib/usbmux/index.js index 2687d99..0742bb3 100644 --- a/lib/usbmux/index.js +++ b/lib/usbmux/index.js @@ -11,6 +11,7 @@ import PlistService from '../plist-service'; import { Lockdown, LOCKDOWN_PORT } from '../lockdown'; import { BaseServiceSocket } from '../base-service'; import { MB } from '../constants'; +import log from '../logger'; const MAX_FRAME_SIZE = 1 * MB; @@ -62,25 +63,31 @@ function swap16 (val) { * @returns {Promise} Connected socket instance */ async function getDefaultSocket(opts = {}) { - let { - socketPath = DEFAULT_USBMUXD_SOCKET, - socketPort = DEFAULT_USBMUXD_PORT, - socketHost = DEFAULT_USBMUXD_HOST, - timeout = 5000, - } = opts; + const defaults = { + socketPath: DEFAULT_USBMUXD_SOCKET, + socketPort: DEFAULT_USBMUXD_PORT, + socketHost: DEFAULT_USBMUXD_HOST, + timeout: 5000 + }; if (process.env.USBMUXD_SOCKET_ADDRESS) { + log.debug(`Using USBMUXD_SOCKET_ADDRESS environment variable as default socket: ${process.env.USBMUXD_SOCKET_ADDRESS}`); // "unix:" or "UNIX:" prefix is optional for unix socket paths. const usbmuxdSocketAddress = process.env.USBMUXD_SOCKET_ADDRESS.replace(/^(unix|UNIX):/i, ''); - let ipPortPair = usbmuxdSocketAddress.split(':'); - if (ipPortPair.length === 2) { - socketHost = ipPortPair[0]; - socketPort = parseInt(ipPortPair[1], 10); + const [ip, port] = usbmuxdSocketAddress.split(':'); + if (ip && port) { + defaults.socketPath = null; + defaults.socketHost = ip; + defaults.socketPort = parseInt(port, 10); } else { - socketPath = usbmuxdSocketAddress; + defaults.socketPath = usbmuxdSocketAddress; + defaults.socketHost = null; + defaults.socketPort = null; } } + const { socketPath, socketPort, socketHost, timeout } = { ...defaults, ...opts }; + let socket; if (await fs.exists(socketPath ?? '')) { socket = net.createConnection(socketPath ?? '');