diff --git a/README.md b/README.md index 59628ed..7b9fc5a 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,10 @@ async function checkAndMountDeveloperImage(udid) { } ``` +### Environment + +* If exists, `USBMUXD_SOCKET_ADDRESS` is used to get usbmuxd socket address. Mostly useful in cases where the `usbmuxd` is run by a non-root user. + ## Test ``` shell diff --git a/lib/usbmux/index.js b/lib/usbmux/index.js index 77e4041..0b3d41a 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; @@ -61,13 +62,28 @@ function swap16 (val) { * @throws {B.TimeoutError} if connection timeout happened * @returns {Promise} Connected socket instance */ -async function getDefaultSocket (opts = {}) { - const { - socketPath = DEFAULT_USBMUXD_SOCKET, - socketPort = DEFAULT_USBMUXD_PORT, - socketHost = DEFAULT_USBMUXD_HOST, - timeout = 5000, - } = opts; +async function getDefaultSocket(opts = {}) { + const defaults = { + socketPath: DEFAULT_USBMUXD_SOCKET, + socketPort: DEFAULT_USBMUXD_PORT, + socketHost: DEFAULT_USBMUXD_HOST, + timeout: 5000 + }; + + if (process.env.USBMUXD_SOCKET_ADDRESS && !opts.socketPath && !opts.socketPort && !opts.socketHost) { + 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):/i, ''); + const [ip, port] = usbmuxdSocketAddress.split(':'); + if (ip && port) { + defaults.socketHost = ip; + defaults.socketPort = parseInt(port, 10); + } else { + defaults.socketPath = usbmuxdSocketAddress; + } + } + + const { socketPath, socketPort, socketHost, timeout } = { ...defaults, ...opts }; let socket; if (await fs.exists(socketPath ?? '')) {