summaryrefslogtreecommitdiff
path: root/main/src/main/java/de/blinkt/openvpn/core/LogFileHandler.java
blob: a30cde5b53ed1b02c5cfa1f22ed90a36c80f9cb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (c) 2012-2015 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */

package de.blinkt.openvpn.core;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Parcel;
import android.text.TextUtils;

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 de.blinkt.openvpn.R;
import de.blinkt.openvpn.fragments.Utils;

/**
 * Created by arne on 23.01.16.
 */
class LogFileHandler extends Handler {
    static final int TRIM_LOG_FILE = 100;
    static final int FLUSH_TO_DISK = 101;
    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";


    public LogFileHandler(Looper looper) {
        super(looper);
    }


    @Override
    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) {
                // Ignore log messages if not yet initialized
                if (mLogFile == null)
                    return;
                writeLogItemToDisk((VpnStatus.LogItem) msg.obj);
            } else if (msg.what == TRIM_LOG_FILE) {
                trimLogFile();
                for (VpnStatus.LogItem li : VpnStatus.getlogbuffer())
                    writeLogItemToDisk(li);
            } else if (msg.what == FLUSH_TO_DISK) {
                flushToDisk();
            }

        } catch (IOException e) {
            e.printStackTrace();
            VpnStatus.logError("Error during log cache: " + msg.what);
            VpnStatus.logException(e);
        }

    }

    private void flushToDisk() throws IOException {
        mLogFile.flush();
    }

    private static void trimLogFile() {
        try {
            mBufLogfile.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);
        // We do not really care if the log cache breaks between Android upgrades,
        // write binary format to disc
        byte[] liBytes = p.marshall();

        byte[] lenBytes = ByteBuffer.allocate(4).putInt(liBytes.length).array();
        mLogFile.write(lenBytes);
        mLogFile.write(liBytes);
        p.recycle();
    }

    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;



        try {

            BufferedInputStream logFile = new BufferedInputStream(new FileInputStream(logfile));

            byte[] buf = new byte[8192];
            int read = logFile.read(buf, 0, 4);
            int itemsRead=0;

            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);
                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, 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");
            VpnStatus.logException(e);
            e.printStackTrace();
            // ignore reading file error
        }
    }

    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);
    }


}