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
{
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
,
53 s
= this.reader
.readLine();
57 this.writer
.append(s
+ "\n");
60 System
.out
.println("close remote");
61 synchronized (this.sync
) {
64 } catch (IOException e
) {
70 public Connection(final Socket socket
, final Context context
) {
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();
86 while (this.remote
== null) {
87 s
= localReader
.readLine();
88 buffer
.append(s
+ "\n");
89 System
.out
.println(s
);
90 if (s
.startsWith("Host:")) {
93 String targetHost
= s
.substring(6).trim();
94 int i
= targetHost
.indexOf(':');
96 targetPort
= Integer
.parseInt(targetHost
98 targetHost
= targetHost
.substring(0, i
);
100 System
.out
.println("connect to " + targetHost
+ " "
102 this.remote
= new Socket();
103 this.remote
.connect(new InetSocketAddress(targetHost
,
105 remoteReader
= new BufferedReader(
106 new InputStreamReader(this.remote
108 remoteWriter
= new BufferedWriter(
109 new OutputStreamWriter(this.remote
110 .getOutputStream()));
111 remoteWriter
.append(buffer
);
112 remoteWriter
.flush();
116 if (this.remote
!= null && this.remote
.isConnected()) {
117 Object sync
= new Object();
119 Thread t1
= new Thread(new CopyStream(remoteReader
,
121 Thread t2
= new Thread(new CopyStream(localReader
,
122 remoteWriter
, sync
));
124 synchronized (sync
) {
129 } catch (InterruptedException e
) {
136 } catch (InterruptedException e
) {
138 } catch (NullPointerException e
) {
140 } catch (Exception e
) {
142 Toast
.makeText(this.cont
, e
.toString(), Toast
.LENGTH_LONG
)
151 * @see android.app.Service#onBind(android.content.Intent)
154 public IBinder
onBind(final Intent intent
) {
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);
166 Toast
.makeText(this, "proxy running", Toast
.LENGTH_LONG
).show();
171 public void onDestroy() {
173 Toast
.makeText(this, "stopping proxy..", Toast
.LENGTH_LONG
).show();
180 ServerSocket sock
= new ServerSocket(this.port
);
183 client
= sock
.accept();
184 if (client
!= null) {
185 Thread t
= new Thread(new Connection(client
, this));
189 } catch (IOException e
) {
191 Toast
.makeText(this, e
.toString(), Toast
.LENGTH_LONG
).show();