From f98ae2002eef4f35ca892580d0964e76b2e59656 Mon Sep 17 00:00:00 2001 From: tom79 Date: Fri, 29 Nov 2019 15:26:49 +0100 Subject: [PATCH] Check redirect --- .../android/client/Entities/Status.java | 53 ++++++++++++++++--- .../android/client/HttpsConnection.java | 38 +++++++++++++ app/src/main/res/menu/links_popup.xml | 4 ++ app/src/main/res/values/strings.xml | 2 + 4 files changed, 89 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/client/Entities/Status.java b/app/src/main/java/app/fedilab/android/client/Entities/Status.java index f9dc18327..b67cc9787 100644 --- a/app/src/main/java/app/fedilab/android/client/Entities/Status.java +++ b/app/src/main/java/app/fedilab/android/client/Entities/Status.java @@ -27,6 +27,7 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.os.Parcel; import android.os.Parcelable; import android.text.Html; @@ -64,6 +65,8 @@ import com.github.penfeizhou.animation.apng.decode.APNGParser; import com.github.penfeizhou.animation.gif.GifDrawable; import com.github.penfeizhou.animation.gif.decode.GifParser; +import net.gotev.uploadservice.http.HttpConnection; + import java.io.File; import java.net.URI; import java.net.URISyntaxException; @@ -86,6 +89,7 @@ import app.fedilab.android.activities.PeertubeActivity; import app.fedilab.android.activities.ShowAccountActivity; import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask; import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask; +import app.fedilab.android.client.HttpsConnection; import app.fedilab.android.fragments.TabLayoutNotificationsFragment; import app.fedilab.android.helper.CrossActions; import app.fedilab.android.helper.CustomQuoteSpan; @@ -667,19 +671,20 @@ public class Status implements Parcelable { PopupMenu popup = new PopupMenu(context, textView); popup.getMenuInflater() .inflate(R.menu.links_popup, popup.getMenu()); + int style; + if (theme == Helper.THEME_DARK) { + style = R.style.DialogDark; + } else if (theme == Helper.THEME_BLACK) { + style = R.style.DialogBlack; + } else { + style = R.style.Dialog; + } popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.action_show_link: int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); - int style; - if (theme == Helper.THEME_DARK) { - style = R.style.DialogDark; - } else if (theme == Helper.THEME_BLACK) { - style = R.style.DialogBlack; - } else { - style = R.style.Dialog; - } + AlertDialog.Builder builder = new AlertDialog.Builder(context, style); builder.setMessage(url); builder.setTitle(context.getString(R.string.display_full_link)); @@ -716,6 +721,38 @@ public class Status implements Parcelable { Toasty.info(context, context.getString(R.string.clipboard_url), Toast.LENGTH_LONG).show(); } break; + case R.id.action_unshorten: + Thread thread = new Thread() { + @Override + public void run() { + String response = new HttpsConnection(context, null).checkUrl(url); + + Handler mainHandler = new Handler(context.getMainLooper()); + + Runnable myRunnable = new Runnable() { + @Override + public void run() { + AlertDialog.Builder builder = new AlertDialog.Builder(context, style); + if( response != null ) { + builder.setMessage(response); + }else{ + builder.setMessage(R.string.no_redirect); + } + builder.setTitle(context.getString(R.string.check_redirect)); + builder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }).show(); + } + }; + mainHandler.post(myRunnable); + + } + }; + thread.start(); + break; } return true; } diff --git a/app/src/main/java/app/fedilab/android/client/HttpsConnection.java b/app/src/main/java/app/fedilab/android/client/HttpsConnection.java index bd2655243..c6f959d38 100644 --- a/app/src/main/java/app/fedilab/android/client/HttpsConnection.java +++ b/app/src/main/java/app/fedilab/android/client/HttpsConnection.java @@ -43,6 +43,7 @@ import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.PasswordAuthentication; +import java.net.ProtocolException; import java.net.Proxy; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -76,6 +77,8 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import static app.fedilab.android.helper.Helper.urlPattern; + /** * Created by Thomas on 17/11/2017. @@ -253,6 +256,41 @@ public class HttpsConnection { + + public String checkUrl(String urlConnection){ + URL url; + String redirect = null; + try { + url = new URL(urlConnection); + if (proxy != null) + httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setRequestProperty("http.keepAlive", "false"); + httpsURLConnection.setInstanceFollowRedirects(false); + httpsURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"); + httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); + httpsURLConnection.setRequestMethod("GET"); + if( httpsURLConnection.getResponseCode() == 301) { + Map> map = httpsURLConnection.getHeaderFields(); + for (Map.Entry> entry : map.entrySet()) { + if (entry.toString().startsWith("Location") || entry.toString().startsWith("Location")) { + Matcher matcher = urlPattern.matcher(entry.toString()); + if (matcher.find()) { + redirect = matcher.group(1); + } + } + } + } + httpsURLConnection.getInputStream().close(); + return redirect != null && redirect.compareTo(urlConnection)!=0?redirect:null; + } catch (IOException | NoSuchAlgorithmException | KeyManagementException e) { + e.printStackTrace(); + } + return null; + } + + public String get(String urlConnection) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { if (Build.VERSION.SDK_INT >= 21) { diff --git a/app/src/main/res/menu/links_popup.xml b/app/src/main/res/menu/links_popup.xml index 77d8f7e89..8a8bfeb32 100644 --- a/app/src/main/res/menu/links_popup.xml +++ b/app/src/main/res/menu/links_popup.xml @@ -17,4 +17,8 @@ android:id="@+id/action_copy_link" android:title="@string/copy_link" /> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 155a964c1..188fdcf59 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1194,4 +1194,6 @@ Share link The URL has been copied to the clipboard Open with another app + Check redirect + This URL does not redirect \ No newline at end of file