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",
"%s://%s/inspector/device?name=%s&app=%s&device=%s",
mPackagerConnectionSettings.debugServerHttpProtocol(),
mPackagerConnectionSettings.getDebugServerHost(),
Uri.encode(AndroidInfoHelpers.getFriendlyDeviceName()),
Uri.encode(mPackageName),
Expand Down Expand Up @@ -378,16 +380,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 @@ -399,7 +401,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 @@ -414,20 +417,20 @@ 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) {
Expand All @@ -443,7 +446,8 @@ public void isPackagerRunning(final PackagerStatusCallback 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 @@ -490,7 +494,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 @@ -520,7 +524,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 @@ -41,6 +41,30 @@ public open class PackagerConnectionSettings(private val appContext: Context) {
preferences.edit().putString(PREFS_DEBUG_SERVER_HOST_KEY, host).apply()
}

public open var debugServerHttpProtocol: String
get() {
// Check host setting first. If empty try to detect emulator type and use default
// hostname for those
val tlsFromSettings = preferences.getBoolean(PREFS_DEBUG_SERVER_TLS_KEY, false)
if (tlsFromSettings) {
return "https"
}

return "http"
}

public open var debugServerWsProtocol: String
get() {
// Check host setting first. If empty try to detect emulator type and use default
// hostname for those
val tlsFromSettings = preferences.getBoolean(PREFS_DEBUG_SERVER_TLS_KEY, false)
if (tlsFromSettings) {
return "wss"
}

return "ws"
}

public fun setAdditionalOptionForPackager(key: String, value: String) {
_additionalOptionsForPackager[key] = value
}
Expand All @@ -51,5 +75,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"
}
}