From 86cfac15d59da4c784a6ad35212c0e1586b23e8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Sch=C3=BCller?= Date: Sun, 22 Nov 2020 13:54:54 +0100 Subject: [PATCH] Allow insecure connections --- app/build.gradle | 1 + .../peertube/activity/AccountActivity.java | 4 +- .../peertube/activity/MeActivity.java | 2 +- .../activity/SearchServerActivity.java | 2 +- .../peertube/activity/SettingsActivity.java | 43 +++++++++++- .../peertube/activity/VideoListActivity.java | 4 +- .../fragment/VideoMetaDataFragment.java | 4 +- .../fragment/VideoPlayerFragment.java | 2 +- .../peertube/helper/APIUrlHelper.java | 5 ++ .../peertube/network/RetrofitInstance.java | 15 +++- .../peertube/network/UnsafeOkHttpClient.java | 68 +++++++++++++++++++ .../peertube/service/LoginService.java | 4 +- .../peertube/service/VideoPlayerService.java | 19 +++++- app/src/main/res/values/constants.xml | 2 + app/src/main/res/values/strings.xml | 7 ++ app/src/main/res/xml/root_preferences.xml | 11 +++ build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 18 files changed, 180 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/net/schueller/peertube/network/UnsafeOkHttpClient.java diff --git a/app/build.gradle b/app/build.gradle index fe3e6df..ff59927 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -93,6 +93,7 @@ android { implementation "com.google.android.exoplayer:exoplayer-hls:$libVersions.exoplayer" implementation "com.google.android.exoplayer:exoplayer-smoothstreaming:$libVersions.exoplayer" implementation "com.google.android.exoplayer:extension-mediasession:$libVersions.exoplayer" + implementation "com.google.android.exoplayer:extension-okhttp:$libVersions.exoplayer" // date formatter implementation 'org.ocpsoft.prettytime:prettytime:4.0.4.Final' diff --git a/app/src/main/java/net/schueller/peertube/activity/AccountActivity.java b/app/src/main/java/net/schueller/peertube/activity/AccountActivity.java index 7daf17b..a26d6b3 100644 --- a/app/src/main/java/net/schueller/peertube/activity/AccountActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/AccountActivity.java @@ -97,7 +97,7 @@ public class AccountActivity extends CommonActivity { apiBaseURL = APIUrlHelper.getUrlWithVersion(this); - userService = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetUserService.class); + userService = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(this)).create(GetUserService.class); recyclerViewVideos = findViewById(R.id.account_video_recyclerView); recyclerViewChannels = findViewById(R.id.account_channel_recyclerView); @@ -227,7 +227,7 @@ public class AccountActivity extends CommonActivity { isLoadingVideos = false; - GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetVideoDataService.class); + GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(this)).create(GetVideoDataService.class); Call call; call = service.getAccountVideosData(displayNameAndHost, videosStart, videosCount, videosSort); diff --git a/app/src/main/java/net/schueller/peertube/activity/MeActivity.java b/app/src/main/java/net/schueller/peertube/activity/MeActivity.java index 7a62427..cda962f 100644 --- a/app/src/main/java/net/schueller/peertube/activity/MeActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/MeActivity.java @@ -119,7 +119,7 @@ public class MeActivity extends CommonActivity { String apiBaseURL = APIUrlHelper.getUrlWithVersion(this); String baseURL = APIUrlHelper.getUrl(this); - GetUserService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetUserService.class); + GetUserService service = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(this)).create(GetUserService.class); Call call = service.getMe(); diff --git a/app/src/main/java/net/schueller/peertube/activity/SearchServerActivity.java b/app/src/main/java/net/schueller/peertube/activity/SearchServerActivity.java index b263f39..41f9613 100644 --- a/app/src/main/java/net/schueller/peertube/activity/SearchServerActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/SearchServerActivity.java @@ -148,7 +148,7 @@ public class SearchServerActivity extends CommonActivity { GetServerListDataService service = RetrofitInstance.getRetrofitInstance( APIUrlHelper.getServerIndexUrl(SearchServerActivity.this) - ).create(GetServerListDataService.class); + , APIUrlHelper.useInsecureConnection(this)).create(GetServerListDataService.class); if ( !searchtext.equals( lastSearchtext ) ) { diff --git a/app/src/main/java/net/schueller/peertube/activity/SettingsActivity.java b/app/src/main/java/net/schueller/peertube/activity/SettingsActivity.java index c2e0083..d0a9623 100644 --- a/app/src/main/java/net/schueller/peertube/activity/SettingsActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/SettingsActivity.java @@ -17,14 +17,18 @@ */ package net.schueller.peertube.activity; +import android.content.SharedPreferences; import android.os.Bundle; -import android.view.View; +import android.preference.PreferenceManager; +import android.util.Log; import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AlertDialog.Builder; import androidx.appcompat.widget.Toolbar; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreference; import net.schueller.peertube.BuildConfig; import net.schueller.peertube.R; @@ -68,6 +72,43 @@ public class SettingsActivity extends CommonActivity { assert pref != null; pref.setSummary(Long.toString(BuildConfig.BUILD_TIME)); + // double check disabling SSL + final SwitchPreference insecure = (SwitchPreference) findPreference("pref_accept_insecure"); + if (insecure != null) { + insecure.setOnPreferenceChangeListener((preference, newValue) -> { + + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences.Editor editor = sharedPref.edit(); + + boolean currentValue = sharedPref.getBoolean("pref_accept_insecure", false); + + if (newValue instanceof Boolean && ((Boolean) newValue) != currentValue) { + final boolean enable = (Boolean) newValue; + + Log.v("pref", "enable: " + enable); + Log.v("pref", "currentValue: " + currentValue); + + if (enable) { + new Builder(preference.getContext()) + .setTitle(R.string.pref_insecure_confirm_title) + .setMessage(R.string.pref_insecure_confirm_message) + .setIcon(R.drawable.ic_info_black_24dp) + .setNegativeButton(R.string.pref_insecure_confirm_no, (dialog, whichButton) -> { + // do nothing + }) + .setPositiveButton(R.string.pref_insecure_confirm_yes, (dialog, whichButton) -> { + // OK has been pressed => force the new value and update the checkbox display + editor.putBoolean("pref_accept_insecure", true); + editor.apply(); + insecure.setChecked(true); + }).create().show(); + // by default ignore the pref change, which can only be validated when OK is pressed + return false; + } + } + return true; + }); + } } } } \ No newline at end of file diff --git a/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java b/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java index ae7b650..adcc3c9 100644 --- a/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java @@ -325,13 +325,13 @@ public class VideoListActivity extends CommonActivity { Set languages = sharedPref.getStringSet(getString(R.string.pref_video_language_key), null); String apiBaseURL = APIUrlHelper.getUrlWithVersion(this); - GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetVideoDataService.class); + GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(this)).create(GetVideoDataService.class); Call call; if (!searchQuery.equals("")) { call = service.searchVideosData(start, count, sort, nsfw, searchQuery, filter, languages); } else if (subscriptions) { - GetUserService userService = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetUserService.class); + GetUserService userService = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(this)).create(GetUserService.class); call = userService.getVideosSubscripions(start, count, sort); } else { call = service.getVideosData(start, count, sort, nsfw, filter, languages); diff --git a/app/src/main/java/net/schueller/peertube/fragment/VideoMetaDataFragment.java b/app/src/main/java/net/schueller/peertube/fragment/VideoMetaDataFragment.java index b07634a..5291054 100644 --- a/app/src/main/java/net/schueller/peertube/fragment/VideoMetaDataFragment.java +++ b/app/src/main/java/net/schueller/peertube/fragment/VideoMetaDataFragment.java @@ -100,7 +100,7 @@ public class VideoMetaDataFragment extends Fragment { Activity activity = getActivity(); String apiBaseURL = APIUrlHelper.getUrlWithVersion(context); - GetVideoDataService videoDataService = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetVideoDataService.class); + GetVideoDataService videoDataService = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(context)).create(GetVideoDataService.class); // Thumbs up Button thumbsUpButton = activity.findViewById(R.id.video_thumbs_up); @@ -326,7 +326,7 @@ public class VideoMetaDataFragment extends Fragment { RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json"), "{\"rating\":\"" + ratePayload + "\"}"); String apiBaseURL = APIUrlHelper.getUrlWithVersion(getContext()); - GetVideoDataService videoDataService = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetVideoDataService.class); + GetVideoDataService videoDataService = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(getContext())).create(GetVideoDataService.class); Call call = videoDataService.rateVideo(video.getId(), body); diff --git a/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java b/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java index 397913c..245eaaf 100644 --- a/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java +++ b/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java @@ -184,7 +184,7 @@ public class VideoPlayerFragment extends Fragment implements VideoRendererEventL // get video details from api String apiBaseURL = APIUrlHelper.getUrlWithVersion(context); - GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetVideoDataService.class); + GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(context)).create(GetVideoDataService.class); Call