Skip to content

Commit

Permalink
feat: get usbmuxd socket address from USBMUXD_SOCKET_ADDRESS environm…
Browse files Browse the repository at this point in the history
…ent (#190)
  • Loading branch information
muvaf authored Dec 23, 2024
1 parent 09b2342 commit 9a5be45
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 23 additions & 7 deletions lib/usbmux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,13 +62,28 @@ function swap16 (val) {
* @throws {B.TimeoutError} if connection timeout happened
* @returns {Promise<NodeJS.Socket>} 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 ?? '')) {
Expand Down

0 comments on commit 9a5be45

Please sign in to comment.