Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Add support for TLS on pre-lollipop & fix scrapper (Resolves #9)
Browse files Browse the repository at this point in the history
  • Loading branch information
niranjan94 committed Aug 11, 2017
1 parent 0bef273 commit 33d9267
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 49 deletions.
34 changes: 17 additions & 17 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ configurations {
}

android {
compileSdkVersion 24
buildToolsVersion '24.0.2'
compileSdkVersion 25
buildToolsVersion '25.0.2'

defaultConfig {
applicationId "com.njlabs.amrita.aid"
minSdkVersion 15
targetSdkVersion 24
targetSdkVersion 25
versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
resConfigs "en"
Expand Down Expand Up @@ -53,15 +53,19 @@ android {

dependencies {

compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-crash:9.6.1'
compile 'com.google.firebase:firebase-messaging:9.6.1'
compile 'com.google.firebase:firebase-invites:9.6.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile('com.mikepenz:materialdrawer:3.0.9@aar') {
transitive = true
}

compile 'com.google.firebase:firebase-core:10.0.1'
compile 'com.google.firebase:firebase-crash:10.0.1'
compile 'com.google.firebase:firebase-messaging:10.0.1'
compile 'com.google.firebase:firebase-invites:10.0.1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'org.apache.commons:commons-lang3:3.3.2'
compile 'commons-io:commons-io:2.4'
compile 'org.jsoup:jsoup:1.8.3'
Expand All @@ -78,16 +82,12 @@ dependencies {
compile 'com.jaredrummler:material-spinner:1.1.0'
compile 'jp.wasabeef:recyclerview-animators:2.2.1'
compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.google.code.gson:gson:2.7'
compile 'org.mnode.ical4j:ical4j:1.0.5'
compile 'backport-util-concurrent:backport-util-concurrent:3.1'
compile 'commons-codec:commons-codec:1.10'
compile 'com.niranjan.ln:ln:1.1.2'
apt 'com.github.hotchemi:permissionsdispatcher-processor:2.0.1'
compile('com.mikepenz:materialdrawer:3.0.9@aar') {
transitive = true
}

}

apply plugin: 'com.google.gms.google-services'
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -90,7 +92,11 @@ public Aums(Context context, ProgressResponseBody.ProgressListener progressListe
this.context = context;
client = new AumsClient(context);
client.setProgressListener(progressListener);
client.powerUp();
try {
client.powerUp();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
semesterMapping = new HashMap<>();
loadSemesterMapping();
}
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/com/njlabs/amrita/aid/gpms/client/Gpms.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.jsoup.select.Elements;

import java.io.File;
import java.security.KeyManagementException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
Expand All @@ -76,7 +77,11 @@ public Gpms(Context context) {
this.context = context;
client = new GpmsClient(context);
setupAuthentication();
client.powerUp();
try {
client.powerUp();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}

cookiePrefFile = context.getSharedPreferences(PersistentCookieStore.GPMS_COOKIE_PREFS, Context.MODE_PRIVATE);

Expand All @@ -89,7 +94,11 @@ public Gpms(Context context, String cookiePrefFilename) {
this.context = context;
client = new GpmsClient(context, cookiePrefFilename);
setupAuthentication();
client.powerUp();
try {
client.powerUp();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}

cookiePrefFile = context.getSharedPreferences(cookiePrefFilename, Context.MODE_PRIVATE);

Expand Down
40 changes: 28 additions & 12 deletions app/src/main/java/com/njlabs/amrita/aid/news/NewsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@
import android.widget.TextView;

import com.activeandroid.ActiveAndroid;
import com.google.firebase.crash.FirebaseCrash;
import com.njlabs.amrita.aid.BaseActivity;
import com.njlabs.amrita.aid.R;
import com.njlabs.amrita.aid.util.ExtendedSwipeRefreshLayout;
import com.njlabs.amrita.aid.util.TLSSocketFactory;
import com.njlabs.amrita.aid.util.okhttp.Client;
import com.onemarker.ln.logger.Ln;
import com.squareup.picasso.Picasso;

import org.jsoup.Jsoup;
Expand All @@ -53,6 +57,8 @@
import org.jsoup.select.Elements;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -119,10 +125,16 @@ public void run() {
}
})).start();

client = new OkHttpClient.Builder()
.followRedirects(true)
.followSslRedirects(true)
.build();
Client.initializeSSLContext(this);
try {
client = new OkHttpClient.Builder()
.sslSocketFactory(new TLSSocketFactory())
.followRedirects(true)
.followSslRedirects(true)
.build();
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}

}

Expand All @@ -133,10 +145,11 @@ private void getNews(final Boolean refresh) {

client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
public void onFailure(Call call, final IOException e) {
((Activity) baseContext).runOnUiThread(new Runnable() {
@Override
public void run() {
Ln.e(e);
swipeRefreshLayout.setRefreshing(false);
Snackbar.make(parentView, "Can't establish a reliable connection to the server.", Snackbar.LENGTH_SHORT)
.setAction("Retry", new View.OnClickListener() {
Expand Down Expand Up @@ -170,15 +183,18 @@ public void run() {
Document doc = Jsoup.parse(responseString);
Elements articles = doc.select("article");
for (Element article : articles) {
Element header = article.select(".flexslider").first();
Element content = article.select(".group-blog-content").first();
Element footer = article.select(".group-blog-footer").first();
String imageUrl = header.select("ul > li > img").first().attr("src");
String title = content.select(".field-name-title > div > div > h2").first().text();
String url = "https://www.amrita.edu" + footer.select(".field-name-node-link > div > div > a").first().attr("href");
newsArticles.add(new NewsModel(imageUrl, title, url));
Ln.d(article.html());
try {
String imageUrl = article.select("img.img-responsive").first().attr("src");
String title = article.select(".field-name-title").first().text();
String url = "https://www.amrita.edu" + article.select(".field-name-node-link > div > div > a").first().attr("href");
newsArticles.add(new NewsModel(imageUrl, title, url));
} catch (Exception e) {
FirebaseCrash.report(e);
}
}


(new Thread(new Runnable() {
@Override
public void run() {
Expand Down
41 changes: 28 additions & 13 deletions app/src/main/java/com/njlabs/amrita/aid/news/NewsUpdateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@
import com.activeandroid.ActiveAndroid;
import com.google.firebase.crash.FirebaseCrash;
import com.njlabs.amrita.aid.R;
import com.njlabs.amrita.aid.util.TLSSocketFactory;
import com.njlabs.amrita.aid.util.okhttp.Client;
import com.onemarker.ln.logger.Ln;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -82,16 +87,25 @@ private void getNews(final Boolean refresh, final List<NewsModel> oldArticles) {

List<NewsModel> currentArticles;

OkHttpClient client = new OkHttpClient.Builder()
.followRedirects(true)
.followSslRedirects(true)
.build();
Client.initializeSSLContext(this);

OkHttpClient client = null;
try {
client = new OkHttpClient.Builder()
.sslSocketFactory(new TLSSocketFactory())
.followRedirects(true)
.followSslRedirects(true)
.build();
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}

Request request = new Request.Builder()
.url("https://www.amrita.edu/campus/Coimbatore/news")
.build();

try {

Response response = client.newCall(request).execute();

if (response.isSuccessful()) {
Expand All @@ -107,15 +121,16 @@ private void getNews(final Boolean refresh, final List<NewsModel> oldArticles) {
Document doc = Jsoup.parse(responseString);
Elements articles = doc.select("article");
for (Element article : articles) {
Element header = article.select(".flexslider").first();
Element content = article.select(".group-blog-content").first();
Element footer = article.select(".group-blog-footer").first();
String imageUrl = header.select("ul > li > img").first().attr("src");
String title = content.select(".field-name-title > div > div > h2").first().text();
String url = "https://www.amrita.edu" + footer.select(".field-name-node-link > div > div > a").first().attr("href");

inboxStyle.addLine(title);
currentArticles.add(new NewsModel(imageUrl, title, url));
Ln.d(article.html());
try {
String imageUrl = article.select("img.img-responsive").first().attr("src");
String title = article.select(".field-name-title").first().text();
String url = "https://www.amrita.edu" + article.select(".field-name-node-link > div > div > a").first().attr("href");
inboxStyle.addLine(title);
currentArticles.add(new NewsModel(imageUrl, title, url));
} catch (Exception e) {
FirebaseCrash.report(e);
}
}

ActiveAndroid.beginTransaction();
Expand Down
70 changes: 70 additions & 0 deletions app/src/main/java/com/njlabs/amrita/aid/util/TLSSocketFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.njlabs.amrita.aid.util;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class TLSSocketFactory extends SSLSocketFactory {

private SSLSocketFactory delegate;

public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
delegate = context.getSocketFactory();
}

@Override
public String[] getDefaultCipherSuites() {
return delegate.getDefaultCipherSuites();
}

@Override
public String[] getSupportedCipherSuites() {
return delegate.getSupportedCipherSuites();
}

@Override
public Socket createSocket() throws IOException {
return enableTLSOnSocket(delegate.createSocket());
}

@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose));
}

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return enableTLSOnSocket(delegate.createSocket(host, port));
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort));
}

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(delegate.createSocket(host, port));
}

@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort));
}

private Socket enableTLSOnSocket(Socket socket) {
if(socket != null && (socket instanceof SSLSocket)) {
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"});
}
return socket;
}
}
Loading

0 comments on commit 33d9267

Please sign in to comment.