From 7ac053a2b2037e29065fa0e3915adc55dccd880b Mon Sep 17 00:00:00 2001 From: Felix Bechstein Date: Mon, 28 Sep 2009 22:09:50 +0200 Subject: [PATCH] start blocking --- src/de/ub0r/android/adBlock/AdBlock.java | 18 ++- src/de/ub0r/android/adBlock/Proxy.java | 215 ++++++++++++++++++++++--------- 2 files changed, 165 insertions(+), 68 deletions(-) diff --git a/src/de/ub0r/android/adBlock/AdBlock.java b/src/de/ub0r/android/adBlock/AdBlock.java index 3350ae4..07777ab 100644 --- a/src/de/ub0r/android/adBlock/AdBlock.java +++ b/src/de/ub0r/android/adBlock/AdBlock.java @@ -27,9 +27,9 @@ public class AdBlock extends Activity implements OnClickListener, OnItemClickListener { /** Preferences: Port. */ - private static final String PREFS_PORT = "port"; + static final String PREFS_PORT = "port"; /** Preferences: Filter. */ - private static final String PREFS_FILTER = "filter"; + static final String PREFS_FILTER = "filter"; /** ItemDialog: edit. */ private static final short ITEM_DIALOG_EDIT = 0; @@ -81,10 +81,8 @@ public class AdBlock extends Activity implements OnClickListener, lv.setOnItemClickListener(this); } - /** Called on pause. */ - @Override - public final void onPause() { - super.onPause(); + /** Save Preferences. */ + private void savePreferences() { SharedPreferences.Editor editor = this.preferences.edit(); editor.putString(PREFS_PORT, ((EditText) this.findViewById(R.id.port)) .getText().toString()); @@ -96,6 +94,13 @@ public class AdBlock extends Activity implements OnClickListener, editor.commit(); } + /** Called on pause. */ + @Override + public final void onPause() { + super.onPause(); + this.savePreferences(); + } + /** * OnClickListener. * @@ -106,6 +111,7 @@ public class AdBlock extends Activity implements OnClickListener, public final void onClick(final View v) { switch (v.getId()) { case R.id.start_service: + this.savePreferences(); this.startService(new Intent(this, Proxy.class)); break; case R.id.stop_service: diff --git a/src/de/ub0r/android/adBlock/Proxy.java b/src/de/ub0r/android/adBlock/Proxy.java index 446a6a4..f9eef1c 100644 --- a/src/de/ub0r/android/adBlock/Proxy.java +++ b/src/de/ub0r/android/adBlock/Proxy.java @@ -11,11 +11,14 @@ import java.io.OutputStreamWriter; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; +import java.util.ArrayList; import android.app.Service; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.os.IBinder; +import android.preference.PreferenceManager; import android.widget.Toast; /** @@ -23,10 +26,21 @@ import android.widget.Toast; */ public class Proxy extends Service implements Runnable { + /** HTTP Response: blocked. */ + private static final String HTTP_BLOCK = "HTTP/1.1 500 blocked by AdBlock"; + /** HTTP Response: error. */ + private static final String HTTP_ERROR = "HTTP/1.1 500 error by AdBlock"; + /** HTTP Response: connected. */ + private static final String HTTP_CONNECTED = "HTTP/1.1 200 connected"; + /** HTTP Response: flush. */ + private static final String HTTP_RESPONSE = "\n\n"; + /** Proxy. */ private Thread proxy = null; /** Proxy's port. */ - private int port = 8080; + private int port = -1; + /** Proxy's filter. */ + private ArrayList filter = new ArrayList(); /** Stop proxy? */ private boolean stop = false; @@ -113,6 +127,24 @@ public class Proxy extends Service implements Runnable { } /** + * Check if URL is blocked. + * + * @param url + * URL + * @return if URL is blocked? + */ + private boolean checkURL(final String url) { + for (String f : Proxy.this.filter) { + if (url.indexOf(f) > 0) { + System.out.println("block: " + url); + System.out.println("match: " + f); + return true; + } + } + return false; + } + + /** * Run by Thread.start(). */ @Override @@ -122,68 +154,102 @@ public class Proxy extends Service implements Runnable { new InputStreamReader(this.local.getInputStream())); BufferedWriter localWriter = new BufferedWriter( new OutputStreamWriter(this.local.getOutputStream())); - BufferedReader remoteReader = null; - BufferedWriter remoteWriter = null; - StringBuilder buffer = new StringBuilder(); - String s; - while (this.remote == null) { - s = localReader.readLine(); - buffer.append(s + "\n"); - System.out.println(s); - if (s.startsWith("Host:")) { - // init remote socket - int targetPort = 80; - String targetHost = s.substring(6).trim(); - int i = targetHost.indexOf(':'); - if (i > 0) { - targetPort = Integer.parseInt(targetHost - .substring(i + 1)); - targetHost = targetHost.substring(0, i); + try { + BufferedReader remoteReader = null; + BufferedWriter remoteWriter = null; + StringBuilder buffer = new StringBuilder(); + String s; + boolean firstLine = true; + boolean block = false; + String checkHost = null; + while (this.remote == null && !block) { + s = localReader.readLine(); + buffer.append(s + "\n"); + System.out.println(s); + if (firstLine) { + String url = s.split(" ")[1]; + if (url.startsWith("http:")) { + block = this.checkURL(s); + } else { + checkHost = url; + } + firstLine = false; + } + if (!block && s.startsWith("Host:")) { + // init remote socket + int targetPort = 80; + String targetHost = s.substring(6).trim(); + int i = targetHost.indexOf(':'); + if (i > 0) { + targetPort = Integer.parseInt(targetHost + .substring(i + 1)); + targetHost = targetHost.substring(0, i); + } + if (checkHost != null) { + block = this.checkURL(targetHost + checkHost); + if (block) { + break; + } + } + + System.out.println("connect to " + targetHost + " " + + targetPort); + this.remote = new Socket(); + this.remote.connect(new InetSocketAddress( + targetHost, targetPort)); + remoteReader = new BufferedReader( + new InputStreamReader(this.remote + .getInputStream())); + remoteWriter = new BufferedWriter( + new OutputStreamWriter(this.remote + .getOutputStream())); + remoteWriter.append(buffer); + remoteWriter.flush(); + buffer = null; } - System.out.println("connect to " + targetHost + " " - + targetPort); - this.remote = new Socket(); - this.remote.connect(new InetSocketAddress(targetHost, - targetPort)); - remoteReader = new BufferedReader( - new InputStreamReader(this.remote - .getInputStream())); - remoteWriter = new BufferedWriter( - new OutputStreamWriter(this.remote - .getOutputStream())); - remoteWriter.append(buffer); - remoteWriter.flush(); - buffer = null; } - } - if (this.remote != null && this.remote.isConnected()) { - Object sync = new Object(); - - Thread t1 = new Thread(new CopyStream(remoteReader, - localWriter, sync)); - Thread t2 = new Thread(new CopyStream(localReader, - remoteWriter, sync)); - try { - synchronized (sync) { - t1.start(); - t2.start(); - sync.wait(); + if (this.remote != null && this.remote.isConnected()) { + Object sync = new Object(); + + Thread t1 = new Thread(new CopyStream(remoteReader, + localWriter, sync)); + Thread t2 = new Thread(new CopyStream(localReader, + remoteWriter, sync)); + try { + synchronized (sync) { + t1.start(); + t2.start(); + sync.wait(); + } + } catch (InterruptedException e) { + e.printStackTrace(); } - } catch (InterruptedException e) { - e.printStackTrace(); + t1.join(); + t2.join(); + this.local.close(); + } else if (block) { + while (localReader.ready()) { + localReader.readLine(); + } + localWriter.append(HTTP_BLOCK + HTTP_RESPONSE); + localWriter.flush(); + localWriter.close(); + this.local.close(); } - t1.join(); - t2.join(); + } catch (InterruptedException e) { + // do nothing + } catch (NullPointerException e) { + // do nothing + } catch (Exception e) { + e.printStackTrace(); + localWriter.append(HTTP_ERROR + " - " + e.toString() + + HTTP_RESPONSE); + localWriter.flush(); + localWriter.close(); this.local.close(); } - } catch (InterruptedException e) { - // do nothing - } catch (NullPointerException e) { - // do nothing - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(this.cont, e.toString(), Toast.LENGTH_LONG) - .show(); + } catch (IOException e1) { + // nothing } } } @@ -212,12 +278,34 @@ public class Proxy extends Service implements Runnable { @Override public final void onStart(final Intent intent, final int startId) { super.onStart(intent, startId); + + SharedPreferences preferences = PreferenceManager + .getDefaultSharedPreferences(this); + int p = Integer.parseInt(preferences.getString(AdBlock.PREFS_PORT, + "8080")); + boolean portChanged = p != this.port; + this.port = p; + + String f = preferences.getString(AdBlock.PREFS_FILTER, ""); + this.filter.clear(); + for (String s : f.split("\n")) { + if (s.length() > 0) { + this.filter.add(s); + } + } if (this.proxy == null) { - Toast.makeText(this, "starting proxy..", Toast.LENGTH_LONG).show(); + Toast.makeText(this, "starting proxy on port: " + this.port, + Toast.LENGTH_SHORT).show(); this.proxy = new Thread(this); this.proxy.start(); } else { - Toast.makeText(this, "proxy running", Toast.LENGTH_LONG).show(); + Toast.makeText(this, "proxy running on port " + this.port, + Toast.LENGTH_SHORT).show(); + if (portChanged) { + this.proxy.interrupt(); + this.proxy = new Thread(this); + this.proxy.start(); + } } } @@ -237,9 +325,13 @@ public class Proxy extends Service implements Runnable { @Override public final void run() { try { - ServerSocket sock = new ServerSocket(this.port); + int p = this.port; + ServerSocket sock = new ServerSocket(p); Socket client; - while (!this.stop) { + while (!this.stop && p == this.port) { + if (p != this.port) { + break; + } client = sock.accept(); if (client != null) { Thread t = new Thread(new Connection(client, this)); @@ -249,7 +341,6 @@ public class Proxy extends Service implements Runnable { sock.close(); } catch (IOException e) { e.printStackTrace(); - Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); } } } -- 2.11.4.GIT