From 2fdfb2b7f3d28667de2a20f68eb3ec46aebfec12 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 18 Sep 2013 14:37:34 +0200 Subject: Refactor logging messages --- src/de/blinkt/openvpn/LogWindow.java | 2 +- src/de/blinkt/openvpn/VpnProfile.java | 4 +- src/de/blinkt/openvpn/core/CIDRIP.java | 126 +++++++++++---------- src/de/blinkt/openvpn/core/OpenVPNThread.java | 4 +- .../openvpn/core/OpenVpnManagementThread.java | 29 +++-- src/de/blinkt/openvpn/core/OpenVpnService.java | 18 +-- src/de/blinkt/openvpn/core/VPNLaunchHelper.java | 11 +- src/de/blinkt/openvpn/core/VpnStatus.java | 101 +++++++++++------ 8 files changed, 163 insertions(+), 132 deletions(-) (limited to 'src/de/blinkt/openvpn') diff --git a/src/de/blinkt/openvpn/LogWindow.java b/src/de/blinkt/openvpn/LogWindow.java index 7eadfe60..f19e53d4 100644 --- a/src/de/blinkt/openvpn/LogWindow.java +++ b/src/de/blinkt/openvpn/LogWindow.java @@ -225,7 +225,7 @@ public class LogWindow extends ListActivity implements StateListener { // Actually is probably called from GUI Thread as result of the user // pressing a button. But better safe than sorry VpnStatus.clearLog(); - VpnStatus.logMessage(0, "", "Log cleared."); + VpnStatus.logInfo(R.string.logCleared); mHandler.sendEmptyMessage(MESSAGE_CLEARLOG); } diff --git a/src/de/blinkt/openvpn/VpnProfile.java b/src/de/blinkt/openvpn/VpnProfile.java index ed19cd17..cb186316 100644 --- a/src/de/blinkt/openvpn/VpnProfile.java +++ b/src/de/blinkt/openvpn/VpnProfile.java @@ -564,7 +564,7 @@ public class VpnProfile implements Serializable { cachain = KeyChain.getCertificateChain(context, mAlias); if (cachain.length <= 1 && !nonNull(mCaFilename)) { - VpnStatus.logMessage(0, "", context.getString(R.string.keychain_nocacert)); + VpnStatus.logMessage(VpnStatus.LogLevel.ERROR, "", context.getString(R.string.keychain_nocacert)); } else { StringWriter ksStringWriter = new StringWriter(); @@ -628,7 +628,7 @@ public class VpnProfile implements Serializable { } catch (IOException e) { e.printStackTrace(); } catch (KeyChainException e) { - VpnStatus.logMessage(0, "", context.getString(R.string.keychain_access)); + VpnStatus.logError(R.string.keychain_access); if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN) { if (!mAlias.matches("^[a-zA-Z0-9]$")) { VpnStatus.logError(R.string.jelly_keystore_alphanumeric_bug); diff --git a/src/de/blinkt/openvpn/core/CIDRIP.java b/src/de/blinkt/openvpn/core/CIDRIP.java index 27ce414c..960e7d11 100644 --- a/src/de/blinkt/openvpn/core/CIDRIP.java +++ b/src/de/blinkt/openvpn/core/CIDRIP.java @@ -2,65 +2,69 @@ package de.blinkt.openvpn.core; import java.util.Locale; -class CIDRIP{ - String mIp; - int len; - - - public CIDRIP(String ip, String mask){ - mIp=ip; - long netmask=getInt(mask); - - // Add 33. bit to ensure the loop terminates - netmask += 1l << 32; - - int lenZeros = 0; - while((netmask & 0x1) == 0) { - lenZeros++; - netmask = netmask >> 1; - } - // Check if rest of netmask is only 1s - if(netmask != (0x1ffffffffl >> lenZeros)) { - // Asume no CIDR, set /32 - len=32; - } else { - len =32 -lenZeros; - } - - } - public CIDRIP(String address, int prefix_length) { - len = prefix_length; - mIp = address; - } - @Override - public String toString() { - return String.format(Locale.ENGLISH,"%s/%d",mIp,len); - } - - public boolean normalise(){ - long ip=getInt(mIp); - - long newip = ip & (0xffffffffl << (32 -len)); - if (newip != ip){ - mIp = String.format("%d.%d.%d.%d", (newip & 0xff000000) >> 24,(newip & 0xff0000) >> 16, (newip & 0xff00) >> 8 ,newip & 0xff); - return true; - } else { - return false; - } - } - static long getInt(String ipaddr) { - String[] ipt = ipaddr.split("\\."); - long ip=0; - - ip += Long.parseLong(ipt[0])<< 24; - ip += Integer.parseInt(ipt[1])<< 16; - ip += Integer.parseInt(ipt[2])<< 8; - ip += Integer.parseInt(ipt[3]); - - return ip; - } - public long getInt() { - return getInt(mIp); - } - +class CIDRIP { + String mIp; + int len; + + + public CIDRIP(String ip, String mask) { + mIp = ip; + long netmask = getInt(mask); + + // Add 33. bit to ensure the loop terminates + netmask += 1l << 32; + + int lenZeros = 0; + while ((netmask & 0x1) == 0) { + lenZeros++; + netmask = netmask >> 1; + } + // Check if rest of netmask is only 1s + if (netmask != (0x1ffffffffl >> lenZeros)) { + // Asume no CIDR, set /32 + len = 32; + } else { + len = 32 - lenZeros; + } + + } + + public CIDRIP(String address, int prefix_length) { + len = prefix_length; + mIp = address; + } + + @Override + public String toString() { + return String.format(Locale.ENGLISH, "%s/%d", mIp, len); + } + + public boolean normalise() { + long ip = getInt(mIp); + + long newip = ip & (0xffffffffl << (32 - len)); + if (newip != ip) { + mIp = String.format("%d.%d.%d.%d", (newip & 0xff000000) >> 24, (newip & 0xff0000) >> 16, (newip & 0xff00) >> 8, newip & 0xff); + return true; + } else { + return false; + } + } + + static long getInt(String ipaddr) { + String[] ipt = ipaddr.split("\\."); + long ip = 0; + + ip += Long.parseLong(ipt[0]) << 24; + ip += Integer.parseInt(ipt[1]) << 16; + ip += Integer.parseInt(ipt[2]) << 8; + ip += Integer.parseInt(ipt[3]); + + return ip; + } + + public long getInt() { + return getInt(mIp); + } + } \ No newline at end of file diff --git a/src/de/blinkt/openvpn/core/OpenVPNThread.java b/src/de/blinkt/openvpn/core/OpenVPNThread.java index da6a394a..930d2614 100644 --- a/src/de/blinkt/openvpn/core/OpenVPNThread.java +++ b/src/de/blinkt/openvpn/core/OpenVPNThread.java @@ -111,12 +111,12 @@ public class OpenVPNThread implements Runnable { mDumpPath = logline.substring(DUMP_PATH_STRING.length()); - VpnStatus.logMessage(0, "P:", logline); + VpnStatus.logInfo("P:" + logline); } } catch (IOException e) { - VpnStatus.logMessage(0, "", "Error reading from output of OpenVPN process" + e.getLocalizedMessage()); + VpnStatus.logError("Error reading from output of OpenVPN process" + e.getLocalizedMessage()); e.printStackTrace(); stopProcess(); } diff --git a/src/de/blinkt/openvpn/core/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/core/OpenVpnManagementThread.java index 4fe54217..ffa192b5 100644 --- a/src/de/blinkt/openvpn/core/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/core/OpenVpnManagementThread.java @@ -93,14 +93,14 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { } public void managmentCommand(String cmd) { - if(mSocket!=null && mSocket.getOutputStream() !=null) { - try { + try { + if(mSocket!=null && mSocket.getOutputStream() !=null) { mSocket.getOutputStream().write(cmd.getBytes()); mSocket.getOutputStream().flush(); - } catch (IOException e) { - // Ignore socket stack traces } - } + }catch (IOException e) { + // Ignore socket stack traces + } } @@ -131,7 +131,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { try { fds = mSocket.getAncillaryFileDescriptors(); } catch (IOException e) { - VpnStatus.logMessage(0, "", "Error reading fds from socket" + e.getLocalizedMessage()); + VpnStatus.logError("Error reading fds from socket" + e.getLocalizedMessage()); e.printStackTrace(); } if(fds!=null){ @@ -182,7 +182,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { exp.printStackTrace(); Log.d("Openvpn", "Failed to retrieve fd from socket: " + fd); - VpnStatus.logMessage(0, "", "Failed to retrieve fd from socket: " + exp.getLocalizedMessage()); + VpnStatus.logError("Failed to retrieve fd from socket: " + exp.getLocalizedMessage()); } private String processInput(String pendingInput) { @@ -228,11 +228,11 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { // 0 unix time stamp // 1 log level N,I,E etc. // 2 log message - VpnStatus.logMessage(0, "", args[2]); + VpnStatus.logWarning( args[2]); } else if (cmd.equals("RSA_SIGN")) { processSignCommand(argument); } else { - VpnStatus.logMessage(0, "MGMT:", "Got unrecognized command" + command); + VpnStatus.logWarning("MGMT: Got unrecognized command" + command); Log.i(TAG, "Got unrecognized command" + command); } } else if (command.startsWith("SUCCESS:")) { @@ -240,7 +240,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { return; } else { Log.i(TAG, "Got unrecognized line from managment" + command); - VpnStatus.logMessage(0, "MGMT:", "Got unrecognized line from management:" + command); + VpnStatus.logWarning("MGMT: Got unrecognized line from management:" + command); } } private void handleHold() { @@ -372,8 +372,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { Exception exp; if(!extra.equals("tun")) { // We only support tun - String errmsg = String.format("Devicetype %s requested, but only tun is possible with the Android API, sorry!",extra); - VpnStatus.logMessage(0, "", errmsg); + VpnStatus.logError(String.format("Device type %s requested, but only tun is possible with the Android API, sorry!",extra)); return false; } @@ -416,7 +415,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { } catch (IOException e) { exp =e; } - VpnStatus.logMessage(0, "", "Could not send fd over socket:" + exp.getLocalizedMessage()); + VpnStatus.logError("Could not send fd over socket:" + exp.getLocalizedMessage()); exp.printStackTrace(); return false; @@ -439,7 +438,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { return; } } catch (StringIndexOutOfBoundsException sioob) { - VpnStatus.logMessage(0, "", "Could not parse management Password command: " + argument); + VpnStatus.logError("Could not parse management Password command: " + argument); return; } @@ -457,7 +456,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { String cmd = String.format("password '%s' %s\n", needed, VpnProfile.openVpnEscape(pw)); managmentCommand(cmd); } else { - VpnStatus.logMessage(0, VpnStatus.MANAGMENT_PREFIX, String.format("Openvpn requires Authentication type '%s' but no password/key information available", needed)); + VpnStatus.logError(String.format("Openvpn requires Authentication type '%s' but no password/key information available", needed)); } } diff --git a/src/de/blinkt/openvpn/core/OpenVpnService.java b/src/de/blinkt/openvpn/core/OpenVpnService.java index 37c0d94f..0e48d4d6 100644 --- a/src/de/blinkt/openvpn/core/OpenVpnService.java +++ b/src/de/blinkt/openvpn/core/OpenVpnService.java @@ -389,7 +389,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac Builder builder = new Builder(); if (mLocalIP == null && mLocalIPv6 == null) { - VpnStatus.logMessage(0, "", getString(R.string.opentun_no_ipaddr)); + VpnStatus.logError(getString(R.string.opentun_no_ipaddr)); return null; } @@ -430,7 +430,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac try { builder.addRoute(route.mIp, route.len); } catch (IllegalArgumentException ia) { - VpnStatus.logMessage(0, "", getString(R.string.route_rejected) + route + " " + ia.getLocalizedMessage()); + VpnStatus.logError(getString(R.string.route_rejected) + route + " " + ia.getLocalizedMessage()); } } @@ -439,7 +439,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac String[] v6parts = v6route.split("/"); builder.addRoute(v6parts[0], Integer.parseInt(v6parts[1])); } catch (IllegalArgumentException ia) { - VpnStatus.logMessage(0, "", getString(R.string.route_rejected) + v6route + " " + ia.getLocalizedMessage()); + VpnStatus.logError(getString(R.string.route_rejected) + v6route + " " + ia.getLocalizedMessage()); } } @@ -477,9 +477,9 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac try { return builder.establish(); } catch (Exception e) { - VpnStatus.logMessage(0, "", getString(R.string.tun_open_error)); - VpnStatus.logMessage(0, "", getString(R.string.error) + e.getLocalizedMessage()); - VpnStatus.logMessage(0, "", getString(R.string.tun_error_helpful)); + VpnStatus.logError(R.string.tun_open_error); + VpnStatus.logError(getString(R.string.error) + e.getLocalizedMessage()); + VpnStatus.logError(R.string.tun_error_helpful); return null; } @@ -510,11 +510,11 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac public void addRoute(String dest, String mask) { CIDRIP route = new CIDRIP(dest, mask); if (route.len == 32 && !mask.equals("255.255.255.255")) { - VpnStatus.logMessage(0, "", getString(R.string.route_not_cidr, dest, mask)); + VpnStatus.logWarning(R.string.route_not_cidr, dest, mask); } if (route.normalise()) - VpnStatus.logMessage(0, "", getString(R.string.route_not_netip, dest, route.len, route.mIp)); + VpnStatus.logWarning(R.string.route_not_netip, dest, route.len, route.mIp); mRoutes.add(route); } @@ -544,7 +544,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac else mLocalIP.len = 31; } else { - VpnStatus.logMessage(0, "", getString(R.string.ip_not_cidr, local, netmask, mode)); + VpnStatus.logWarning(R.string.ip_not_cidr, local, netmask, mode); } } } diff --git a/src/de/blinkt/openvpn/core/VPNLaunchHelper.java b/src/de/blinkt/openvpn/core/VPNLaunchHelper.java index f6e8e0e3..a8770a69 100644 --- a/src/de/blinkt/openvpn/core/VPNLaunchHelper.java +++ b/src/de/blinkt/openvpn/core/VPNLaunchHelper.java @@ -45,7 +45,7 @@ public class VPNLaunchHelper { fout.close(); if(!mvpnout.setExecutable(true)) { - VpnStatus.logMessage(0, "", "Failed to set minivpn executable"); + VpnStatus.logError("Failed to set minivpn executable"); return false; } @@ -53,8 +53,8 @@ public class VPNLaunchHelper { return true; } catch (IOException e) { if(e2!=null) - VpnStatus.logMessage(0, "", e2.getLocalizedMessage()); - VpnStatus.logMessage(0, "", e.getLocalizedMessage()); + VpnStatus.logError( e2.getLocalizedMessage()); + VpnStatus.logError(e.getLocalizedMessage()); e.printStackTrace(); return false; } @@ -63,10 +63,11 @@ public class VPNLaunchHelper { public static void startOpenVpn(VpnProfile startprofile, Context context) { if(!writeMiniVPN(context)) { - VpnStatus.logMessage(0, "", "Error writing minivpn binary"); + VpnStatus.logError("Error writing minivpn binary"); return; } - VpnStatus.logMessage(0, "", context.getString(R.string.building_configration)); + + VpnStatus.logInfo(R.string.building_configration); Intent startVPN = startprofile.prepareIntent(context); if(startVPN!=null) diff --git a/src/de/blinkt/openvpn/core/VpnStatus.java b/src/de/blinkt/openvpn/core/VpnStatus.java index 60fa730e..a83714a4 100644 --- a/src/de/blinkt/openvpn/core/VpnStatus.java +++ b/src/de/blinkt/openvpn/core/VpnStatus.java @@ -42,7 +42,6 @@ public class VpnStatus { private static long mlastByteCount[]={0,0,0,0}; - public enum ConnectionStatus { LEVEL_CONNECTED, LEVEL_VPNPAUSED, @@ -55,6 +54,31 @@ public class VpnStatus { UNKNOWN_LEVEL } + public enum LogLevel { + INFO(1), + ERROR(2), + WARNING(3), + VERBOSE(4); + + protected int mValue; + LogLevel(int value) { + mValue = value; + } + + public int getInt() { + return mValue; + } + + public static LogLevel getEnumByValue(int value) { + switch (value) { + case 1: return INFO; + case 2: return ERROR; + case 3: return WARNING; + default: return null; + } + } + } + public static final byte[] officalkey = {-58, -42, -44, -106, 90, -88, -87, -88, -52, -124, 84, 117, 66, 79, -112, -111, -46, 86, -37, 109}; public static final byte[] officaldebugkey = {-99, -69, 45, 71, 114, -116, 82, 66, -99, -122, 50, -70, -56, -111, 98, -35, -65, 105, 82, 43}; public static final byte[] amazonkey = {-116, -115, -118, -89, -116, -112, 120, 55, 79, -8, -119, -23, 106, -114, -85, -56, -4, 105, 26, -57}; @@ -71,18 +95,16 @@ public class VpnStatus { public static class LogItem implements Parcelable { - public static final int ERROR = 1; - public static final int INFO = 2; - public static final int VERBOSE = 3; + private Object [] mArgs = null; private String mMessage = null; private int mRessourceId; // Default log priority - int mLevel = INFO; + LogLevel mLevel = LogLevel.INFO; private long logtime = System.currentTimeMillis(); - public LogItem(int ressourceId, Object[] args) { + private LogItem(int ressourceId, Object[] args) { mRessourceId = ressourceId; mArgs = args; } @@ -93,20 +115,20 @@ public class VpnStatus { } - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeArray(mArgs); - dest.writeString(mMessage); - dest.writeInt(mRessourceId); - dest.writeInt(mLevel); - dest.writeLong(logtime); - } + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeArray(mArgs); + dest.writeString(mMessage); + dest.writeInt(mRessourceId); + dest.writeInt(mLevel.getInt()); + dest.writeLong(logtime); + } public LogItem(Parcel in) { mArgs = in.readArray(Object.class.getClassLoader()); mMessage = in.readString(); mRessourceId = in.readInt(); - mLevel = in.readInt(); + mLevel = LogLevel.getEnumByValue(in.readInt()); logtime = in.readLong(); } @@ -121,24 +143,20 @@ public class VpnStatus { } }; - public LogItem(int loglevel,int ressourceId, Object[] args) { + public LogItem(LogLevel loglevel,int ressourceId, Object[] args) { mRessourceId = ressourceId; mArgs = args; mLevel = loglevel; } - public LogItem(String message) { - mMessage = message; - } - - public LogItem(int loglevel, String msg) { + public LogItem(LogLevel loglevel, String msg) { mLevel = loglevel; mMessage = msg; } - public LogItem(int loglevel, int ressourceId) { + public LogItem(LogLevel loglevel, int ressourceId) { mRessourceId =ressourceId; mLevel = loglevel; } @@ -242,9 +260,9 @@ public class VpnStatus { void updateByteCount(long in, long out, long diffin, long diffout); } - public synchronized static void logMessage(int level,String prefix, String message) + public synchronized static void logMessage(LogLevel level,String prefix, String message) { - newlogItem(new LogItem(prefix + message)); + newLogItem(new LogItem(level, prefix + message)); } @@ -390,14 +408,14 @@ public class VpnStatus { } public static void logInfo(String message) { - newlogItem(new LogItem(LogItem.INFO, message)); + newLogItem(new LogItem(LogLevel.INFO, message)); } - public static void logInfo(int ressourceId, Object... args) { - newlogItem(new LogItem(LogItem.INFO, ressourceId, args)); + public static void logInfo(int resourceId, Object... args) { + newLogItem(new LogItem(LogLevel.INFO, resourceId, args)); } - private synchronized static void newlogItem(LogItem logItem) { + private synchronized static void newLogItem(LogItem logItem) { logbuffer.addLast(logItem); if(logbuffer.size()>MAXLOGENTRIES) logbuffer.removeFirst(); @@ -408,28 +426,37 @@ public class VpnStatus { } public static void logError(String msg) { - newlogItem(new LogItem(LogItem.ERROR, msg)); + newLogItem(new LogItem(LogLevel.ERROR, msg)); } - public static void logError(int ressourceId) { - newlogItem(new LogItem(LogItem.ERROR, ressourceId)); + public static void logWarning(int resourceId, Object... args) { + newLogItem(new LogItem(LogLevel.WARNING, resourceId, args)); + } + + public static void logWarning(String msg) { + newLogItem(new LogItem(LogLevel.WARNING, msg)); + } + + + public static void logError(int resourceId) { + newLogItem(new LogItem(LogLevel.ERROR, resourceId)); } - public static void logError(int ressourceId, Object... args) { - newlogItem(new LogItem(LogItem.ERROR, ressourceId,args)); + public static void logError(int resourceId, Object... args) { + newLogItem(new LogItem(LogLevel.ERROR, resourceId, args)); } public static synchronized void updateByteCount(long in, long out) { long lastIn = mlastByteCount[0]; long lastOut = mlastByteCount[1]; - long diffin = mlastByteCount[2] = in - lastIn; - long diffout = mlastByteCount[3] = out - lastOut; + long diffIn = mlastByteCount[2] = in - lastIn; + long diffOut = mlastByteCount[3] = out - lastOut; - mlastByteCount = new long[] {in,out,diffin,diffout}; + mlastByteCount = new long[] {in,out,diffIn,diffOut}; for(ByteCountListener bcl:byteCountListener){ - bcl.updateByteCount(in, out, diffin,diffout); + bcl.updateByteCount(in, out, diffIn,diffOut); } } -- cgit v1.2.3