Commit fb1eef05 authored by Tiago Cunha's avatar Tiago Cunha

App connects to insecure servers.

parent 2bec0df7
...@@ -38,8 +38,10 @@ public class DDPClientWrapper { ...@@ -38,8 +38,10 @@ public class DDPClientWrapper {
/** /**
* Connect to WebSocket server with DDP client. * Connect to WebSocket server with DDP client.
*/ */
public Task<DDPClientCallback.Connect> connect(@Nullable String session) { public Task<DDPClientCallback.Connect> connect(@Nullable String session,
return ddpClient.connect("wss://" + hostname + "/websocket", session); boolean usesSecureConnection) {
final String protocol = usesSecureConnection ? "wss://" : "ws://";
return ddpClient.connect(protocol + hostname + "/websocket", session);
} }
/** /**
......
...@@ -44,8 +44,8 @@ public class InputHostnameFragment extends AbstractServerConfigFragment { ...@@ -44,8 +44,8 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
ServerPolicyHelper.isApiVersionValid(OkHttpHelper.getClientForUploadFile(), hostname, ServerPolicyHelper.isApiVersionValid(OkHttpHelper.getClientForUploadFile(), hostname,
new ServerPolicyHelper.Callback() { new ServerPolicyHelper.Callback() {
@Override @Override
public void isValid() { public void isValid(boolean usesSecureConnection) {
getActivity().runOnUiThread(() -> onServerValid(hostname)); getActivity().runOnUiThread(() -> onServerValid(hostname, usesSecureConnection));
} }
@Override @Override
...@@ -53,6 +53,12 @@ public class InputHostnameFragment extends AbstractServerConfigFragment { ...@@ -53,6 +53,12 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
getActivity().runOnUiThread(() -> getActivity().runOnUiThread(() ->
showError(getString(R.string.input_hostname_invalid_server_message))); showError(getString(R.string.input_hostname_invalid_server_message)));
} }
@Override
public void onNetworkError() {
getActivity().runOnUiThread(() ->
showError(getString(R.string.connection_error_try_later)));
}
}); });
} }
...@@ -68,7 +74,7 @@ public class InputHostnameFragment extends AbstractServerConfigFragment { ...@@ -68,7 +74,7 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
return TextUtils.or(TextUtils.or(editor.getText(), editor.getHint()), "").toString(); return TextUtils.or(TextUtils.or(editor.getText(), editor.getHint()), "").toString();
} }
private void onServerValid(final String hostname) { private void onServerValid(final String hostname, boolean usesSecureConnection) {
RocketChatCache.get(getContext()).edit() RocketChatCache.get(getContext()).edit()
.putString(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID, serverConfigId) .putString(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID, serverConfigId)
.apply(); .apply();
...@@ -79,6 +85,7 @@ public class InputHostnameFragment extends AbstractServerConfigFragment { ...@@ -79,6 +85,7 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
.put(ServerConfig.HOSTNAME, hostname) .put(ServerConfig.HOSTNAME, hostname)
.put(ServerConfig.ERROR, JSONObject.NULL) .put(ServerConfig.ERROR, JSONObject.NULL)
.put(ServerConfig.SESSION, JSONObject.NULL) .put(ServerConfig.SESSION, JSONObject.NULL)
.put(ServerConfig.SECURE_CONNECTION, usesSecureConnection)
.put(ServerConfig.STATE, ServerConfig.STATE_READY))) .put(ServerConfig.STATE, ServerConfig.STATE_READY)))
.continueWith(new LogcatIfError()); .continueWith(new LogcatIfError());
} }
......
...@@ -27,32 +27,20 @@ public class ServerPolicyHelper { ...@@ -27,32 +27,20 @@ public class ServerPolicyHelper {
public static void isApiVersionValid(@NonNull OkHttpClient client, @NonNull String host, public static void isApiVersionValid(@NonNull OkHttpClient client, @NonNull String host,
@NonNull Callback callback) { @NonNull Callback callback) {
Request request; trySecureValidation(client, host, new Callback() {
try { @Override
request = new Request.Builder() public void isValid(boolean usesSecureConnection) {
.url("https://" + host + API_INFO_PATH) callback.isValid(usesSecureConnection);
.get() }
.build();
} catch (Exception e) {
callback.isNotValid();
return;
}
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override @Override
public void onFailure(Call call, IOException exception) { public void isNotValid() {
// some connection error
callback.isNotValid(); callback.isNotValid();
} }
@Override @Override
public void onResponse(Call call, Response response) throws IOException { public void onNetworkError() {
if (!response.isSuccessful() || !isValid(response.body())) { tryInsecureValidation(client, host, callback);
callback.isNotValid();
return;
}
callback.isValid();
} }
}); });
} }
...@@ -104,9 +92,64 @@ public class ServerPolicyHelper { ...@@ -104,9 +92,64 @@ public class ServerPolicyHelper {
return versionParts.length >= 3 && Integer.parseInt(versionParts[1]) >= 49; return versionParts.length >= 3 && Integer.parseInt(versionParts[1]) >= 49;
} }
private static void trySecureValidation(@NonNull OkHttpClient client, @NonNull String host,
@NonNull Callback callback) {
Request request;
try {
request = createRequest("https://", host);
} catch (Exception e) {
callback.isNotValid();
return;
}
validate(request, client, callback, true);
}
private static void tryInsecureValidation(@NonNull OkHttpClient client, @NonNull String host,
@NonNull Callback callback) {
Request request;
try {
request = createRequest("http://", host);
} catch (Exception e) {
callback.isNotValid();
return;
}
validate(request, client, callback, false);
}
private static Request createRequest(@NonNull String protocol, @NonNull String host) {
return new Request.Builder()
.url(protocol + host + API_INFO_PATH)
.get()
.build();
}
private static void validate(@NonNull Request request, @NonNull OkHttpClient client,
@NonNull Callback callback, boolean usesSecureConnection) {
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException exception) {
callback.onNetworkError();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful() || !isValid(response.body())) {
callback.isNotValid();
return;
}
callback.isValid(usesSecureConnection);
}
});
}
public interface Callback { public interface Callback {
void isValid(); void isValid(boolean usesSecureConnection);
void isNotValid(); void isNotValid();
void onNetworkError();
} }
} }
...@@ -19,6 +19,7 @@ public class ServerConfig extends RealmObject { ...@@ -19,6 +19,7 @@ public class ServerConfig extends RealmObject {
public static final String HOSTNAME = "hostname"; public static final String HOSTNAME = "hostname";
public static final String STATE = "state"; public static final String STATE = "state";
public static final String SESSION = "session"; public static final String SESSION = "session";
public static final String SECURE_CONNECTION = "secureConnection";
public static final String ERROR = "error"; public static final String ERROR = "error";
public static final int STATE_READY = 0; public static final int STATE_READY = 0;
...@@ -30,6 +31,7 @@ public class ServerConfig extends RealmObject { ...@@ -30,6 +31,7 @@ public class ServerConfig extends RealmObject {
private String hostname; private String hostname;
private int state; private int state;
private String session; private String session;
private boolean secureConnection;
private String error; private String error;
/** /**
...@@ -93,6 +95,14 @@ public class ServerConfig extends RealmObject { ...@@ -93,6 +95,14 @@ public class ServerConfig extends RealmObject {
this.session = session; this.session = session;
} }
public boolean usesSecureConnection() {
return secureConnection;
}
public void setSecureConnection(boolean usesSecureConnection) {
this.secureConnection = usesSecureConnection;
}
public String getError() { public String getError() {
return error; return error;
} }
......
...@@ -170,7 +170,7 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -170,7 +170,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
realm.where(ServerConfig.class).equalTo(ServerConfig.ID, serverConfigId).findFirst()); realm.where(ServerConfig.class).equalTo(ServerConfig.ID, serverConfigId).findFirst());
prepareWebSocket(config.getHostname()); prepareWebSocket(config.getHostname());
return ddpClient.connect(config.getSession()) return ddpClient.connect(config.getSession(), config.usesSecureConnection())
.onSuccessTask(task -> { .onSuccessTask(task -> {
final String session = task.getResult().session; final String session = task.getResult().session;
defaultRealm.executeTransaction(realm -> defaultRealm.executeTransaction(realm ->
......
...@@ -41,4 +41,5 @@ ...@@ -41,4 +41,5 @@
<string name="video_upload_message_spec_title">Attach video</string> <string name="video_upload_message_spec_title">Attach video</string>
<string name="input_hostname_invalid_server_message">Invalid server version</string> <string name="input_hostname_invalid_server_message">Invalid server version</string>
<string name="connection_error_try_later">There\'s a connection error. Please try later.</string>
</resources> </resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment