aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/helpers
diff options
context:
space:
mode:
authorFil <fil.bergamo@riseup.net>2017-03-11 12:40:52 +0100
committerFil <fil.bergamo@riseup.net>2017-03-11 12:40:52 +0100
commit49f64a40a23c74510c1270d9609d05e1e316bc67 (patch)
treeebb1462801c7503ddc712205344121c71e9698d0 /app/src/fil/libre/repwifiapp/helpers
parent98a80150b7c3ee2f47ec004676b62cdcc8b87afd (diff)
downloadpackages_apps_repwifi-49f64a40a23c74510c1270d9609d05e1e316bc67.zip
packages_apps_repwifi-49f64a40a23c74510c1270d9609d05e1e316bc67.tar.gz
packages_apps_repwifi-49f64a40a23c74510c1270d9609d05e1e316bc67.tar.bz2
Enable logging + bugfix + new features
Diffstat (limited to 'app/src/fil/libre/repwifiapp/helpers')
-rw-r--r--app/src/fil/libre/repwifiapp/helpers/Engine.java135
-rw-r--r--app/src/fil/libre/repwifiapp/helpers/Engine6p0.java17
-rw-r--r--app/src/fil/libre/repwifiapp/helpers/IEngine.java5
-rw-r--r--app/src/fil/libre/repwifiapp/helpers/NetworkManager.java46
-rw-r--r--app/src/fil/libre/repwifiapp/helpers/RootCommand.java2
-rw-r--r--app/src/fil/libre/repwifiapp/helpers/ShellCommand.java85
-rw-r--r--app/src/fil/libre/repwifiapp/helpers/Utils.java13
7 files changed, 244 insertions, 59 deletions
diff --git a/app/src/fil/libre/repwifiapp/helpers/Engine.java b/app/src/fil/libre/repwifiapp/helpers/Engine.java
index cdddb0b..5a2f2b2 100644
--- a/app/src/fil/libre/repwifiapp/helpers/Engine.java
+++ b/app/src/fil/libre/repwifiapp/helpers/Engine.java
@@ -20,11 +20,15 @@
package fil.libre.repwifiapp.helpers;
+import java.util.ArrayList;
import fil.libre.repwifiapp.Commons;
public abstract class Engine implements IEngine{
+ public static final String DNS1 = "193.183.98.154";
+ public static final String DNS2 = "87.98.175.85";
+
protected String getCmdWpaSup(){
return "wpa_supplicant -B -dd -i" + Commons.INTERFACE_NAME + " -C\"" +Commons.SOCKET_DIR + "\" -P\"" + Commons.PID_FILE + "\"";
}
@@ -34,10 +38,7 @@ public abstract class Engine implements IEngine{
}
protected abstract String getCmdWpaStart();
-
- public static final String DNS1 = "193.183.98.154";
- public static final String DNS2 = "87.98.175.85";
-
+
public boolean deleteFileIfExists(String filePath){
if (filePath == null){
@@ -54,16 +55,21 @@ public abstract class Engine implements IEngine{
return false;
}
+ //needs root (it only gets used by the 4p2 engine, working in /data/misc/wifi)
return executeRootCmd("if [ -e \""+ filePath + "\" ]; then rm \"" + filePath + "\"; fi");
+
}
public boolean chmodFile(String filePath, String mod){
+ //needs root (chmod)
return executeRootCmd("chmod " + mod + " \"" + filePath + "\"");
}
@Override
public boolean killPreviousConnections() {
+ //needs root (for killall)
+
Utils.logDebug("killing wpa_supplicant..:");
if (executeRootCmd("killall -SIGINT wpa_supplicant")){
Utils.logDebug("Killed wpa_supplicant");
@@ -86,6 +92,8 @@ public abstract class Engine implements IEngine{
@Override
public boolean clearWorkingDir(){
+ //needs root (to work within /data/misc/wifi)
+
Utils.logDebug("clearWorkingDir():");
if (executeRootCmd("rm -r " + Commons.SOCKET_DIR)){
@@ -122,6 +130,7 @@ public abstract class Engine implements IEngine{
Utils.logDebug("startWpaSupplicant():");
+ //needs root (for wpa_supplicant)
if (executeRootCmd(getCmdWpaSup())){
return true;
}else{
@@ -138,6 +147,7 @@ public abstract class Engine implements IEngine{
killPreviousConnections();
+ //is it really necessary??? --- Fil
if (! clearWorkingDir()){
Utils.logError("Failed clearing dir");
return null;
@@ -163,8 +173,8 @@ public abstract class Engine implements IEngine{
return null;
}
- //chmod 666 scan_file to make it readable
- if (!chmodFile(Commons.getScanFile(), "666")){
+ //chmod 664 scan_file to make it readable
+ if (!chmodFile(Commons.getScanFile(), "664")){
Utils.logError("failed chmodding scan_file");
return null;
}
@@ -174,7 +184,6 @@ public abstract class Engine implements IEngine{
Utils.logError("Unable to parse scan file into AccessPointInfo array");
}
-
return a;
}
@@ -185,6 +194,8 @@ public abstract class Engine implements IEngine{
@Override
public boolean disconnect(){
+ //needs root (for wpa_cli)
+
if (! isWpaSupplicantRunning()){
return true;
}
@@ -248,16 +259,99 @@ public abstract class Engine implements IEngine{
}
+ @Override
+ public boolean isInterfaceAvailable(String ifaceName){
+
+ String[]ifaces = getAvailableInterfaces();
+ if(ifaces == null || ifaces.length == 0){
+ return false;
+ }
+
+ for(String name : ifaces){
+ if (name.equals(ifaceName)){
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ @Override
+ public String[] getAvailableInterfaces(){
+
+ try {
+
+ ShellCommand cmd = new ShellCommand("ip link");
+ if (cmd.execute() == 0){
+
+ String out = cmd.getOutput();
+ if (out == null || out.contains("\n") == false){
+ Utils.logDebug("No out from ip link");
+ return null;
+ }
+
+ ArrayList<String> list = new ArrayList<String>();
+
+ String[] lines = out.split("\n");
+ for (String l : lines){
+
+ String[] fields = l.split(":");
+ if (fields.length != 3){
+ continue;
+ }
+
+ String interfName = fields[1].trim();
+ list.add(interfName);
+
+ }
+
+ String[] retArr = new String[list.size()];
+ retArr = list.toArray(retArr);
+
+ return retArr;
+
+ }
+ else{
+ return null;
+ }
+
+ } catch (Exception e) {
+ Utils.logError("Error while querying ip link", e);
+ return null;
+ }
+
+ }
+
public boolean runDhcpcd(){
+ //needs root
return executeRootCmd("dhcpcd " + Commons.INTERFACE_NAME);
}
public boolean interfaceUp(){
+ //needs root (tested)
return executeRootCmd("ifconfig " + Commons.INTERFACE_NAME + " up");
}
+ protected boolean executeCmd(String cmd){
+
+ try {
+
+ ShellCommand c = new ShellCommand(cmd);
+ if ( c.execute() == 0){
+ return true;
+ }else {
+ return false;
+ }
+
+ } catch (Exception e) {
+ Utils.logError("Error executing \"" + cmd + "\"",e);
+ return false;
+ }
+
+ }
+
protected boolean executeRootCmd(String cmd){
try {
@@ -306,12 +400,14 @@ public abstract class Engine implements IEngine{
protected boolean scanNetworks(){
+ //needs root (for wpa_supplicant and wpa_cli)
return executeRootCmd("bash " + Commons.getScriptScan());
}
protected boolean getScanResults(){
+ //needs root (for wpa_supplicant and wpa_cli)
return executeRootCmd("bash " + Commons.getScriptScanRes());
}
@@ -336,11 +432,6 @@ public abstract class Engine implements IEngine{
"sleep 1s\n";
- //Try to create and chmod script scan
- /* executeRootCmd("echo > " + Commons.getSCRIPT_SCAN());
- chmodFile(Commons.getSCRIPT_SCAN(), "666");*/
-
-
if (! Utils.writeFile(Commons.getScriptScan(),scan,true) ){
Exception e = Utils.getLastException();
@@ -351,9 +442,6 @@ public abstract class Engine implements IEngine{
return false;
}
- //Try to create and chmod script scanres
- /*executeRootCmd("echo > " + Commons.getSCRIPT_SCANRES());
- chmodFile(Commons.getSCRIPT_SCANRES(), "666");*/
if (! Utils.writeFile(Commons.getScriptScanRes(),scanRes,true) ){
@@ -376,22 +464,5 @@ public abstract class Engine implements IEngine{
}
- /*protected boolean createDhcpcdScritp(){
-
- String scriptDhcp = "dhcpcd "+ Commons.INTERFACE_NAME + "\n" +
- "sleep 3s\n";
- if (! Utils.writeFile(Commons.getScriptDhcpcd(),scriptDhcp,true) ){
-
- Exception e = Utils.getLastException();
- if (e != null){
- Utils.logError("Error while writing dhcpcd script.",e);
- }
-
- return false;
- }
-
- return true;
-
- }*/
}
diff --git a/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java b/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java
index 24096d2..9cbfeaf 100644
--- a/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java
+++ b/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java
@@ -180,6 +180,7 @@ public class Engine6p0 extends Engine{
}
private boolean destroyNetwork(){
+ //needs root (tested)
return executeRootCmd("ndc network destroy 1");
}
@@ -187,6 +188,7 @@ public class Engine6p0 extends Engine{
try {
+ //needs root (wpa_cli)
RootCommand su = new RootCommand(getCmdWpaCli() + " set_network " + networkID + " ssid '\"" + ssid + "\"'" );
if (su.execute() == 0){
String out = su.getOutput();
@@ -211,6 +213,8 @@ public class Engine6p0 extends Engine{
try {
+ //needs root (wpa_cli)
+
String cmdSetPass = null;
if (info.needsPassword()){
cmdSetPass = getCmdWpaCli() + " set_network " + networkID + " psk '\"" + info.getPassword() + "\"'";
@@ -244,6 +248,7 @@ public class Engine6p0 extends Engine{
try {
+ //needs root (wpa_cli)
RootCommand su = new RootCommand(getCmdWpaCli() + " select_network " + networkID);
if (su.execute() == 0){
String out = su.getOutput();
@@ -268,6 +273,8 @@ public class Engine6p0 extends Engine{
try {
+ //needs root (wpa_cli)
+
RootCommand su = new RootCommand(getCmdWpaCli() + " enable_network " + networkID);
if (su.execute() == 0){
String out = su.getOutput();
@@ -292,6 +299,8 @@ public class Engine6p0 extends Engine{
try {
+ //needs root (wpa_cli)
+
RootCommand su = new RootCommand(getCmdWpaCli() + " reassociate");
if (su.execute() == 0){
String out = su.getOutput();
@@ -316,14 +325,15 @@ public class Engine6p0 extends Engine{
try {
- RootCommand su = new RootCommand("ip route show dev " + Commons.INTERFACE_NAME);
- if (su.execute() != 0){
+ //doesn't need root (tested)
+ ShellCommand cmd = new ShellCommand("ip route show dev " + Commons.INTERFACE_NAME);
+ if (cmd.execute() != 0){
Utils.logDebug("command failed show route");
return null;
}
//read command output
- String out = su.getOutput();
+ String out = cmd.getOutput();
if (out == null){
return null;
}
@@ -354,6 +364,7 @@ public class Engine6p0 extends Engine{
}
private boolean clearAddrs(){
+ //needs root (tested)
return executeRootCmd("ndc interface clearaddrs " + Commons.INTERFACE_NAME);
}
diff --git a/app/src/fil/libre/repwifiapp/helpers/IEngine.java b/app/src/fil/libre/repwifiapp/helpers/IEngine.java
index 67ffb86..1e33f1c 100644
--- a/app/src/fil/libre/repwifiapp/helpers/IEngine.java
+++ b/app/src/fil/libre/repwifiapp/helpers/IEngine.java
@@ -37,5 +37,8 @@ public interface IEngine {
public ConnectionStatus getConnectionStatus();
-
+ public boolean isInterfaceAvailable(String ifaceName);
+
+ public String[] getAvailableInterfaces();
+
}
diff --git a/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java b/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java
index bc59862..33acbb7 100644
--- a/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java
+++ b/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java
@@ -109,32 +109,40 @@ public class NetworkManager {
}
}
+
+
+ if (save){
+ //add the updated info to the storage
+ info.setLastTimeUsed(System.currentTimeMillis());
+ newlist.add(info);
+ }
for(AccessPointInfo old : existingNets){
if (old == null){
+ //error while loading from file. skip.
continue;
}
-
- if (old.getBSSID().equals(info.getBSSID()) && old.getSSID().equals(info.getSSID())){
-
- //found previous entry for this network,
- //if the call is for saving, overwrite the entry with the new one,
- //else omit the line, to remove network from the saved list.
- if (save){
- info.setLastTimeUsed(System.currentTimeMillis());
- newlist.add(info);
- }
-
- }else{
- //other network, keep it in the file
- // only if it's not older than the max age for a network
- if (! info.isOlderThan(NET_MAX_AGE)){
- newlist.add(old);
- }
+
+ // keep network only if it's not older than the max age for a network
+ else if (old.isOlderThan(NET_MAX_AGE)){
+ //skip it
+ continue;
}
-
+
+ else if (old.getBSSID().equals(info.getBSSID()) && old.getSSID().equals(info.getSSID())){
+ //found previously saved entry for the same network we are managing
+ //skip it
+ continue;
+ }
+
+ else{
+ //old network info that can be kept in the storage
+ newlist.add(old);
+ }
+
}
+
AccessPointInfo[] newContents = new AccessPointInfo[newlist.size()];
newContents = newlist.toArray(newContents);
@@ -240,7 +248,7 @@ public class NetworkManager {
}
String[] lines = Utils.readFileLines(_knownNetworksFile);
- if (lines.length == 0){
+ if (lines == null || lines.length == 0){
return null;
}
diff --git a/app/src/fil/libre/repwifiapp/helpers/RootCommand.java b/app/src/fil/libre/repwifiapp/helpers/RootCommand.java
index 3da4ae0..bcb6d0f 100644
--- a/app/src/fil/libre/repwifiapp/helpers/RootCommand.java
+++ b/app/src/fil/libre/repwifiapp/helpers/RootCommand.java
@@ -51,7 +51,7 @@ public class RootCommand {
if ( this._cmdTxt != null ){
- Utils.logDebug("EXEC: " + this._cmdTxt);
+ Utils.logDebug("SU:EXEC: " + this._cmdTxt);
this._cmdTxt += " > " + Commons.getTempOutFile();
diff --git a/app/src/fil/libre/repwifiapp/helpers/ShellCommand.java b/app/src/fil/libre/repwifiapp/helpers/ShellCommand.java
new file mode 100644
index 0000000..e2004a7
--- /dev/null
+++ b/app/src/fil/libre/repwifiapp/helpers/ShellCommand.java
@@ -0,0 +1,85 @@
+package fil.libre.repwifiapp.helpers;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class ShellCommand {
+
+ private String _cmdOut = "";
+ private String _cmdTxt = "";
+
+ public ShellCommand(String commandText){
+ this._cmdTxt = commandText;
+ }
+
+
+ public int execute() throws Exception{
+
+ if ( this._cmdTxt == null ){
+ return -9;
+ }
+
+ Utils.logDebug("EXEC: " + this._cmdTxt);
+
+ Process cmd = Runtime.getRuntime().exec(this._cmdTxt);
+
+ InputStream os = cmd.getInputStream();
+ InputStream es = cmd.getErrorStream();
+
+ StringBuilder sb = new StringBuilder();
+
+ sb.append(getStringFromStream(es));
+ sb.append(getStringFromStream(os));
+
+ int res = cmd.waitFor();
+
+ //re-read the output, in case it was empty when first tried
+ sb.append(getStringFromStream(es));
+ sb.append(getStringFromStream(os));
+
+ this._cmdOut = sb.toString();
+
+ Utils.logDebug("EXITCODE: " + res);
+ Utils.logDebug("OUT: " + getOutput());
+
+ return res;
+
+ }
+
+ private String getStringFromStream(InputStream s) throws IOException{
+
+ StringBuilder sb = new StringBuilder();
+ while ( (s.available() > 0) ) {
+ int b = s.read();
+ if (b>=0){
+ sb.append((char)b);
+ }else{
+ break;
+ }
+ }
+
+ return sb.toString();
+
+ }
+
+
+ public String getOutput(){
+
+ return this._cmdOut;
+
+ /*String[] lastOut = Utils.readFileLines(Commons.getTempOutFile());
+ if (lastOut == null){
+ return this._cmdOut;
+ }
+
+ String fout = "";
+
+ for (String s : lastOut){
+ fout += s + "\n";
+ }
+
+ return fout;*/
+
+ }
+
+}
diff --git a/app/src/fil/libre/repwifiapp/helpers/Utils.java b/app/src/fil/libre/repwifiapp/helpers/Utils.java
index 2e5fa27..bb3c682 100644
--- a/app/src/fil/libre/repwifiapp/helpers/Utils.java
+++ b/app/src/fil/libre/repwifiapp/helpers/Utils.java
@@ -27,15 +27,21 @@ import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
+import fil.libre.repwifiapp.Commons;
+
+import android.app.AlertDialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.SharedPreferences;
import android.util.Log;
public class Utils {
private static final long MILLIS_IN_DAY = 86400000;
- public static final int logLevel = 1;
-
+
public static final String APP_NAME = "RepWifi";
private static Exception _lastException = null;
@@ -58,13 +64,14 @@ public class Utils {
public static void logDebug(String msg, int level){
- if (level < logLevel){
+ if (level < Commons.getLogPriority()){
return;
}
Log.d(APP_NAME,msg);
}
+
public static boolean writeFile(String filePath, String text, boolean overwrite){