Commit fb1eef05 authored by Tiago Cunha's avatar Tiago Cunha

App connects to insecure servers.

parent 2bec0df7
......@@ -38,8 +38,10 @@ public class DDPClientWrapper {
/**
* Connect to WebSocket server with DDP client.
*/
public Task<DDPClientCallback.Connect> connect(@Nullable String session) {
return ddpClient.connect("wss://" + hostname + "/websocket", session);
public Task<DDPClientCallback.Connect> connect(@Nullable String 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 {
ServerPolicyHelper.isApiVersionValid(OkHttpHelper.getClientForUploadFile(), hostname,
new ServerPolicyHelper.Callback() {
@Override
public void isValid() {
getActivity().runOnUiThread(() -> onServerValid(hostname));
public void isValid(boolean usesSecureConnection) {
getActivity().runOnUiThread(() -> onServerValid(hostname, usesSecureConnection));
}
@Override
......@@ -53,6 +53,12 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
getActivity().runOnUiThread(() ->
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 {
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()
.putString(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID, serverConfigId)
.apply();
......@@ -79,6 +85,7 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
.put(ServerConfig.HOSTNAME, hostname)
.put(ServerConfig.ERROR, JSONObject.NULL)
.put(ServerConfig.SESSION, JSONObject.NULL)
.put(ServerConfig.SECURE_CONNECTION, usesSecureConnection)
.put(ServerConfig.STATE, ServerConfig.STATE_READY)))
.continueWith(new LogcatIfError());
}
......
......@@ -27,32 +27,20 @@ public class ServerPolicyHelper {
public static void isApiVersionValid(@NonNull OkHttpClient client, @NonNull String host,
@NonNull Callback callback) {
Request request;
try {
request = new Request.Builder()
.url("https://" + host + API_INFO_PATH)
.get()
.build();
} catch (Exception e) {
callback.isNotValid();
return;
}
client.newCall(request).enqueue(new okhttp3.Callback() {
trySecureValidation(client, host, new Callback() {
@Override
public void onFailure(Call call, IOException exception) {
// some connection error
callback.isNotValid();
public void isValid(boolean usesSecureConnection) {
callback.isValid(usesSecureConnection);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful() || !isValid(response.body())) {
public void isNotValid() {
callback.isNotValid();
return;
}
callback.isValid();
@Override
public void onNetworkError() {
tryInsecureValidation(client, host, callback);
}
});
}
......@@ -104,9 +92,64 @@ public class ServerPolicyHelper {
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 {
void isValid();
void isValid(boolean usesSecureConnection);
void isNotValid();
void onNetworkError();
}
}
......@@ -19,6 +19,7 @@ public class ServerConfig extends RealmObject {
public static final String HOSTNAME = "hostname";
public static final String STATE = "state";
public static final String SESSION = "session";
public static final String SECURE_CONNECTION = "secureConnection";
public static final String ERROR = "error";
public static final int STATE_READY = 0;
......@@ -30,6 +31,7 @@ public class ServerConfig extends RealmObject {
private String hostname;
private int state;
private String session;
private boolean secureConnection;
private String error;
/**
......@@ -93,6 +95,14 @@ public class ServerConfig extends RealmObject {
this.session = session;
}
public boolean usesSecureConnection() {
return secureConnection;
}
public void setSecureConnection(boolean usesSecureConnection) {
this.secureConnection = usesSecureConnection;
}
public String getError() {
return error;
}
......
......@@ -170,7 +170,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
realm.where(ServerConfig.class).equalTo(ServerConfig.ID, serverConfigId).findFirst());
prepareWebSocket(config.getHostname());
return ddpClient.connect(config.getSession())
return ddpClient.connect(config.getSession(), config.usesSecureConnection())
.onSuccessTask(task -> {
final String session = task.getResult().session;
defaultRealm.executeTransaction(realm ->
......
......@@ -41,4 +41,5 @@
<string name="video_upload_message_spec_title">Attach video</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>
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