http proxy working
[adBlock.git] / src / de / ub0r / android / adBlock / Proxy.java
blobe4927ebb4c2ff24dd0f22fff013fe87c5dbeb6a9
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));
124 try {
125 synchronized (sync) {
126 t1.start();
127 t2.start();
128 sync.wait();
130 } catch (InterruptedException e) {
131 e.printStackTrace();
133 System.out.println("join");
134 t1.join();
135 t2.join();
136 System.out.println("close local");
137 this.local.close();
139 } catch (InterruptedException e) {
140 // do nothing
141 } catch (NullPointerException e) {
142 // do nothing
143 } catch (Exception e) {
144 e.printStackTrace();
145 Toast.makeText(this.cont, e.toString(), Toast.LENGTH_LONG)
146 .show();
152 * (non-Javadoc)
154 * @see android.app.Service#onBind(android.content.Intent)
156 @Override
157 public IBinder onBind(final Intent intent) {
158 return null;
161 @Override
162 public void onStart(final Intent intent, final int startId) {
163 super.onStart(intent, startId);
164 if (this.proxy == null) {
165 Toast.makeText(this, "starting proxy..", Toast.LENGTH_LONG).show();
166 this.proxy = new Thread(this);
167 this.proxy.start();
168 } else {
169 Toast.makeText(this, "proxy running", Toast.LENGTH_LONG).show();
173 @Override
174 public void onDestroy() {
175 super.onDestroy();
176 Toast.makeText(this, "stopping proxy..", Toast.LENGTH_LONG).show();
177 this.stop = true;
180 @Override
181 public void run() {
182 try {
183 ServerSocket sock = new ServerSocket(this.port);
184 Socket client;
185 while (!this.stop) {
186 client = sock.accept();
187 if (client != null) {
188 Thread t = new Thread(new Connection(client, this));
189 t.start();
192 } catch (IOException e) {
193 e.printStackTrace();
194 Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();