Whitespace-only code formatting changes.

This commit is contained in:
akwizgran
2015-11-30 09:38:25 +00:00
parent 1950c13ffb
commit 027ae8340f
202 changed files with 2993 additions and 2993 deletions

View File

@@ -115,13 +115,13 @@ class DroidtoothPlugin implements DuplexPlugin {
return BluetoothAdapter.getDefaultAdapter();
}
});
} catch(InterruptedException e) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Interrupted while getting BluetoothAdapter");
} catch(ExecutionException e) {
} catch (ExecutionException e) {
throw new IOException(e.toString());
}
if(adapter == null) {
if (adapter == null) {
LOG.info("Bluetooth is not supported");
return false;
}
@@ -133,11 +133,11 @@ class DroidtoothPlugin implements DuplexPlugin {
receiver = new BluetoothStateReceiver();
appContext.registerReceiver(receiver, filter);
// If Bluetooth is enabled, bind a socket - otherwise enable it
if(adapter.isEnabled()) {
if (adapter.isEnabled()) {
bind();
} else if(callback.getConfig().getBoolean("enable", true)) {
} else if (callback.getConfig().getBoolean("enable", true)) {
wasDisabled = true;
if(adapter.enable()) LOG.info("Enabling Bluetooth");
if (adapter.enable()) LOG.info("Enabling Bluetooth");
else LOG.info("Could not enable Bluetooth");
} else {
LOG.info("Not enabling Bluetooth");
@@ -148,8 +148,8 @@ class DroidtoothPlugin implements DuplexPlugin {
private void bind() {
ioExecutor.execute(new Runnable() {
public void run() {
if(!isRunning()) return;
if(LOG.isLoggable(INFO))
if (!isRunning()) return;
if (LOG.isLoggable(INFO))
LOG.info("Local address " + adapter.getAddress());
// Advertise the Bluetooth address to contacts
TransportProperties p = new TransportProperties();
@@ -159,13 +159,13 @@ class DroidtoothPlugin implements DuplexPlugin {
BluetoothServerSocket ss = null;
try {
ss = InsecureBluetooth.listen(adapter, "RFCOMM", getUuid());
} catch(IOException e) {
if(LOG.isLoggable(WARNING))
} catch (IOException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
tryToClose(ss);
return;
}
if(!isRunning()) {
if (!isRunning()) {
tryToClose(ss);
return;
}
@@ -179,7 +179,7 @@ class DroidtoothPlugin implements DuplexPlugin {
private UUID getUuid() {
String uuid = callback.getLocalProperties().get("uuid");
if(uuid == null) {
if (uuid == null) {
byte[] random = new byte[UUID_BYTES];
secureRandom.nextBytes(random);
uuid = UUID.nameUUIDFromBytes(random).toString();
@@ -192,23 +192,23 @@ class DroidtoothPlugin implements DuplexPlugin {
private void tryToClose(BluetoothServerSocket ss) {
try {
if(ss != null) ss.close();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (ss != null) ss.close();
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
private void acceptContactConnections() {
while(isRunning()) {
while (isRunning()) {
BluetoothSocket s;
try {
s = socket.accept();
} catch(IOException e) {
} catch (IOException e) {
// This is expected when the socket is closed
if(LOG.isLoggable(INFO)) LOG.info(e.toString());
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
return;
}
if(LOG.isLoggable(INFO)) {
if (LOG.isLoggable(INFO)) {
String address = s.getRemoteDevice().getAddress();
LOG.info("Connection from " + address);
}
@@ -222,11 +222,11 @@ class DroidtoothPlugin implements DuplexPlugin {
public void stop() {
running = false;
if(receiver != null) appContext.unregisterReceiver(receiver);
if (receiver != null) appContext.unregisterReceiver(receiver);
tryToClose(socket);
// Disable Bluetooth if we enabled it and it's still enabled
if(wasDisabled && adapter.isEnabled()) {
if(adapter.disable()) LOG.info("Disabling Bluetooth");
if (wasDisabled && adapter.isEnabled()) {
if (adapter.disable()) LOG.info("Disabling Bluetooth");
else LOG.info("Could not disable Bluetooth");
}
}
@@ -244,22 +244,22 @@ class DroidtoothPlugin implements DuplexPlugin {
}
public void poll(Collection<ContactId> connected) {
if(!isRunning()) return;
if (!isRunning()) return;
// Try to connect to known devices in parallel
Map<ContactId, TransportProperties> remote =
callback.getRemoteProperties();
for(Entry<ContactId, TransportProperties> e : remote.entrySet()) {
for (Entry<ContactId, TransportProperties> e : remote.entrySet()) {
final ContactId c = e.getKey();
if(connected.contains(c)) continue;
if (connected.contains(c)) continue;
final String address = e.getValue().get("address");
if(StringUtils.isNullOrEmpty(address)) continue;
if (StringUtils.isNullOrEmpty(address)) continue;
final String uuid = e.getValue().get("uuid");
if(StringUtils.isNullOrEmpty(uuid)) continue;
if (StringUtils.isNullOrEmpty(uuid)) continue;
ioExecutor.execute(new Runnable() {
public void run() {
if(!running) return;
if (!running) return;
BluetoothSocket s = connect(address, uuid);
if(s != null)
if (s != null)
callback.outgoingConnectionCreated(c, wrapSocket(s));
}
});
@@ -268,8 +268,8 @@ class DroidtoothPlugin implements DuplexPlugin {
private BluetoothSocket connect(String address, String uuid) {
// Validate the address
if(!BluetoothAdapter.checkBluetoothAddress(address)) {
if(LOG.isLoggable(WARNING))
if (!BluetoothAdapter.checkBluetoothAddress(address)) {
if (LOG.isLoggable(WARNING))
LOG.warning("Invalid address " + address);
return null;
}
@@ -277,8 +277,8 @@ class DroidtoothPlugin implements DuplexPlugin {
UUID u;
try {
u = UUID.fromString(uuid);
} catch(IllegalArgumentException e) {
if(LOG.isLoggable(WARNING)) LOG.warning("Invalid UUID " + uuid);
} catch (IllegalArgumentException e) {
if (LOG.isLoggable(WARNING)) LOG.warning("Invalid UUID " + uuid);
return null;
}
// Try to connect
@@ -286,12 +286,12 @@ class DroidtoothPlugin implements DuplexPlugin {
BluetoothSocket s = null;
try {
s = InsecureBluetooth.createSocket(d, u);
if(LOG.isLoggable(INFO)) LOG.info("Connecting to " + address);
if (LOG.isLoggable(INFO)) LOG.info("Connecting to " + address);
s.connect();
if(LOG.isLoggable(INFO)) LOG.info("Connected to " + address);
if (LOG.isLoggable(INFO)) LOG.info("Connected to " + address);
return s;
} catch(IOException e) {
if(LOG.isLoggable(INFO))
} catch (IOException e) {
if (LOG.isLoggable(INFO))
LOG.info("Failed to connect to " + address);
tryToClose(s);
return null;
@@ -300,22 +300,22 @@ class DroidtoothPlugin implements DuplexPlugin {
private void tryToClose(BluetoothSocket s) {
try {
if(s != null) s.close();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (s != null) s.close();
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
public DuplexTransportConnection createConnection(ContactId c) {
if(!isRunning()) return null;
if (!isRunning()) return null;
TransportProperties p = callback.getRemoteProperties().get(c);
if(p == null) return null;
if (p == null) return null;
String address = p.get("address");
if(StringUtils.isNullOrEmpty(address)) return null;
if (StringUtils.isNullOrEmpty(address)) return null;
String uuid = p.get("uuid");
if(StringUtils.isNullOrEmpty(uuid)) return null;
if (StringUtils.isNullOrEmpty(uuid)) return null;
BluetoothSocket s = connect(address, uuid);
if(s == null) return null;
if (s == null) return null;
return new DroidtoothTransportConnection(this, s);
}
@@ -325,17 +325,17 @@ class DroidtoothPlugin implements DuplexPlugin {
public DuplexTransportConnection createInvitationConnection(PseudoRandom r,
long timeout) {
if(!isRunning()) return null;
if (!isRunning()) return null;
// Use the invitation codes to generate the UUID
byte[] b = r.nextBytes(UUID_BYTES);
UUID uuid = UUID.nameUUIDFromBytes(b);
if(LOG.isLoggable(INFO)) LOG.info("Invitation UUID " + uuid);
if (LOG.isLoggable(INFO)) LOG.info("Invitation UUID " + uuid);
// Bind a server socket for receiving invitation connections
BluetoothServerSocket ss = null;
try {
ss = InsecureBluetooth.listen(adapter, "RFCOMM", uuid);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
tryToClose(ss);
return null;
}
@@ -347,8 +347,8 @@ class DroidtoothPlugin implements DuplexPlugin {
// Wait for an incoming or outgoing connection
try {
BluetoothSocket s = socketLatch.waitForReference(timeout);
if(s != null) return new DroidtoothTransportConnection(this, s);
} catch(InterruptedException e) {
if (s != null) return new DroidtoothTransportConnection(this, s);
} catch (InterruptedException e) {
LOG.warning("Interrupted while exchanging invitations");
Thread.currentThread().interrupt();
} finally {
@@ -363,19 +363,19 @@ class DroidtoothPlugin implements DuplexPlugin {
@Override
public void onReceive(Context ctx, Intent intent) {
int state = intent.getIntExtra(EXTRA_STATE, 0);
if(state == STATE_ON) {
if (state == STATE_ON) {
LOG.info("Bluetooth enabled");
bind();
} else if(state == STATE_OFF) {
} else if (state == STATE_OFF) {
LOG.info("Bluetooth disabled");
tryToClose(socket);
}
int scanMode = intent.getIntExtra(EXTRA_SCAN_MODE, 0);
if(scanMode == SCAN_MODE_NONE) {
if (scanMode == SCAN_MODE_NONE) {
LOG.info("Scan mode: None");
} else if(scanMode == SCAN_MODE_CONNECTABLE) {
} else if (scanMode == SCAN_MODE_CONNECTABLE) {
LOG.info("Scan mode: Connectable");
} else if(scanMode == SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
} else if (scanMode == SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
LOG.info("Scan mode: Discoverable");
}
}
@@ -397,29 +397,29 @@ class DroidtoothPlugin implements DuplexPlugin {
@Override
public void run() {
long end = clock.currentTimeMillis() + timeout;
while(!finished(end)) {
while (!finished(end)) {
// Discover nearby devices
LOG.info("Discovering nearby devices");
List<String> addresses;
try {
long now = clock.currentTimeMillis();
addresses = discoverDevices(end - now);
} catch(InterruptedException e) {
} catch (InterruptedException e) {
LOG.warning("Interrupted while discovering devices");
Thread.currentThread().interrupt();
return;
}
if(addresses.isEmpty()) {
if (addresses.isEmpty()) {
LOG.info("No devices discovered");
continue;
}
// Connect to any device with the right UUID
for(String address : addresses) {
if(finished(end)) return;
for (String address : addresses) {
if (finished(end)) return;
BluetoothSocket s = connect(address, uuid);
if(s != null) {
if (s != null) {
LOG.info("Outgoing connection");
if(!socketLatch.set(s)) {
if (!socketLatch.set(s)) {
LOG.info("Closing redundant connection");
tryToClose(s);
}
@@ -455,13 +455,13 @@ class DroidtoothPlugin implements DuplexPlugin {
@Override
public void onReceive(Context ctx, Intent intent) {
String action = intent.getAction();
if(action.equals(DISCOVERY_FINISHED)) {
if (action.equals(DISCOVERY_FINISHED)) {
LOG.info("Discovery finished");
ctx.unregisterReceiver(this);
finished.countDown();
} else if(action.equals(FOUND)) {
} else if (action.equals(FOUND)) {
BluetoothDevice d = intent.getParcelableExtra(EXTRA_DEVICE);
if(LOG.isLoggable(INFO))
if (LOG.isLoggable(INFO))
LOG.info("Discovered device: " + d.getAddress());
addresses.add(d.getAddress());
}
@@ -491,13 +491,13 @@ class DroidtoothPlugin implements DuplexPlugin {
try {
BluetoothSocket s = serverSocket.accept();
LOG.info("Incoming connection");
if(!socketLatch.set(s)) {
if (!socketLatch.set(s)) {
LOG.info("Closing redundant connection");
s.close();
}
} catch(IOException e) {
} catch (IOException e) {
// This is expected when the socket is closed
if(LOG.isLoggable(INFO)) LOG.info(e.toString());
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
}
}
}

View File

@@ -49,8 +49,8 @@ class DroidtoothTransportConnection implements DuplexTransportConnection {
public void dispose(boolean exception, boolean recognised)
throws IOException {
if(halfClosed.getAndSet(true) || exception)
if(!closed.getAndSet(true)) socket.close();
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) socket.close();
}
}
@@ -73,8 +73,8 @@ class DroidtoothTransportConnection implements DuplexTransportConnection {
}
public void dispose(boolean exception) throws IOException {
if(halfClosed.getAndSet(true) || exception)
if(!closed.getAndSet(true)) socket.close();
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) socket.close();
}
}
}

View File

@@ -30,7 +30,7 @@ class InsecureBluetooth {
@SuppressLint("NewApi")
static BluetoothServerSocket listen(BluetoothAdapter adapter, String name,
UUID uuid) throws IOException {
if(Build.VERSION.SDK_INT >= 10) {
if (Build.VERSION.SDK_INT >= 10) {
LOG.info("Listening with new API");
return adapter.listenUsingInsecureRfcommWithServiceRecord(name,
uuid);
@@ -42,13 +42,13 @@ class InsecureBluetooth {
+ ".RfcommChannelPicker";
Class<?> channelPickerClass = null;
Class<?>[] children = BluetoothAdapter.class.getDeclaredClasses();
for(Class<?> c : children) {
if(c.getCanonicalName().equals(className)) {
for (Class<?> c : children) {
if (c.getCanonicalName().equals(className)) {
channelPickerClass = c;
break;
}
}
if(channelPickerClass == null)
if (channelPickerClass == null)
throw new IOException("Can't find channel picker class");
Constructor<?> constructor =
channelPickerClass.getDeclaredConstructor(UUID.class);
@@ -58,7 +58,7 @@ class InsecureBluetooth {
channelPickerClass.getDeclaredMethod("nextChannel");
nextChannel.setAccessible(true);
int channel = (Integer) nextChannel.invoke(channelPicker);
if(channel == -1) throw new IOException("No available channels");
if (channel == -1) throw new IOException("No available channels");
// Listen on the channel
BluetoothServerSocket socket = listen(channel);
// Add a service record
@@ -72,7 +72,7 @@ class InsecureBluetooth {
addRfcommServiceRecord.setAccessible(true);
int handle = (Integer) addRfcommServiceRecord.invoke(mService, name,
new ParcelUuid(uuid), channel, new Binder());
if(handle == -1) {
if (handle == -1) {
socket.close();
throw new IOException("Can't register SDP record for " + name);
}
@@ -84,16 +84,16 @@ class InsecureBluetooth {
setCloseHandler.setAccessible(true);
setCloseHandler.invoke(socket, mHandler, handle);
return socket;
} catch(NoSuchMethodException e) {
} catch (NoSuchMethodException e) {
throw new IOException(e.toString());
} catch(NoSuchFieldException e) {
} catch (NoSuchFieldException e) {
throw new IOException(e.toString());
} catch(IllegalAccessException e) {
} catch (IllegalAccessException e) {
throw new IOException(e.toString());
} catch(InstantiationException e) {
} catch (InstantiationException e) {
throw new IOException(e.toString());
} catch(InvocationTargetException e) {
if(e.getCause() instanceof IOException) {
} catch (InvocationTargetException e) {
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
throw new IOException(e.toString());
@@ -116,21 +116,21 @@ class InsecureBluetooth {
mSocket.getClass().getDeclaredMethod("bindListen");
bindListen.setAccessible(true);
int errno = (Integer) bindListen.invoke(mSocket);
if(errno != 0) {
if (errno != 0) {
socket.close();
throw new IOException("Can't bind: errno " + errno);
}
return socket;
} catch(NoSuchMethodException e) {
} catch (NoSuchMethodException e) {
throw new IOException(e.toString());
} catch(NoSuchFieldException e) {
} catch (NoSuchFieldException e) {
throw new IOException(e.toString());
} catch(IllegalAccessException e) {
} catch (IllegalAccessException e) {
throw new IOException(e.toString());
} catch(InstantiationException e) {
} catch (InstantiationException e) {
throw new IOException(e.toString());
} catch(InvocationTargetException e) {
if(e.getCause() instanceof IOException) {
} catch (InvocationTargetException e) {
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
throw new IOException(e.toString());
@@ -141,7 +141,7 @@ class InsecureBluetooth {
@SuppressLint("NewApi")
static BluetoothSocket createSocket(BluetoothDevice device, UUID uuid)
throws IOException {
if(Build.VERSION.SDK_INT >= 10) {
if (Build.VERSION.SDK_INT >= 10) {
LOG.info("Creating socket with new API");
return device.createInsecureRfcommSocketToServiceRecord(uuid);
}
@@ -154,14 +154,14 @@ class InsecureBluetooth {
constructor.setAccessible(true);
return constructor.newInstance(TYPE_RFCOMM, -1, false, true, device,
-1, new ParcelUuid(uuid));
} catch(NoSuchMethodException e) {
} catch (NoSuchMethodException e) {
throw new IOException(e.toString());
} catch(IllegalAccessException e) {
} catch (IllegalAccessException e) {
throw new IOException(e.toString());
} catch(InstantiationException e) {
} catch (InstantiationException e) {
throw new IOException(e.toString());
} catch(InvocationTargetException e) {
if(e.getCause() instanceof IOException) {
} catch (InvocationTargetException e) {
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
throw new IOException(e.toString());

View File

@@ -45,7 +45,7 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
@Override
public void stop() {
running = false;
if(networkStateReceiver != null)
if (networkStateReceiver != null)
appContext.unregisterReceiver(networkStateReceiver);
tryToClose(socket);
}
@@ -54,13 +54,13 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
@Override
public void onReceive(Context ctx, Intent i) {
if(!running) return;
if (!running) return;
Object o = ctx.getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) o;
NetworkInfo net = cm.getActiveNetworkInfo();
if(net != null && net.getType() == TYPE_WIFI && net.isConnected()) {
if (net != null && net.getType() == TYPE_WIFI && net.isConnected()) {
LOG.info("Connected to Wi-Fi");
if(socket == null || socket.isClosed()) bind();
if (socket == null || socket.isClosed()) bind();
} else {
LOG.info("Not connected to Wi-Fi");
tryToClose(socket);

View File

@@ -100,7 +100,7 @@ class TorPlugin implements DuplexPlugin, EventHandler {
this.maxLatency = maxLatency;
this.maxIdleTime = maxIdleTime;
this.pollingInterval = pollingInterval;
if(maxIdleTime > Integer.MAX_VALUE / 2)
if (maxIdleTime > Integer.MAX_VALUE / 2)
socketTimeout = Integer.MAX_VALUE;
else socketTimeout = maxIdleTime * 2;
torDirectory = appContext.getDir("tor", MODE_PRIVATE);
@@ -131,16 +131,16 @@ class TorPlugin implements DuplexPlugin, EventHandler {
try {
controlSocket = new Socket("127.0.0.1", CONTROL_PORT);
LOG.info("Tor is already running");
} catch(IOException e) {
} catch (IOException e) {
LOG.info("Tor is not running");
startProcess = true;
// Install the binary, possibly overwriting an older version
if(!installBinary()) {
if (!installBinary()) {
LOG.warning("Could not install Tor binary");
return false;
}
// Install the GeoIP database and config file if necessary
if(!isConfigInstalled() && !installConfig()) {
if (!isConfigInstalled() && !installConfig()) {
LOG.info("Could not install Tor config");
return false;
}
@@ -160,31 +160,31 @@ class TorPlugin implements DuplexPlugin, EventHandler {
Process torProcess;
try {
torProcess = Runtime.getRuntime().exec(cmd, env, torDirectory);
} catch(SecurityException e1) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e1.toString(), e1);
} catch (SecurityException e1) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e1.toString(), e1);
return false;
}
// Log the process's standard output until it detaches
if(LOG.isLoggable(INFO)) {
if (LOG.isLoggable(INFO)) {
Scanner stdout = new Scanner(torProcess.getInputStream());
while(stdout.hasNextLine()) LOG.info(stdout.nextLine());
while (stdout.hasNextLine()) LOG.info(stdout.nextLine());
stdout.close();
}
try {
// Wait for the process to detach or exit
int exit = torProcess.waitFor();
if(exit != 0) {
if(LOG.isLoggable(WARNING))
if (exit != 0) {
if (LOG.isLoggable(WARNING))
LOG.warning("Tor exited with value " + exit);
return false;
}
// Wait for the auth cookie file to be created/updated
if(!latch.await(COOKIE_TIMEOUT, MILLISECONDS)) {
if (!latch.await(COOKIE_TIMEOUT, MILLISECONDS)) {
LOG.warning("Auth cookie not created");
if(LOG.isLoggable(INFO)) listFiles(torDirectory);
if (LOG.isLoggable(INFO)) listFiles(torDirectory);
return false;
}
} catch(InterruptedException e1) {
} catch (InterruptedException e1) {
LOG.warning("Interrupted while starting Tor");
Thread.currentThread().interrupt();
return false;
@@ -203,9 +203,9 @@ class TorPlugin implements DuplexPlugin, EventHandler {
controlConnection.setEventHandler(this);
controlConnection.setEvents(Arrays.asList(EVENTS));
// If Tor was already running, find out whether it's bootstrapped
if(!startProcess) {
if (!startProcess) {
String phase = controlConnection.getInfo("status/bootstrap-phase");
if(phase != null && phase.contains("PROGRESS=100")) {
if (phase != null && phase.contains("PROGRESS=100")) {
LOG.info("Tor has already bootstrapped");
bootstrapped = true;
}
@@ -228,13 +228,13 @@ class TorPlugin implements DuplexPlugin, EventHandler {
out = new FileOutputStream(torFile);
copy(in, out);
// Make the Tor binary executable
if(!setExecutable(torFile)) {
if (!setExecutable(torFile)) {
LOG.warning("Could not make Tor binary executable");
return false;
}
return true;
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
tryToClose(in);
tryToClose(out);
return false;
@@ -260,8 +260,8 @@ class TorPlugin implements DuplexPlugin, EventHandler {
// Create a file to indicate that installation succeeded
doneFile.createNewFile();
return true;
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
tryToClose(in);
tryToClose(out);
return false;
@@ -269,12 +269,12 @@ class TorPlugin implements DuplexPlugin, EventHandler {
}
private InputStream getTorInputStream() throws IOException {
if(LOG.isLoggable(INFO))
if (LOG.isLoggable(INFO))
LOG.info("Installing Tor binary for " + architecture);
String filename = "tor-" + architecture + ".zip";
InputStream in = appContext.getResources().getAssets().open(filename);
ZipInputStream zin = new ZipInputStream(in);
if(zin.getNextEntry() == null) throw new IOException();
if (zin.getNextEntry() == null) throw new IOException();
return zin;
}
@@ -282,7 +282,7 @@ class TorPlugin implements DuplexPlugin, EventHandler {
String filename = "geoip.zip";
InputStream in = appContext.getResources().getAssets().open(filename);
ZipInputStream zin = new ZipInputStream(in);
if(zin.getNextEntry() == null) throw new IOException();
if (zin.getNextEntry() == null) throw new IOException();
return zin;
}
@@ -292,9 +292,9 @@ class TorPlugin implements DuplexPlugin, EventHandler {
private void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[4096];
while(true) {
while (true) {
int read = in.read(buf);
if(read == -1) break;
if (read == -1) break;
out.write(buf, 0, read);
}
in.close();
@@ -303,19 +303,19 @@ class TorPlugin implements DuplexPlugin, EventHandler {
@SuppressLint("NewApi")
private boolean setExecutable(File f) {
if(Build.VERSION.SDK_INT >= 9) {
if (Build.VERSION.SDK_INT >= 9) {
return f.setExecutable(true, true);
} else {
String[] command = { "chmod", "700", f.getAbsolutePath() };
try {
return Runtime.getRuntime().exec(command).waitFor() == 0;
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (InterruptedException e) {
LOG.warning("Interrupted while executing chmod");
Thread.currentThread().interrupt();
} catch(SecurityException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (SecurityException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
return false;
}
@@ -323,22 +323,22 @@ class TorPlugin implements DuplexPlugin, EventHandler {
private void tryToClose(InputStream in) {
try {
if(in != null) in.close();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (in != null) in.close();
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
private void tryToClose(OutputStream out) {
try {
if(out != null) out.close();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (out != null) out.close();
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
private void listFiles(File f) {
if(f.isDirectory()) for(File child : f.listFiles()) listFiles(child);
if (f.isDirectory()) for (File child : f.listFiles()) listFiles(child);
else LOG.info(f.getAbsolutePath());
}
@@ -347,9 +347,9 @@ class TorPlugin implements DuplexPlugin, EventHandler {
FileInputStream in = new FileInputStream(f);
try {
int offset = 0;
while(offset < b.length) {
while (offset < b.length) {
int read = in.read(b, offset, b.length - offset);
if(read == -1) throw new EOFException();
if (read == -1) throw new EOFException();
offset += read;
}
return b;
@@ -364,19 +364,19 @@ class TorPlugin implements DuplexPlugin, EventHandler {
// If there's already a port number stored in config, reuse it
String portString = callback.getConfig().get("port");
int port;
if(StringUtils.isNullOrEmpty(portString)) port = 0;
if (StringUtils.isNullOrEmpty(portString)) port = 0;
else port = Integer.parseInt(portString);
// Bind a server socket to receive connections from Tor
ServerSocket ss = null;
try {
ss = new ServerSocket();
ss.bind(new InetSocketAddress("127.0.0.1", port));
} catch(IOException e) {
if(LOG.isLoggable(WARNING))
} catch (IOException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
tryToClose(ss);
}
if(!running) {
if (!running) {
tryToClose(ss);
return;
}
@@ -400,15 +400,15 @@ class TorPlugin implements DuplexPlugin, EventHandler {
private void tryToClose(ServerSocket ss) {
try {
if(ss != null) ss.close();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (ss != null) ss.close();
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
private void publishHiddenService(String port) {
if(!running) return;
if(!hostnameFile.exists()) {
if (!running) return;
if (!hostnameFile.exists()) {
LOG.info("Creating hidden service");
try {
// Watch for the hostname file being created/updated
@@ -424,15 +424,15 @@ class TorPlugin implements DuplexPlugin, EventHandler {
controlConnection.setConf(config);
controlConnection.saveConf();
// Wait for the hostname file to be created/updated
if(!latch.await(HOSTNAME_TIMEOUT, MILLISECONDS)) {
if (!latch.await(HOSTNAME_TIMEOUT, MILLISECONDS)) {
LOG.warning("Hidden service not created");
if(LOG.isLoggable(INFO)) listFiles(torDirectory);
if (LOG.isLoggable(INFO)) listFiles(torDirectory);
return;
}
if(!running) return;
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if (!running) return;
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (InterruptedException e) {
LOG.warning("Interrupted while creating hidden service");
Thread.currentThread().interrupt();
return;
@@ -441,24 +441,24 @@ class TorPlugin implements DuplexPlugin, EventHandler {
// Publish the hidden service's onion hostname in transport properties
try {
String hostname = new String(read(hostnameFile), "UTF-8").trim();
if(LOG.isLoggable(INFO)) LOG.info("Hidden service " + hostname);
if (LOG.isLoggable(INFO)) LOG.info("Hidden service " + hostname);
TransportProperties p = new TransportProperties();
p.put("onion", hostname);
callback.mergeLocalProperties(p);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
private void acceptContactConnections(ServerSocket ss) {
while(running) {
while (running) {
Socket s;
try {
s = ss.accept();
s.setSoTimeout(socketTimeout);
} catch(IOException e) {
} catch (IOException e) {
// This is expected when the socket is closed
if(LOG.isLoggable(INFO)) LOG.info(e.toString());
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
return;
}
LOG.info("Connection received");
@@ -468,9 +468,9 @@ class TorPlugin implements DuplexPlugin, EventHandler {
}
private void enableNetwork(boolean enable) throws IOException {
if(!running) return;
if(LOG.isLoggable(INFO)) LOG.info("Enabling network: " + enable);
if(!enable) circuitBuilt.set(false);
if (!running) return;
if (LOG.isLoggable(INFO)) LOG.info("Enabling network: " + enable);
if (!enable) circuitBuilt.set(false);
networkEnabled = enable;
controlConnection.setConf("DisableNetwork", enable ? "0" : "1");
}
@@ -478,21 +478,21 @@ class TorPlugin implements DuplexPlugin, EventHandler {
public void stop() throws IOException {
running = false;
tryToClose(socket);
if(networkStateReceiver != null)
if (networkStateReceiver != null)
appContext.unregisterReceiver(networkStateReceiver);
try {
LOG.info("Stopping Tor");
if(controlSocket == null)
if (controlSocket == null)
controlSocket = new Socket("127.0.0.1", CONTROL_PORT);
if(controlConnection == null) {
if (controlConnection == null) {
controlConnection = new TorControlConnection(controlSocket);
controlConnection.authenticate(read(cookieFile));
}
controlConnection.setConf("DisableNetwork", "1");
controlConnection.shutdownTor("TERM");
controlSocket.close();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
@@ -509,41 +509,41 @@ class TorPlugin implements DuplexPlugin, EventHandler {
}
public void poll(Collection<ContactId> connected) {
if(!isRunning()) return;
for(ContactId c : callback.getRemoteProperties().keySet())
if(!connected.contains(c)) connectAndCallBack(c);
if (!isRunning()) return;
for (ContactId c : callback.getRemoteProperties().keySet())
if (!connected.contains(c)) connectAndCallBack(c);
}
private void connectAndCallBack(final ContactId c) {
ioExecutor.execute(new Runnable() {
public void run() {
DuplexTransportConnection d = createConnection(c);
if(d != null) callback.outgoingConnectionCreated(c, d);
if (d != null) callback.outgoingConnectionCreated(c, d);
}
});
}
public DuplexTransportConnection createConnection(ContactId c) {
if(!isRunning()) return null;
if (!isRunning()) return null;
TransportProperties p = callback.getRemoteProperties().get(c);
if(p == null) return null;
if (p == null) return null;
String onion = p.get("onion");
if(StringUtils.isNullOrEmpty(onion)) return null;
if(!ONION.matcher(onion).matches()) {
if(LOG.isLoggable(INFO)) LOG.info("Invalid hostname: " + onion);
if (StringUtils.isNullOrEmpty(onion)) return null;
if (!ONION.matcher(onion).matches()) {
if (LOG.isLoggable(INFO)) LOG.info("Invalid hostname: " + onion);
return null;
}
try {
if(LOG.isLoggable(INFO)) LOG.info("Connecting to " + onion);
if (LOG.isLoggable(INFO)) LOG.info("Connecting to " + onion);
controlConnection.forgetHiddenService(onion.substring(0, 16));
Socks5Proxy proxy = new Socks5Proxy("127.0.0.1", SOCKS_PORT);
proxy.resolveAddrLocally(false);
Socket s = new SocksSocket(proxy, onion, 80);
s.setSoTimeout(socketTimeout);
if(LOG.isLoggable(INFO)) LOG.info("Connected to " + onion);
if (LOG.isLoggable(INFO)) LOG.info("Connected to " + onion);
return new TorTransportConnection(this, s);
} catch(IOException e) {
if(LOG.isLoggable(INFO)) LOG.info("Could not connect to " + onion);
} catch (IOException e) {
if (LOG.isLoggable(INFO)) LOG.info("Could not connect to " + onion);
return null;
}
}
@@ -558,16 +558,16 @@ class TorPlugin implements DuplexPlugin, EventHandler {
}
public void circuitStatus(String status, String id, String path) {
if(status.equals("BUILT") && !circuitBuilt.getAndSet(true)) {
if (status.equals("BUILT") && !circuitBuilt.getAndSet(true)) {
LOG.info("First circuit built");
if(isRunning()) callback.pollNow();
if (isRunning()) callback.pollNow();
}
}
public void streamStatus(String status, String id, String target) {}
public void orConnStatus(String status, String orName) {
if(LOG.isLoggable(INFO)) LOG.info("OR connection " + status);
if (LOG.isLoggable(INFO)) LOG.info("OR connection " + status);
}
public void bandwidthUsed(long read, long written) {}
@@ -575,10 +575,10 @@ class TorPlugin implements DuplexPlugin, EventHandler {
public void newDescriptors(List<String> orList) {}
public void message(String severity, String msg) {
if(LOG.isLoggable(INFO)) LOG.info(severity + " " + msg);
if(severity.equals("NOTICE") && msg.startsWith("Bootstrapped 100%")) {
if (LOG.isLoggable(INFO)) LOG.info(severity + " " + msg);
if (severity.equals("NOTICE") && msg.startsWith("Bootstrapped 100%")) {
bootstrapped = true;
if(isRunning()) callback.pollNow();
if (isRunning()) callback.pollNow();
}
}
@@ -604,26 +604,26 @@ class TorPlugin implements DuplexPlugin, EventHandler {
@Override
public void onReceive(Context ctx, Intent i) {
if(!running) return;
if (!running) return;
boolean online = !i.getBooleanExtra(EXTRA_NO_CONNECTIVITY, false);
if(online) {
if (online) {
// Some devices fail to set EXTRA_NO_CONNECTIVITY, double check
Object o = ctx.getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) o;
NetworkInfo net = cm.getActiveNetworkInfo();
if(net == null || !net.isConnected()) online = false;
if (net == null || !net.isConnected()) online = false;
}
String country = locationUtils.getCurrentCountry();
if(LOG.isLoggable(INFO)) {
if (LOG.isLoggable(INFO)) {
LOG.info("Online: " + online);
if("".equals(country)) LOG.info("Country code unknown");
if ("".equals(country)) LOG.info("Country code unknown");
else LOG.info("Country code: " + country);
}
boolean blocked = TorNetworkMetadata.isTorProbablyBlocked(country);
try {
enableNetwork(online && !blocked);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
}

View File

@@ -40,21 +40,21 @@ public class TorPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
// Check that we have a Tor binary for this architecture
String architecture = null;
for(String abi : AndroidUtils.getSupportedArchitectures()) {
if(abi.startsWith("x86")) {
for (String abi : AndroidUtils.getSupportedArchitectures()) {
if (abi.startsWith("x86")) {
architecture = "x86";
break;
} else if(abi.startsWith("armeabi")) {
} else if (abi.startsWith("armeabi")) {
architecture = "arm";
break;
}
}
if(architecture == null) {
if (architecture == null) {
LOG.info("Tor is not supported on this architecture");
return null;
}
// Use position-independent executable for SDK >= 16
if(Build.VERSION.SDK_INT >= 16) architecture += "-pie";
if (Build.VERSION.SDK_INT >= 16) architecture += "-pie";
return new TorPlugin(ioExecutor,appContext, locationUtils, callback,
architecture, MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
}

View File

@@ -48,8 +48,8 @@ class TorTransportConnection implements DuplexTransportConnection {
public void dispose(boolean exception, boolean recognised)
throws IOException {
if(halfClosed.getAndSet(true) || exception)
if(!closed.getAndSet(true)) socket.close();
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) socket.close();
}
}
@@ -72,8 +72,8 @@ class TorTransportConnection implements DuplexTransportConnection {
}
public void dispose(boolean exception) throws IOException {
if(halfClosed.getAndSet(true) || exception)
if(!closed.getAndSet(true)) socket.close();
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) socket.close();
}
}
}