Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get usbmuxd socket address from USBMUXD_SOCKET_ADDRESS environment #190

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`);
muvaf marked this conversation as resolved.
Show resolved Hide resolved
// "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
Loading