Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: src/org/adblockplus/android/configurators/IptablesProxyConfigurator.java

Issue 4705284891082752: Proxy configurators (Closed)
Left Patch Set: Created Aug. 11, 2014, 12:36 p.m.
Right Patch Set: Last batch of review issues Created Aug. 24, 2014, 11:52 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 13 matching lines...) Expand all
24 24
25 import org.adblockplus.android.Utils; 25 import org.adblockplus.android.Utils;
26 26
27 import com.stericson.RootTools.RootTools; 27 import com.stericson.RootTools.RootTools;
28 28
29 import android.content.Context; 29 import android.content.Context;
30 import android.util.Log; 30 import android.util.Log;
31 31
32 /** 32 /**
33 * Proxy registration using RootTools and iptables. 33 * Proxy registration using RootTools and iptables.
34 *
35 * @author rjeschke
36 */ 34 */
37 public class IptablesProxyConfigurator implements ProxyConfigurator 35 public class IptablesProxyConfigurator implements ProxyConfigurator
38 { 36 {
39 private static final String TAG = Utils.getTag(IptablesProxyConfigurator.class ); 37 private static final String TAG = Utils.getTag(IptablesProxyConfigurator.class );
40 private static final int DEFAULT_TIMEOUT = 3000; 38 private static final int DEFAULT_TIMEOUT = 3000;
41 private static final String IPTABLES_RETURN = " -t nat -m owner --uid-owner {{ UID}} -A OUTPUT -p tcp -j RETURN\n"; 39 private static final String IPTABLES_RETURN = " -t nat -m owner --uid-owner {{ UID}} -A OUTPUT -p tcp -j RETURN\n";
42 private static final String IPTABLES_ADD_HTTP = " -t nat -A OUTPUT -p tcp --dp ort 80 -j REDIRECT --to {{PORT}}\n"; 40 private static final String IPTABLES_ADD_HTTP = " -t nat -A OUTPUT -p tcp --dp ort 80 -j REDIRECT --to {{PORT}}\n";
43 41
44 private final Context context; 42 private final Context context;
45 private String iptables = null; 43 private String iptables;
Felix Dahlke 2014/08/19 09:06:00 null is the default, isn't it?
René Jeschke 2014/08/19 10:41:33 Jup, of course :-D
44 private boolean isRegistered = false;
46 45
47 public IptablesProxyConfigurator(final Context context) 46 public IptablesProxyConfigurator(final Context context)
48 { 47 {
49 this.context = context; 48 this.context = context;
50 } 49 }
51 50
52 @Override 51 @Override
53 public boolean initialize() 52 public boolean initialize()
54 { 53 {
55 try 54 try
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 112
114 final StringBuilder cmd = new StringBuilder(); 113 final StringBuilder cmd = new StringBuilder();
115 cmd.append(this.iptables); 114 cmd.append(this.iptables);
116 cmd.append(IPTABLES_RETURN.replace("{{UID}}", String.valueOf(uid))); 115 cmd.append(IPTABLES_RETURN.replace("{{UID}}", String.valueOf(uid)));
117 cmd.append('\n'); 116 cmd.append('\n');
118 cmd.append(this.iptables); 117 cmd.append(this.iptables);
119 cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port))); 118 cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port)));
120 119
121 RootTools.sendShell(cmd.toString(), DEFAULT_TIMEOUT); 120 RootTools.sendShell(cmd.toString(), DEFAULT_TIMEOUT);
122 121
122 this.isRegistered = true;
123
123 return true; 124 return true;
124 } 125 }
125 catch (final Exception e) 126 catch (final Exception e)
126 { 127 {
127 // I leave this logging message for now, passing 'init' and failing 'regis ter' definitely is a failure 128 // I leave this logging message for now, passing 'init' and failing 'regis ter' definitely is a failure
128 Log.e(TAG, "Couldn't register proxy using iptables.", e); 129 Log.e(TAG, "Couldn't register proxy using iptables.", e);
129 return false; 130 return false;
130 } 131 }
131 } 132 }
132 133
133 @Override 134 @Override
134 public void unregisterProxy() 135 public void unregisterProxy()
135 { 136 {
136 try 137 try
137 { 138 {
138 // TODO: Maybe we should use a more selective unregister process instead o f
Felix Dahlke 2014/08/19 09:06:00 That sounds like something that should be an issue
René Jeschke 2014/08/19 10:41:33 Done.
139 // just dropping every OUTPUT NAT rule?
140 RootTools.sendShell(this.iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT); 139 RootTools.sendShell(this.iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT);
141 } 140 }
142 catch (final Exception e) 141 catch (final Exception e)
143 { 142 {
144 Log.w(TAG, "Failed to unregister proxy using iptables.", e); 143 Log.w(TAG, "Failed to unregister proxy using iptables.", e);
144 }
145 finally
146 {
147 this.isRegistered = false;
145 } 148 }
146 } 149 }
147 150
148 @Override 151 @Override
149 public void shutdown() 152 public void shutdown()
150 { 153 {
151 // Nothing to do here 154 // Nothing to do here
152 } 155 }
153 156
154 public static List<String> getIptablesOutput(final Context context) 157 public static List<String> getIptablesOutput(final Context context)
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 210
208 @Override 211 @Override
209 public ProxyRegistrationType getType() 212 public ProxyRegistrationType getType()
210 { 213 {
211 return ProxyRegistrationType.IPTABLES; 214 return ProxyRegistrationType.IPTABLES;
212 } 215 }
213 216
214 @Override 217 @Override
215 public boolean isRegistered() 218 public boolean isRegistered()
216 { 219 {
217 // TODO Auto-generated method stub 220 return this.isRegistered;
Felix Dahlke 2014/08/19 09:06:00 Better remove this auto generated comment, and the
René Jeschke 2014/08/19 10:41:33 Done, there has been something missing here *cough
221 }
222
223 @Override
224 public boolean isSticky()
225 {
218 return false; 226 return false;
219 } 227 }
220 228
221 @Override 229 @Override
222 public boolean isSticky()
223 {
224 // TODO Auto-generated method stub
225 return false;
226 }
227
228 @Override
229 public String toString() 230 public String toString()
230 { 231 {
231 return "[ProxyConfigurator: " + this.getType() + "]"; 232 return "[ProxyConfigurator: " + this.getType() + "]";
232 } 233 }
233 } 234 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld