Skip to content

Commit

Permalink
usbmuxd: should use the overrides even if USBMUXD_SOCKET_ADDRESS env …
Browse files Browse the repository at this point in the history
…var is given

Signed-off-by: Muvaffak Onus <[email protected]>
  • Loading branch information
muvaf committed Dec 23, 2024
1 parent 3b447aa commit c9f73f6
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 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 @@ -62,25 +63,28 @@ function swap16 (val) {
* @returns {Promise<NodeJS.Socket>} Connected socket instance
*/
async function getDefaultSocket(opts = {}) {
let {
socketPath = DEFAULT_USBMUXD_SOCKET,
socketPort = DEFAULT_USBMUXD_PORT,
socketHost = DEFAULT_USBMUXD_HOST,
timeout = 5000,
} = opts;

if (process.env.USBMUXD_SOCKET_ADDRESS) {
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|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.socketHost = ip;
defaults.socketPort = parseInt(port, 10);
} else {
socketPath = usbmuxdSocketAddress;
defaults.socketPath = usbmuxdSocketAddress;
}
}

const { socketPath, socketPort, socketHost, timeout } = { ...defaults, ...opts };

let socket;
if (await fs.exists(socketPath ?? '')) {
socket = net.createConnection(socketPath ?? '');
Expand Down

0 comments on commit c9f73f6

Please sign in to comment.