| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 Loading... | |
| 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, |
|
Felix Dahlke
2014/11/19 10:38:39
I think "sendShell" is not a great name, I can see
René Jeschke
2014/11/24 13:07:57
Right, makes sense, will change this.
| |
| 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 return false; | 119 return false; |
| 127 } | 120 } |
| 128 } | 121 } |
| 129 | 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(); | |
| 134 } | |
| 135 | |
| 130 @Override | 136 @Override |
| 131 public boolean registerProxy(final InetAddress address, final int port) | 137 public boolean registerProxy(final InetAddress address, final int port) |
| 132 { | 138 { |
| 133 try | 139 try |
| 134 { | 140 { |
| 135 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; |
| 136 | 142 |
| 137 final StringBuilder cmd = new StringBuilder(); | 143 final StringBuilder cmd = new StringBuilder(); |
| 138 cmd.append(this.iptables); | 144 cmd.append(this.iptables); |
| 139 cmd.append(IPTABLES_RETURN.replace("{{UID}}", String.valueOf(uid))); | 145 cmd.append(IPTABLES_RETURN.replace("{{UID}}", String.valueOf(uid))); |
| 140 cmd.append('\n'); | 146 cmd.append('\n'); |
| 141 cmd.append(this.iptables); | 147 cmd.append(this.iptables); |
| 142 cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port))); | 148 cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port))); |
| 143 | 149 |
| 144 sendShell(cmd.toString(), DEFAULT_TIMEOUT); | 150 runRootCommand(cmd.toString(), DEFAULT_TIMEOUT); |
| 145 | 151 |
| 146 this.isRegistered = true; | 152 this.isRegistered = true; |
| 147 | 153 |
| 148 return true; | 154 return true; |
| 149 } | 155 } |
| 150 catch (final Exception e) | 156 catch (final Exception e) |
| 151 { | 157 { |
| 152 // 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 |
| 153 Log.e(TAG, "Couldn't register proxy using iptables.", e); | 159 Log.e(TAG, "Couldn't register proxy using iptables.", e); |
| 154 return false; | 160 return false; |
| 155 } | 161 } |
| 156 } | 162 } |
| 157 | 163 |
| 158 @Override | 164 @Override |
| 159 public void unregisterProxy() | 165 public void unregisterProxy() |
| 160 { | 166 { |
| 161 try | 167 try |
| 162 { | 168 { |
| 163 sendShell(this.iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT); | 169 runRootCommand(this.iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT); |
| 164 } | 170 } |
| 165 catch (final Exception e) | 171 catch (final Exception e) |
| 166 { | 172 { |
| 167 Log.w(TAG, "Failed to unregister proxy using iptables.", e); | 173 Log.w(TAG, "Failed to unregister proxy using iptables.", e); |
| 168 } | 174 } |
| 169 finally | 175 finally |
| 170 { | 176 { |
| 171 this.isRegistered = false; | 177 this.isRegistered = false; |
| 172 } | 178 } |
| 173 } | 179 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 189 | 195 |
| 190 final File ipt = context.getFileStreamPath("iptables"); | 196 final File ipt = context.getFileStreamPath("iptables"); |
| 191 | 197 |
| 192 if (!ipt.exists()) | 198 if (!ipt.exists()) |
| 193 { | 199 { |
| 194 throw new FileNotFoundException("No iptables executable"); | 200 throw new FileNotFoundException("No iptables executable"); |
| 195 } | 201 } |
| 196 | 202 |
| 197 final String path = ipt.getAbsolutePath(); | 203 final String path = ipt.getAbsolutePath(); |
| 198 | 204 |
| 199 sendShell("chmod 700 " + path, DEFAULT_TIMEOUT); | 205 runRootCommand("chmod 700 " + path, DEFAULT_TIMEOUT); |
| 200 | 206 |
| 201 boolean compatible = false; | 207 boolean compatible = false; |
| 202 boolean version = false; | 208 boolean version = false; |
| 203 | 209 |
| 204 String command = path + " --version\n" + path + " -L -t nat -n\n"; | 210 String command = path + " --version\n" + path + " -L -t nat -n\n"; |
| 205 | 211 |
| 206 final List<String> result = sendShell(command, DEFAULT_TIMEOUT); | 212 final List<String> result = runRootCommand(command, DEFAULT_TIMEOUT); |
| 207 | 213 |
| 208 for (final String line : result) | 214 for (final String line : result) |
| 209 { | 215 { |
| 210 if (line.contains("OUTPUT")) | 216 if (line.contains("OUTPUT")) |
| 211 { | 217 { |
| 212 compatible = true; | 218 compatible = true; |
| 213 } | 219 } |
| 214 if (line.contains("v1.4.")) | 220 if (line.contains("v1.4.")) |
| 215 { | 221 { |
| 216 version = true; | 222 version = true; |
| 217 } | 223 } |
| 218 } | 224 } |
| 219 | 225 |
| 220 if (!(compatible && version)) | 226 if (!(compatible && version)) |
| 221 { | 227 { |
| 222 throw new IllegalStateException("Incompatible iptables excutable"); | 228 throw new IllegalStateException("Incompatible iptables excutable"); |
| 223 } | 229 } |
| 224 | 230 |
| 225 command = path + " -L -t nat -n\n"; | 231 command = path + " -L -t nat -n\n"; |
| 226 | 232 |
| 227 return sendShell(command, DEFAULT_TIMEOUT); | 233 return runRootCommand(command, DEFAULT_TIMEOUT); |
| 228 } | 234 } |
| 229 catch (final Throwable t) | 235 catch (final Throwable t) |
| 230 { | 236 { |
| 231 return null; | 237 return null; |
| 232 } | 238 } |
| 233 } | 239 } |
| 234 | 240 |
| 235 @Override | 241 @Override |
| 236 public ProxyRegistrationType getType() | 242 public ProxyRegistrationType getType() |
| 237 { | 243 { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 249 { | 255 { |
| 250 return false; | 256 return false; |
| 251 } | 257 } |
| 252 | 258 |
| 253 @Override | 259 @Override |
| 254 public String toString() | 260 public String toString() |
| 255 { | 261 { |
| 256 return "[ProxyConfigurator: " + this.getType() + "]"; | 262 return "[ProxyConfigurator: " + this.getType() + "]"; |
| 257 } | 263 } |
| 258 | 264 |
| 259 private final static class CommandOutput extends Command | 265 private final static class CapturingOutputCommand extends Command |
|
Felix Dahlke
2014/11/19 10:38:39
Shouldn't this rather be "OutputCommand" (or anyth
René Jeschke
2014/11/24 13:07:57
Right, will also change this.
| |
| 260 { | 266 { |
| 261 private final Semaphore completionNotify = new Semaphore(1); | 267 private final Semaphore running = new Semaphore(1); |
|
Felix Dahlke
2014/11/19 10:38:39
This confused me a little bit, being a verb (bit u
René Jeschke
2014/11/24 13:07:57
"running" as name for a flag would mean sense, but
René Jeschke
2015/01/23 13:37:36
Ok, I changed this to running as I don't have a be
Felix Dahlke
2015/01/27 08:45:36
True, but I figure for a semaphore with one permit
| |
| 262 | 268 |
| 263 public List<String> output = new ArrayList<String>(); | 269 public List<String> output = new ArrayList<String>(); |
| 264 | 270 |
| 265 public CommandOutput(final int id, final int timeout, final String command) | 271 public CapturingOutputCommand(final int id, final int timeout, final String command) |
| 266 { | 272 { |
| 267 super(id, timeout, command); | 273 super(id, timeout, command); |
| 268 | 274 |
| 269 this.completionNotify.acquireUninterruptibly(); | 275 this.running.acquireUninterruptibly(); |
| 270 } | 276 } |
| 271 | 277 |
| 272 @Override | 278 @Override |
| 273 public void commandOutput(int id, String line) | 279 public void commandOutput(int id, String line) |
| 274 { | 280 { |
| 275 this.output.add(line); | 281 this.output.add(line); |
| 276 } | 282 } |
| 277 | 283 |
| 278 @Override | 284 @Override |
| 279 public void commandCompleted(int id, int exitCode) | 285 public void commandCompleted(int id, int exitCode) |
| 280 { | 286 { |
| 281 this.completionNotify.release(); | 287 this.running.release(); |
| 282 } | 288 } |
| 283 | 289 |
| 284 @Override | 290 @Override |
| 285 public void commandTerminated(int id, String reason) | 291 public void commandTerminated(int id, String reason) |
| 286 { | 292 { |
| 287 this.completionNotify.release(); | 293 this.running.release(); |
| 288 } | 294 } |
| 289 | 295 |
| 290 public void waitForCompletion() | 296 public void waitForCompletion() |
| 291 { | 297 { |
| 292 this.completionNotify.acquireUninterruptibly(); | 298 this.running.acquireUninterruptibly(); |
| 293 } | 299 } |
| 294 } | 300 } |
| 295 } | 301 } |
| LEFT | RIGHT |