Skip to content

Support TLS connection for Android to RN DevServer #47952

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ protected Void doInBackground(Void... params) {
public String getWebsocketProxyURL() {
return String.format(
Locale.US,
"ws://%s/debugger-proxy?role=client",
"%s://%s/debugger-proxy?role=client",
mPackagerConnectionSettings.getDebugServerWsProtocol(),
mPackagerConnectionSettings.getDebugServerHost());
}

Expand Down Expand Up @@ -325,7 +326,8 @@ private String getInspectorDeviceId() {
private String getInspectorDeviceUrl() {
return String.format(
Locale.US,
"http://%s/inspector/device?name=%s&app=%s&device=%s&profiling=%b",
"%s://%s/inspector/device?name=%s&app=%s&device=%s&profiling=%b",
mPackagerConnectionSettings.getDebugServerHttpProtocol(),
mPackagerConnectionSettings.getDebugServerHost(),
Uri.encode(AndroidInfoHelpers.getFriendlyDeviceName()),
Uri.encode(mPackageName),
Expand Down Expand Up @@ -379,16 +381,16 @@ private boolean getJSMinifyMode() {
return mSettings.isJSMinifyEnabled();
}

private String createBundleURL(String mainModuleID, BundleType type, String host) {
return createBundleURL(mainModuleID, type, host, false, true);
private String createBundleURL(String mainModuleID, BundleType type, String protocol, String host) {
return createBundleURL(mainModuleID, type, protocol, host, false, true);
}

private String createSplitBundleURL(String mainModuleID, String host) {
return createBundleURL(mainModuleID, BundleType.BUNDLE, host, true, false);
private String createSplitBundleURL(String mainModuleID, String protocol, String host) {
return createBundleURL(mainModuleID, BundleType.BUNDLE, protocol, host, true, false);
}

private String createBundleURL(
String mainModuleID, BundleType type, String host, boolean modulesOnly, boolean runModule) {
String mainModuleID, BundleType type, String protocol, String host, boolean modulesOnly, boolean runModule) {
boolean dev = getDevMode();
StringBuilder additionalOptionsBuilder = new StringBuilder();
for (Map.Entry<String, String> entry :
Expand All @@ -400,7 +402,8 @@ private String createBundleURL(
}
return String.format(
Locale.US,
"http://%s/%s.%s?platform=android&dev=%s&lazy=%s&minify=%s&app=%s&modulesOnly=%s&runModule=%s",
"%s://%s/%s.%s?platform=android&dev=%s&lazy=%s&minify=%s&app=%s&modulesOnly=%s&runModule=%s",
protocol,
host,
mainModuleID,
type.typeID(),
Expand All @@ -415,36 +418,38 @@ private String createBundleURL(
}

private String createBundleURL(String mainModuleID, BundleType type) {
return createBundleURL(mainModuleID, type, mPackagerConnectionSettings.getDebugServerHost());
return createBundleURL(mainModuleID, type, mPackagerConnectionSettings.getDebugServerHttpProtocol(), mPackagerConnectionSettings.getDebugServerHost());
}

private static String createResourceURL(String host, String resourcePath) {
return String.format(Locale.US, "http://%s/%s", host, resourcePath);
private static String createResourceURL(String protocol, String host, String resourcePath) {
return String.format(Locale.US, "%s://%s/%s", protocol, host, resourcePath);
}

public String getDevServerBundleURL(final String jsModulePath) {
return createBundleURL(
jsModulePath, BundleType.BUNDLE, mPackagerConnectionSettings.getDebugServerHost());
jsModulePath, BundleType.BUNDLE, mPackagerConnectionSettings.getDebugServerHttpProtocol(), mPackagerConnectionSettings.getDebugServerHost());
}

public String getDevServerSplitBundleURL(String jsModulePath) {
return createSplitBundleURL(jsModulePath, mPackagerConnectionSettings.getDebugServerHost());
return createSplitBundleURL(jsModulePath, mPackagerConnectionSettings.getDebugServerHttpProtocol(), mPackagerConnectionSettings.getDebugServerHost());
}

public void isPackagerRunning(final PackagerStatusCallback callback) {
String protocol = mPackagerConnectionSettings.getDebugServerHttpProtocol();
String host = mPackagerConnectionSettings.getDebugServerHost();
if (host == null) {
FLog.w(ReactConstants.TAG, "No packager host configured.");
callback.onPackagerStatusFetched(false);
} else {
mPackagerStatusCheck.run(host, callback);
mPackagerStatusCheck.run(protocol, host, callback);
}
}

private String createLaunchJSDevtoolsCommandUrl() {
return String.format(
Locale.US,
"http://%s/launch-js-devtools",
"%s://%s/launch-js-devtools",
mPackagerConnectionSettings.getDebugServerHttpProtocol(),
mPackagerConnectionSettings.getDebugServerHost());
}

Expand Down Expand Up @@ -478,8 +483,8 @@ public String getSourceUrl(String mainModuleName) {
public String getJSBundleURLForRemoteDebugging(String mainModuleName) {
// The host we use when connecting to the JS bundle server from the emulator is not the
// same as the one needed to connect to the same server from the JavaScript proxy running on the
// host itself.
return createBundleURL(mainModuleName, BundleType.BUNDLE, getHostForJSProxy());
// host itself. Http protocol will always be used because it's pointing to localhost in this case
return createBundleURL(mainModuleName, BundleType.BUNDLE, "http", getHostForJSProxy());
}

/**
Expand All @@ -491,7 +496,7 @@ public String getJSBundleURLForRemoteDebugging(String mainModuleName) {
public @Nullable File downloadBundleResourceFromUrlSync(
final String resourcePath, final File outputFile) {
final String resourceURL =
createResourceURL(mPackagerConnectionSettings.getDebugServerHost(), resourcePath);
createResourceURL(mPackagerConnectionSettings.getDebugServerHttpProtocol(), mPackagerConnectionSettings.getDebugServerHost(), resourcePath);
final Request request = new Request.Builder().url(resourceURL).build();

try (Response response = mClient.newCall(request).execute()) {
Expand Down Expand Up @@ -521,7 +526,8 @@ public void openDebugger(@Nullable final ReactContext context, final String erro
String requestUrl =
String.format(
Locale.US,
"http://%s/open-debugger?device=%s",
"%s://%s/open-debugger?device=%s",
mPackagerConnectionSettings.getDebugServerHttpProtocol(),
mPackagerConnectionSettings.getDebugServerHost(),
Uri.encode(getInspectorDeviceId()));
Request request =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class PackagerStatusCheck {

private static final String PACKAGER_OK_STATUS = "packager-status:running";
private static final int HTTP_CONNECT_TIMEOUT_MS = 5000;
private static final String PACKAGER_STATUS_URL_TEMPLATE = "http://%s/status";
private static final String PACKAGER_STATUS_URL_TEMPLATE = "%s://%s/status";

private final OkHttpClient mClient;

Expand All @@ -44,8 +44,8 @@ public PackagerStatusCheck(OkHttpClient client) {
mClient = client;
}

public void run(String host, final PackagerStatusCallback callback) {
String statusURL = createPackagerStatusURL(host);
public void run(String protocol, String host, final PackagerStatusCallback callback) {
String statusURL = createPackagerStatusURL(protocol, host);
Request request = new Request.Builder().url(statusURL).build();

mClient
Expand Down Expand Up @@ -96,7 +96,7 @@ public void onResponse(Call call, Response response) throws IOException {
});
}

private static String createPackagerStatusURL(String host) {
return String.format(Locale.US, PACKAGER_STATUS_URL_TEMPLATE, host);
private static String createPackagerStatusURL(String protocol, String host) {
return String.format(Locale.US, PACKAGER_STATUS_URL_TEMPLATE, protocol, host);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public JSPackagerClient(

Uri.Builder builder = new Uri.Builder();
builder
.scheme("ws")
.scheme(settings.getDebugServerWsProtocol())
.encodedAuthority(settings.getDebugServerHost())
.appendPath("message")
.appendQueryParameter("device", AndroidInfoHelpers.getFriendlyDeviceName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public open class PackagerConnectionSettings(private val appContext: Context) {
preferences.edit().putString(PREFS_DEBUG_SERVER_HOST_KEY, host).apply()
}

public open val debugServerHttpProtocol: String
get() {
val tlsFromSettings = preferences.getBoolean(PREFS_DEBUG_SERVER_TLS_KEY, false)
return if (tlsFromSettings) "https" else "http"
}

public open val debugServerWsProtocol: String
get() {
val tlsFromSettings = preferences.getBoolean(PREFS_DEBUG_SERVER_TLS_KEY, false)
return if (tlsFromSettings) "wss" else "ws"
}

public fun setAdditionalOptionForPackager(key: String, value: String) {
_additionalOptionsForPackager[key] = value
}
Expand All @@ -51,5 +63,6 @@ public open class PackagerConnectionSettings(private val appContext: Context) {
private companion object {
private val TAG = PackagerConnectionSettings::class.java.simpleName
private const val PREFS_DEBUG_SERVER_HOST_KEY = "debug_http_host"
private const val PREFS_DEBUG_SERVER_TLS_KEY = "debug_http_tls"
}
}