Skip to content

Commit

Permalink
Fixed last update bug and cleaner authentication to OpenDNS API
Browse files Browse the repository at this point in the history
  • Loading branch information
Willena committed May 23, 2019
1 parent 4d747bc commit 1598edb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
Expand All @@ -29,7 +31,6 @@

import java.util.Set;

import androidx.core.app.NotificationCompat;
import fr.guillaumevillena.opendnsupdater.OpenDnsUpdater;
import fr.guillaumevillena.opendnsupdater.R;
import fr.guillaumevillena.opendnsupdater.event.InterfaceUpdatedEvent;
Expand Down Expand Up @@ -192,11 +193,8 @@ private void createTimedNotification(Boolean state) {

@Override
public void onTaskFinished(AsyncTask task, Boolean result) {
final Boolean shouldCreateNotification = prefs.getBoolean(PreferenceCodes.APP_NOTIFY, false);
final boolean shouldCreateNotification = prefs.getBoolean(PreferenceCodes.APP_NOTIFY, false);
if (shouldCreateNotification)
createTimedNotification(result);

if (result)
prefs.edit().putLong(PreferenceCodes.OPENDNS_LAST_UPDATE, System.currentTimeMillis()).apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
import android.widget.Switch;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;

import com.wooplr.spotlight.SpotlightConfig;
import com.wooplr.spotlight.utils.SpotlightSequence;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import fr.guillaumevillena.opendnsupdater.BuildConfig;
import fr.guillaumevillena.opendnsupdater.OpenDnsUpdater;
import fr.guillaumevillena.opendnsupdater.R;
Expand Down Expand Up @@ -215,10 +216,14 @@ private void restoreSettings() {
switchEnableEnableOpendnsServers.setChecked(prefs.getBoolean(PreferenceCodes.APP_DNS, false));
switchEnableAutoUpdate.setChecked(prefs.getBoolean(PreferenceCodes.APP_AUTO_UPDATE, false));

long lastUpdate = OpenDnsUpdater.getPrefs().getLong(PreferenceCodes.OPENDNS_LAST_UPDATE, -1);
lastUpdateDateTextView.setText(getString(R.string.main_activity_last_ip_update, lastUpdate != -1 ? DateUtils.getDate(this, lastUpdate) : getString(R.string.text_never)));
printDateLastUpdate();

}

private void printDateLastUpdate() {
long lastUpdate = OpenDnsUpdater.getPrefs().getLong(PreferenceCodes.OPENDNS_LAST_UPDATE, -1);
Log.d(TAG, "restoreSettings: " + lastUpdate);
lastUpdateDateTextView.setText(getString(R.string.main_activity_last_ip_update, lastUpdate != -1 ? DateUtils.getDate(this, lastUpdate) : getString(R.string.text_never)));
}

private void initStateSwitcher(StateSwitcher stateSwitcher, ProgressBar progressBar, AppCompatImageView imgStatus) {
Expand Down Expand Up @@ -381,5 +386,8 @@ public void onTaskFinished(AsyncTask task, Boolean result) {
} else if (task instanceof CheckFakePhishingSite) {
filterPhishingStateSwitcher.setCurrentState(result ? SUCCESS : ERROR);
}

printDateLastUpdate();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;

import com.bugsnag.android.Bugsnag;
Expand All @@ -16,6 +15,7 @@
import javax.net.ssl.SSLException;

import fr.guillaumevillena.opendnsupdater.OpenDnsUpdater;
import fr.guillaumevillena.opendnsupdater.utils.BasicAuthInterceptor;
import fr.guillaumevillena.opendnsupdater.utils.PreferenceCodes;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -43,12 +43,14 @@ public UpdateOnlineIP(TaskFinished asyncEventListener, Configurator configurator
String network = prefs.getString(PreferenceCodes.OPENDNS_NETWORK, "");
Boolean makeUpdate = prefs.getBoolean(PreferenceCodes.APP_AUTO_UPDATE, false);


this.configurator = new Configurator();
this.configurator.setNetwork(network);
this.configurator.setPassword(password);
this.configurator.setUsername(username);
this.configurator.makeUpdate(makeUpdate);


}
}

Expand All @@ -61,26 +63,34 @@ protected Boolean doInBackground(Void... voids) {
}


OkHttpClient client = new OkHttpClient();
OkHttpClient client = new OkHttpClient()
.newBuilder()
.addInterceptor(new BasicAuthInterceptor(this.configurator.username, this.configurator.password))
.build();
// Log.d(TAG, "doInBackground: passwd " + this.configurator.password);

HttpUrl.Builder urlBuilder = HttpUrl.parse("https://updates.opendns.com/nic/update").newBuilder();
urlBuilder.addQueryParameter("hostname", this.configurator.getNetwork());
String url = urlBuilder.build().toString();

try {
Request request = new Request.Builder()
.header("Authorization", "Basic " + Base64.encodeToString((this.configurator.getUsername() + ":" + this.configurator.getPassword()).getBytes("UTF-8"), Base64.NO_WRAP))
.url(url)
.build();

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

if (response.isSuccessful()) {
String data = response.body().string();

Log.d(TAG, "doInBackground: " + data);
OpenDnsUpdater.getPrefs().edit().putLong(PreferenceCodes.OPENDNS_LAST_UPDATE, System.currentTimeMillis()).apply();
return data.contains("good") || data.contains("abuse");
}

Log.d(TAG, "doInBackground: respons");
Log.d(TAG, "doInBackground: " + response.toString());


} catch (UnsupportedEncodingException | ConnectException | UnknownHostException | SSLException | SocketTimeoutException ignored) {
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.guillaumevillena.opendnsupdater.utils;

import java.io.IOException;

import okhttp3.Credentials;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class BasicAuthInterceptor implements Interceptor {

private String credentials;

public BasicAuthInterceptor(String user, String password) {
this.credentials = Credentials.basic(user, password);
}

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request authenticatedRequest = request.newBuilder()
.header("Authorization", credentials).build();
return chain.proceed(authenticatedRequest);
}

}

0 comments on commit 1598edb

Please sign in to comment.