remove debugout
[adBlock.git] / src / de / ub0r / android / adBlock / Proxy.java
blob3e92b508ad54d7e7484d94e0db88f7e6952d5184
1 /**
2 *
3 */
4 package de.ub0r.android.adBlock;
6 import java.io.BufferedReader;
7 import java.io.BufferedWriter;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.io.OutputStreamWriter;
11 import java.net.InetSocketAddress;
12 import java.net.ServerSocket;
13 import java.net.Socket;
15 import android.app.Service;
16 import android.content.Context;
17 import android.content.Intent;
18 import android.os.IBinder;
19 import android.widget.Toast;
21 /**
22 * @author flx
24 public class Proxy extends Service implements Runnable {
26 private Thread proxy = null;
27 private int port = 8080;
28 private boolean stop = false;
30 private class Connection implements Runnable {
32 private final Socket local;
33 private Socket remote;
34 private final Context cont;
36 private class CopyStream implements Runnable {
37 private final BufferedReader reader;
38 private final BufferedWriter writer;
39 private final Object sync;
41 public CopyStream(final BufferedReader r, final BufferedWriter w,
42 final Object s) {
43 this.reader = r;
44 this.writer = w;
45 this.sync = s;
48 @Override
49 public void run() {
50 try {
51 String s;
52 do {
53 s = this.reader.readLine();
54 if (s == null) {
55 break;
57 this.writer.append(s + "\n");
58 this.writer.flush();
59 } while (true);
60 System.out.println("close remote");
61 synchronized (this.sync) {
62 this.sync.notify();
64 } catch (IOException e) {
65 // handle exception
70 public Connection(final Socket socket, final Context context) {
71 this.local = socket;
72 this.cont = context;
75 @Override
76 public void run() {
77 try {
78 BufferedReader localReader = new BufferedReader(
79 new InputStreamReader(this.local.getInputStream()));
80 BufferedWriter localWriter = new BufferedWriter(
81 new OutputStreamWriter(this.local.getOutputStream()));
82 BufferedReader remoteReader = null;
83 BufferedWriter remoteWriter = null;
84 StringBuilder buffer = new StringBuilder();
85 String s;
86 while (this.remote == null) {
87 s = localReader.readLine();
88 buffer.append(s + "\n");
89 System.out.println(s);
90 if (s.startsWith("Host:")) {
91 // init remote socket
92 int targetPort = 80;
93 String targetHost = s.substring(6).trim();
94 int i = targetHost.indexOf(':');
95 if (i > 0) {
96 targetPort = Integer.parseInt(targetHost
97 .substring(i + 1));
98 targetHost = targetHost.substring(0, i);
100 System.out.println("connect to " + targetHost + " "
101 + targetPort);
102 this.remote = new Socket();
103 this.remote.connect(new InetSocketAddress(targetHost,
104 targetPort));
105 remoteReader = new BufferedReader(
106 new InputStreamReader(this.remote
107 .getInputStream()));
108 remoteWriter = new BufferedWriter(
109 new OutputStreamWriter(this.remote
110 .getOutputStream()));
111 remoteWriter.append(buffer);
112 remoteWriter.flush();
113 buffer = null;
116 if (this.remote != null && this.remote.isConnected()) {
117 Object sync = new Object();
119 Thread t1 = new Thread(new CopyStream(remoteReader,
120 localWriter, sync));
121 Thread t2 = new Thread(new CopyStream(localReader,
122 remoteWriter, sync));
123 try {
124 synchronized (sync) {
125 t1.start();
126 t2.start();
127 sync.wait();
129 } catch (InterruptedException e) {
130 e.printStackTrace();
132 t1.join();
133 t2.join();
134 this.local.close();
136 } catch (InterruptedException e) {
137 // do nothing
138 } catch (NullPointerException e) {
139 // do nothing
140 } catch (Exception e) {
141 e.printStackTrace();
142 Toast.makeText(this.cont, e.toString(), Toast.LENGTH_LONG)
143 .show();
149 * (non-Javadoc)
151 * @see android.app.Service#onBind(android.content.Intent)
153 @Override
154 public IBinder onBind(final Intent intent) {
155 return null;
158 @Override
159 public void onStart(final Intent intent, final int startId) {
160 super.onStart(intent, startId);
161 if (this.proxy == null) {
162 Toast.makeText(this, "starting proxy..", Toast.LENGTH_LONG).show();
163 this.proxy = new Thread(this);
164 this.proxy.start();
165 } else {
166 Toast.makeText(this, "proxy running", Toast.LENGTH_LONG).show();
170 @Override
171 public void onDestroy() {
172 super.onDestroy();
173 Toast.makeText(this, "stopping proxy..", Toast.LENGTH_LONG).show();
174 this.stop = true;
177 @Override
178 public void run() {
179 try {
180 ServerSocket sock = new ServerSocket(this.port);
181 Socket client;
182 while (!this.stop) {
183 client = sock.accept();
184 if (client != null) {
185 Thread t = new Thread(new Connection(client, this));
186 t.start();
189 } catch (IOException e) {
190 e.printStackTrace();
191 Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();