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
;
24 public class Proxy
extends Service
implements Runnable
{
27 private Thread proxy
= null;
29 private int port
= 8080;
31 private boolean stop
= false;
34 * Class to handle a single proxy connection.
38 private class Connection
implements Runnable
{
41 private final Socket local
;
43 private Socket remote
;
44 /** Context in which the app is running. */
45 private final Context cont
;
48 * Class to Copy a Stream into an other Stream in a Thread.
52 private class CopyStream
implements Runnable
{
54 private final BufferedReader reader
;
56 private final BufferedWriter writer
;
57 /** Object to notify with at EOF. */
58 private final Object sync
;
70 public CopyStream(final BufferedReader r
, final BufferedWriter w
,
78 * Run by Thread.start().
85 s
= this.reader
.readLine();
89 this.writer
.append(s
+ "\n");
92 System
.out
.println("close remote");
93 synchronized (this.sync
) {
96 } catch (IOException e
) {
110 public Connection(final Socket socket
, final Context context
) {
116 * Run by Thread.start().
121 BufferedReader localReader
= new BufferedReader(
122 new InputStreamReader(this.local
.getInputStream()));
123 BufferedWriter localWriter
= new BufferedWriter(
124 new OutputStreamWriter(this.local
.getOutputStream()));
125 BufferedReader remoteReader
= null;
126 BufferedWriter remoteWriter
= null;
127 StringBuilder buffer
= new StringBuilder();
129 while (this.remote
== null) {
130 s
= localReader
.readLine();
131 buffer
.append(s
+ "\n");
132 System
.out
.println(s
);
133 if (s
.startsWith("Host:")) {
134 // init remote socket
136 String targetHost
= s
.substring(6).trim();
137 int i
= targetHost
.indexOf(':');
139 targetPort
= Integer
.parseInt(targetHost
141 targetHost
= targetHost
.substring(0, i
);
143 System
.out
.println("connect to " + targetHost
+ " "
145 this.remote
= new Socket();
146 this.remote
.connect(new InetSocketAddress(targetHost
,
148 remoteReader
= new BufferedReader(
149 new InputStreamReader(this.remote
151 remoteWriter
= new BufferedWriter(
152 new OutputStreamWriter(this.remote
153 .getOutputStream()));
154 remoteWriter
.append(buffer
);
155 remoteWriter
.flush();
159 if (this.remote
!= null && this.remote
.isConnected()) {
160 Object sync
= new Object();
162 Thread t1
= new Thread(new CopyStream(remoteReader
,
164 Thread t2
= new Thread(new CopyStream(localReader
,
165 remoteWriter
, sync
));
167 synchronized (sync
) {
172 } catch (InterruptedException e
) {
179 } catch (InterruptedException e
) {
181 } catch (NullPointerException e
) {
183 } catch (Exception e
) {
185 Toast
.makeText(this.cont
, e
.toString(), Toast
.LENGTH_LONG
)
192 * Default Implementation.
197 * @see android.app.Service#onBind(android.content.Intent)
200 public final IBinder
onBind(final Intent intent
) {
213 public final void onStart(final Intent intent
, final int startId
) {
214 super.onStart(intent
, startId
);
215 if (this.proxy
== null) {
216 Toast
.makeText(this, "starting proxy..", Toast
.LENGTH_LONG
).show();
217 this.proxy
= new Thread(this);
220 Toast
.makeText(this, "proxy running", Toast
.LENGTH_LONG
).show();
228 public final void onDestroy() {
230 Toast
.makeText(this, "stopping proxy..", Toast
.LENGTH_LONG
).show();
235 * Run by Thread.start().
238 public final void run() {
240 ServerSocket sock
= new ServerSocket(this.port
);
243 client
= sock
.accept();
244 if (client
!= null) {
245 Thread t
= new Thread(new Connection(client
, this));
249 } catch (IOException e
) {
251 Toast
.makeText(this, e
.toString(), Toast
.LENGTH_LONG
).show();