diff --git a/src/dispatcher/androidDevice.js b/src/dispatcher/androidDevice.js index b663d3eb8..f2b270aef 100644 --- a/src/dispatcher/androidDevice.js +++ b/src/dispatcher/androidDevice.js @@ -14,6 +14,7 @@ import type BaseDevice from '../devices/BaseDevice'; import type Logger from '../fb-stubs/Logger.js'; import {registerDeviceCallbackOnPlugins} from '../utils/onRegisterDevice.js'; import {reportPlatformFailures} from '../utils/metrics'; +import adbConfig from '../utils/adbConfig'; const adb = require('adbkit-fb'); function createDevice( @@ -79,11 +80,7 @@ export default (store: Store, logger: Logger) => { ); } }) - .then(() => - adb.createClient({ - port: process.env.ANDROID_ADB_SERVER_PORT || '5037', - }), - ), + .then(() => adb.createClient(adbConfig())), 'createADBClient.shell', ).catch(err => { console.error( @@ -92,7 +89,7 @@ export default (store: Store, logger: Logger) => { /* In the event that starting adb with the above method fails, fallback to using adbkit, though its known to be unreliable. */ - const unsafeClient = adb.createClient(); + const unsafeClient = adb.createClient(adbConfig()); return reportPlatformFailures( promiseRetry( (retry, number) => { diff --git a/src/utils/CertificateProvider.js b/src/utils/CertificateProvider.js index 81000b685..64ba47c7d 100644 --- a/src/utils/CertificateProvider.js +++ b/src/utils/CertificateProvider.js @@ -20,6 +20,7 @@ const tmpFile = promisify(tmp.file); const tmpDir = promisify(tmp.dir); import iosUtil from '../fb-stubs/iOSContainerUtility'; import {reportPlatformFailures} from './metrics'; +import adbConfig from './adbConfig'; // Desktop file paths const os = require('os'); @@ -75,9 +76,7 @@ export default class CertificateProvider { constructor(server: Server, logger: LogManager) { this.logger = logger; - this.adb = adb.createClient({ - port: process.env.ANDROID_ADB_SERVER_PORT || '5037', - }); + this.adb = adb.createClient(adbConfig()); this.certificateSetup = reportPlatformFailures( this.ensureServerCertExists(), 'ensureServerCertExists', diff --git a/src/utils/__tests__/adbConfig.js b/src/utils/__tests__/adbConfig.js new file mode 100644 index 000000000..ba04a22a8 --- /dev/null +++ b/src/utils/__tests__/adbConfig.js @@ -0,0 +1,45 @@ +/** + * Copyright 2018-present Facebook. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @format + */ + +import adbConfig from '../adbConfig'; + +test('get host and port from ADB_SERVER_SOCKET', () => { + process.env.ANDROID_ADB_SERVER_PORT = undefined; + process.env.ADB_SERVER_SOCKET = 'tcp:127.0.0.1:5037'; + const {port, host} = adbConfig(); + expect(port).toBe(5037); + expect(host).toBe('127.0.0.1'); +}); + +test('get IPv6 address from ADB_SERVER_SOCKET', () => { + process.env.ANDROID_ADB_SERVER_PORT = undefined; + process.env.ADB_SERVER_SOCKET = 'tcp::::1:5037'; + const {host} = adbConfig(); + expect(host).toBe(':::1'); +}); + +test('get port from ANDROID_ADB_SERVER_PORT', () => { + process.env.ANDROID_ADB_SERVER_PORT = '1337'; + process.env.ADB_SERVER_SOCKET = undefined; + const {port} = adbConfig(); + expect(port).toBe(1337); +}); + +test('prefer ADB_SERVER_SOCKET over ANDROID_ADB_SERVER_PORT', () => { + process.env.ANDROID_ADB_SERVER_PORT = '1337'; + process.env.ADB_SERVER_SOCKET = 'tcp:127.0.0.1:5037'; + const {port} = adbConfig(); + expect(port).toBe(5037); +}); + +test('have defaults', () => { + process.env.ANDROID_ADB_SERVER_PORT = undefined; + process.env.ADB_SERVER_SOCKET = undefined; + const {port, host} = adbConfig(); + expect(port).toBe(5037); + expect(host).toBe('localhost'); +}); diff --git a/src/utils/adbConfig.js b/src/utils/adbConfig.js new file mode 100644 index 000000000..8f2b3d290 --- /dev/null +++ b/src/utils/adbConfig.js @@ -0,0 +1,25 @@ +/** + * Copyright 2018-present Facebook. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @format + */ + +export default () => { + let port = parseInt(process.env.ANDROID_ADB_SERVER_PORT, 10) || 5037; + let host = 'localhost'; + + const socket = (process.env.ADB_SERVER_SOCKET || '').trim(); + if (socket && socket.length > 0) { + const match = socket.match(/^(tcp:)(\S+):(\d+)/); + if (match && match.length === 4) { + host = match[2]; + port = parseInt(match[3], 10); + } + } + + return { + port, + host, + }; +};