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

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

Issue 5624117646589952: Issue 1771 - Update RootTools (Closed)
Left Patch Set: Created Nov. 6, 2014, 10:39 a.m.
Right Patch Set: Renamed some stuff Created Jan. 23, 2015, 1:38 p.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
« no previous file with change/comment | « libs/RootTools-3.4.jar ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2015 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 48
49 private final Context context; 49 private final Context context;
50 private String iptables; 50 private String iptables;
51 private boolean isRegistered = false; 51 private boolean isRegistered = false;
52 52
53 public IptablesProxyConfigurator(final Context context) 53 public IptablesProxyConfigurator(final Context context)
54 { 54 {
55 this.context = context; 55 this.context = context;
56 } 56 }
57 57
58 private static List<String> sendShell(final String command, final int timeout) throws IOException, TimeoutException, 58 private static List<String> runRootCommand(final String command, final int tim eout) throws IOException, TimeoutException,
59 RootDeniedException 59 RootDeniedException
60 { 60 {
61 final CommandOutput cmd = new CommandOutput(0, DEFAULT_TIMEOUT, command); 61 final CapturingOutputCommand cmd = new CapturingOutputCommand(0, DEFAULT_TIM EOUT, command);
62 62
63 Shell.runRootCommand(cmd); 63 Shell.runRootCommand(cmd);
64 64
65 cmd.waitForCompletion(); 65 cmd.waitForCompletion();
66 66
67 return cmd.output; 67 return cmd.output;
68 } 68 }
69 69
70 @Override 70 @Override
71 public boolean initialize() 71 public boolean initialize()
72 { 72 {
73 try 73 try
74 { 74 {
75 // If we don't set `handlerEnabled` to `false`, RootTools uses Handlers 75 // If we don't set `handlerEnabled` to `false`, RootTools uses Handlers
76 // which get executed on the UI thread which in fact renders it useless 76 // which get executed on the UI thread which in fact renders it useless
77 // for our purpose (as it either finished too late or blocks forever). 77 // for our purpose (as it either finished too late or blocks forever).
78 RootTools.handlerEnabled = false; 78 RootTools.handlerEnabled = false;
79 79
80 if (!RootTools.isAccessGiven()) 80 if (!RootTools.isAccessGiven())
81 { 81 {
82 throw new IllegalStateException("No root access"); 82 throw new IllegalStateException("No root access");
83 } 83 }
84 84
85 final File ipt = this.context.getFileStreamPath("iptables"); 85 final String path = getIptablesExecutablePath();
86 86
87 if (!ipt.exists()) 87 runRootCommand("chmod 700 " + path, DEFAULT_TIMEOUT);
88 {
89 throw new FileNotFoundException("No iptables executable");
90 }
91
92 final String path = ipt.getAbsolutePath();
93
94 sendShell("chmod 700 " + path, DEFAULT_TIMEOUT);
95 88
96 boolean compatible = false; 89 boolean compatible = false;
97 boolean version = false; 90 boolean version = false;
98 91
99 final String command = path + " --version\n" + path + " -L -t nat -n\n"; 92 final String command = path + " --version\n" + path + " -L -t nat -n\n";
100 93
101 final List<String> result = sendShell(command, DEFAULT_TIMEOUT); 94 final List<String> result = runRootCommand(command, DEFAULT_TIMEOUT);
102 95
103 for (final String line : result) 96 for (final String line : result)
104 { 97 {
105 if (line.contains("OUTPUT")) 98 if (line.contains("OUTPUT"))
106 { 99 {
107 compatible = true; 100 compatible = true;
108 } 101 }
109 if (line.contains("v1.4.")) 102 if (line.contains("v1.4."))
110 { 103 {
111 version = true; 104 version = true;
112 } 105 }
113 } 106 }
114 107
115 if (!(compatible && version)) 108 if (!(compatible && version))
116 { 109 {
117 throw new IllegalStateException("Incompatible iptables excutable"); 110 throw new IllegalStateException("Incompatible iptables excutable");
118 } 111 }
119 112
120 this.iptables = path; 113 this.iptables = path;
121 114
122 return true; 115 return true;
123 } 116 }
124 catch (final Exception e) 117 catch (final Exception e)
125 { 118 {
126 Log.e(TAG, "iptables setup failed", e);
127 return false; 119 return false;
128 } 120 }
121 }
122
123 private String getIptablesExecutablePath() throws FileNotFoundException
124 {
125 File iptablesExecutable = new File("/system/bin/iptables");
126 if (!iptablesExecutable.exists())
127 {
128 Log.i(TAG, "iptables not found on the system, using embedded binary");
129 iptablesExecutable = context.getFileStreamPath("iptables");
130 }
131 if (!iptablesExecutable.exists())
132 throw new FileNotFoundException("No iptables executable");
133 return iptablesExecutable.getAbsolutePath();
129 } 134 }
130 135
131 @Override 136 @Override
132 public boolean registerProxy(final InetAddress address, final int port) 137 public boolean registerProxy(final InetAddress address, final int port)
133 { 138 {
134 try 139 try
135 { 140 {
136 final int uid = this.context.getPackageManager().getPackageInfo(this.conte xt.getPackageName(), 0).applicationInfo.uid; 141 final int uid = this.context.getPackageManager().getPackageInfo(this.conte xt.getPackageName(), 0).applicationInfo.uid;
137 142
138 final StringBuilder cmd = new StringBuilder(); 143 final StringBuilder cmd = new StringBuilder();
139 cmd.append(this.iptables); 144 cmd.append(this.iptables);
140 cmd.append(IPTABLES_RETURN.replace("{{UID}}", String.valueOf(uid))); 145 cmd.append(IPTABLES_RETURN.replace("{{UID}}", String.valueOf(uid)));
141 cmd.append('\n'); 146 cmd.append('\n');
142 cmd.append(this.iptables); 147 cmd.append(this.iptables);
143 cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port))); 148 cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port)));
144 149
145 sendShell(cmd.toString(), DEFAULT_TIMEOUT); 150 runRootCommand(cmd.toString(), DEFAULT_TIMEOUT);
146 151
147 this.isRegistered = true; 152 this.isRegistered = true;
148 153
149 return true; 154 return true;
150 } 155 }
151 catch (final Exception e) 156 catch (final Exception e)
152 { 157 {
153 // I leave this logging message for now, passing 'init' and failing 'regis ter' definitely is a failure 158 // I leave this logging message for now, passing 'init' and failing 'regis ter' definitely is a failure
154 Log.e(TAG, "Couldn't register proxy using iptables.", e); 159 Log.e(TAG, "Couldn't register proxy using iptables.", e);
155 return false; 160 return false;
156 } 161 }
157 } 162 }
158 163
159 @Override 164 @Override
160 public void unregisterProxy() 165 public void unregisterProxy()
161 { 166 {
162 try 167 try
163 { 168 {
164 sendShell(this.iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT); 169 runRootCommand(this.iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT);
165 } 170 }
166 catch (final Exception e) 171 catch (final Exception e)
167 { 172 {
168 Log.w(TAG, "Failed to unregister proxy using iptables.", e); 173 Log.w(TAG, "Failed to unregister proxy using iptables.", e);
169 } 174 }
170 finally 175 finally
171 { 176 {
172 this.isRegistered = false; 177 this.isRegistered = false;
173 } 178 }
174 } 179 }
(...skipping 15 matching lines...) Expand all
190 195
191 final File ipt = context.getFileStreamPath("iptables"); 196 final File ipt = context.getFileStreamPath("iptables");
192 197
193 if (!ipt.exists()) 198 if (!ipt.exists())
194 { 199 {
195 throw new FileNotFoundException("No iptables executable"); 200 throw new FileNotFoundException("No iptables executable");
196 } 201 }
197 202
198 final String path = ipt.getAbsolutePath(); 203 final String path = ipt.getAbsolutePath();
199 204
200 sendShell("chmod 700 " + path, DEFAULT_TIMEOUT); 205 runRootCommand("chmod 700 " + path, DEFAULT_TIMEOUT);
201 206
202 boolean compatible = false; 207 boolean compatible = false;
203 boolean version = false; 208 boolean version = false;
204 209
205 String command = path + " --version\n" + path + " -L -t nat -n\n"; 210 String command = path + " --version\n" + path + " -L -t nat -n\n";
206 211
207 final List<String> result = sendShell(command, DEFAULT_TIMEOUT); 212 final List<String> result = runRootCommand(command, DEFAULT_TIMEOUT);
208 213
209 for (final String line : result) 214 for (final String line : result)
210 { 215 {
211 if (line.contains("OUTPUT")) 216 if (line.contains("OUTPUT"))
212 { 217 {
213 compatible = true; 218 compatible = true;
214 } 219 }
215 if (line.contains("v1.4.")) 220 if (line.contains("v1.4."))
216 { 221 {
217 version = true; 222 version = true;
218 } 223 }
219 } 224 }
220 225
221 if (!(compatible && version)) 226 if (!(compatible && version))
222 { 227 {
223 throw new IllegalStateException("Incompatible iptables excutable"); 228 throw new IllegalStateException("Incompatible iptables excutable");
224 } 229 }
225 230
226 command = path + " -L -t nat -n\n"; 231 command = path + " -L -t nat -n\n";
227 232
228 return sendShell(command, DEFAULT_TIMEOUT); 233 return runRootCommand(command, DEFAULT_TIMEOUT);
229 } 234 }
230 catch (final Throwable t) 235 catch (final Throwable t)
231 { 236 {
232 return null; 237 return null;
233 } 238 }
234 } 239 }
235 240
236 @Override 241 @Override
237 public ProxyRegistrationType getType() 242 public ProxyRegistrationType getType()
238 { 243 {
(...skipping 11 matching lines...) Expand all
250 { 255 {
251 return false; 256 return false;
252 } 257 }
253 258
254 @Override 259 @Override
255 public String toString() 260 public String toString()
256 { 261 {
257 return "[ProxyConfigurator: " + this.getType() + "]"; 262 return "[ProxyConfigurator: " + this.getType() + "]";
258 } 263 }
259 264
260 private final static class CommandOutput extends Command 265 private final static class CapturingOutputCommand extends Command
261 { 266 {
262 private final Semaphore completionNotify = new Semaphore(1); 267 private final Semaphore running = new Semaphore(1);
263 268
264 public List<String> output = new ArrayList<String>(); 269 public List<String> output = new ArrayList<String>();
265 270
266 public CommandOutput(final int id, final int timeout, final String command) 271 public CapturingOutputCommand(final int id, final int timeout, final String command)
267 { 272 {
268 super(id, timeout, command); 273 super(id, timeout, command);
269 274
270 this.completionNotify.acquireUninterruptibly(); 275 this.running.acquireUninterruptibly();
271 } 276 }
272 277
273 @Override 278 @Override
274 public void commandOutput(int id, String line) 279 public void commandOutput(int id, String line)
275 { 280 {
276 this.output.add(line); 281 this.output.add(line);
277 } 282 }
278 283
279 @Override 284 @Override
280 public void commandCompleted(int id, int exitCode) 285 public void commandCompleted(int id, int exitCode)
281 { 286 {
282 this.completionNotify.release(); 287 this.running.release();
283 } 288 }
284 289
285 @Override 290 @Override
286 public void commandTerminated(int id, String reason) 291 public void commandTerminated(int id, String reason)
287 { 292 {
288 this.completionNotify.release(); 293 this.running.release();
289 } 294 }
290 295
291 public void waitForCompletion() 296 public void waitForCompletion()
292 { 297 {
293 this.completionNotify.acquireUninterruptibly(); 298 this.running.acquireUninterruptibly();
294 } 299 }
295 } 300 }
296 } 301 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld