From 3a409ed35f0f26644d99b8704801103de8783864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Sat, 2 Apr 2016 12:40:10 +0200 Subject: Update ics-openvpn --- .../de/blinkt/openvpn/core/LogFileHandler.java | 62 ++++++++++++++++------ 1 file changed, 47 insertions(+), 15 deletions(-) (limited to 'app/src/main/java/de/blinkt/openvpn/core/LogFileHandler.java') diff --git a/app/src/main/java/de/blinkt/openvpn/core/LogFileHandler.java b/app/src/main/java/de/blinkt/openvpn/core/LogFileHandler.java index 5c1741d9..288c7934 100644 --- a/app/src/main/java/de/blinkt/openvpn/core/LogFileHandler.java +++ b/app/src/main/java/de/blinkt/openvpn/core/LogFileHandler.java @@ -11,12 +11,15 @@ import android.os.Message; import android.os.Parcel; import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Locale; + +import se.leap.bitmaskclient.R; /** * Created by arne on 23.01.16. @@ -27,7 +30,6 @@ class LogFileHandler extends Handler { static final int LOG_INIT = 102; public static final int LOG_MESSAGE = 103; private static FileOutputStream mLogFile; - private static BufferedOutputStream mBufLogfile; public static final String LOGFILE_NAME = "logcache.dat"; @@ -41,6 +43,8 @@ class LogFileHandler extends Handler { public void handleMessage(Message msg) { try { if (msg.what == LOG_INIT) { + if (mLogFile != null) + throw new RuntimeException("mLogFile not null"); readLogCache((File) msg.obj); openLogFile((File) msg.obj); } else if (msg.what == LOG_MESSAGE && msg.obj instanceof VpnStatus.LogItem) { @@ -70,15 +74,13 @@ class LogFileHandler extends Handler { private static void trimLogFile() { try { - mBufLogfile.flush(); + mLogFile.flush(); mLogFile.getChannel().truncate(0); - } catch (IOException e) { e.printStackTrace(); } } - private void writeLogItemToDisk(VpnStatus.LogItem li) throws IOException { Parcel p = Parcel.obtain(); li.writeToParcel(p, 0); @@ -86,8 +88,8 @@ class LogFileHandler extends Handler { // write binary format to disc byte[] liBytes = p.marshall(); - mLogFile.write(liBytes.length & 0xff); - mLogFile.write(liBytes.length >> 8); + byte[] lenBytes = ByteBuffer.allocate(4).putInt(liBytes.length).array(); + mLogFile.write(lenBytes); mLogFile.write(liBytes); p.recycle(); } @@ -95,39 +97,56 @@ class LogFileHandler extends Handler { private void openLogFile (File cacheDir) throws FileNotFoundException { File logfile = new File(cacheDir, LOGFILE_NAME); mLogFile = new FileOutputStream(logfile); - mBufLogfile = new BufferedOutputStream(mLogFile); } private void readLogCache(File cacheDir) { File logfile = new File(cacheDir, LOGFILE_NAME); + if (!logfile.exists() || !logfile.canRead()) return; - VpnStatus.logDebug("Reread log items from cache file"); + try { + BufferedInputStream logFile = new BufferedInputStream(new FileInputStream(logfile)); byte[] buf = new byte[8192]; - int read = logFile.read(buf, 0, 2); + int read = logFile.read(buf, 0, 4); + int itemsRead=0; - while (read > 0) { - // Marshalled LogItem - int len = (0xff & buf[0]) | buf[1] << 8; + while (read >= 4) { + int len = ByteBuffer.wrap(buf, 0, 4).asIntBuffer().get(); + // Marshalled LogItem read = logFile.read(buf, 0, len); Parcel p = Parcel.obtain(); p.unmarshall(buf, 0, read); p.setDataPosition(0); VpnStatus.LogItem li = VpnStatus.LogItem.CREATOR.createFromParcel(p); - VpnStatus.newLogItem(li, true); + if (li.verify()) { + VpnStatus.newLogItem(li, true); + } else { + VpnStatus.logError(String.format(Locale.getDefault(), + "Could not read log item from file: %d/%d: %s", + read, len, bytesToHex(buf, Math.max(read,80)))); + } p.recycle(); //Next item - read = logFile.read(buf, 0, 2); + read = logFile.read(buf, 0, 4); + itemsRead++; + if (itemsRead > 2*VpnStatus.MAXLOGENTRIES) { + VpnStatus.logError("Too many logentries read from cache, aborting."); + read = 0; + } + } + VpnStatus.logDebug(R.string.reread_log, itemsRead); + + } catch (java.io.IOException | java.lang.RuntimeException e) { VpnStatus.logError("Reading cached logfile failed"); @@ -137,4 +156,17 @@ class LogFileHandler extends Handler { } } + final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); + public static String bytesToHex(byte[] bytes, int len) { + len = Math.min(bytes.length, len); + char[] hexChars = new char[len * 2]; + for ( int j = 0; j < len; j++ ) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = hexArray[v >>> 4]; + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; + } + return new String(hexChars); + } + + } -- cgit v1.2.3