/
DatabaseHelper.java
1042 lines (911 loc) · 35.9 KB
/
DatabaseHelper.java
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package eu.faircode.netguard;
/*
This file is part of NetGuard.
NetGuard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2015-2017 by Marcel Bokhorst (M66B)
*/
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDoneException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "NetGuard.Database";
private static final String DB_NAME = "Netguard";
private static final int DB_VERSION = 20;
private static boolean once = true;
private static List<LogChangedListener> logChangedListeners = new ArrayList<>();
private static List<AccessChangedListener> accessChangedListeners = new ArrayList<>();
private static List<ForwardChangedListener> forwardChangedListeners = new ArrayList<>();
private static HandlerThread hthread = null;
private static Handler handler = null;
private final static int MSG_LOG = 1;
private final static int MSG_ACCESS = 2;
private final static int MSG_FORWARD = 3;
private SharedPreferences prefs;
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
static {
hthread = new HandlerThread("DatabaseHelper");
hthread.start();
handler = new Handler(hthread.getLooper()) {
@Override
public void handleMessage(Message msg) {
handleChangedNotification(msg);
}
};
}
private static DatabaseHelper dh = null;
public static DatabaseHelper getInstance(Context context) {
if (dh == null)
dh = new DatabaseHelper(context.getApplicationContext());
return dh;
}
@Override
public void close() {
Log.w(TAG, "Database is being closed");
}
private DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!once) {
once = true;
File dbfile = context.getDatabasePath(DB_NAME);
if (dbfile.exists()) {
Log.w(TAG, "Deleting " + dbfile);
dbfile.delete();
}
File dbjournal = context.getDatabasePath(DB_NAME + "-journal");
if (dbjournal.exists()) {
Log.w(TAG, "Deleting " + dbjournal);
dbjournal.delete();
}
}
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "Creating database " + DB_NAME + " version " + DB_VERSION);
createTableLog(db);
createTableAccess(db);
createTableDns(db);
createTableForward(db);
}
@Override
public void onConfigure(SQLiteDatabase db) {
db.enableWriteAheadLogging();
super.onConfigure(db);
}
private void createTableLog(SQLiteDatabase db) {
Log.i(TAG, "Creating log table");
db.execSQL("CREATE TABLE log (" +
" ID INTEGER PRIMARY KEY AUTOINCREMENT" +
", time INTEGER NOT NULL" +
", version INTEGER NULL" +
", protocol INTEGER NULL" +
", flags TEXT" +
", saddr TEXT" +
", sport INTEGER NULL" +
", daddr TEXT" +
", dport INTEGER NULL" +
", dname TEXT NULL" +
", uid INTEGER NULL" +
", data TEXT" +
", allowed INTEGER NULL" +
", connection INTEGER NULL" +
", interactive INTEGER NULL" +
");");
db.execSQL("CREATE INDEX idx_log_time ON log(time)");
db.execSQL("CREATE INDEX idx_log_dest ON log(daddr)");
db.execSQL("CREATE INDEX idx_log_dname ON log(dname)");
db.execSQL("CREATE INDEX idx_log_dport ON log(dport)");
db.execSQL("CREATE INDEX idx_log_uid ON log(uid)");
}
private void createTableAccess(SQLiteDatabase db) {
Log.i(TAG, "Creating access table");
db.execSQL("CREATE TABLE access (" +
" ID INTEGER PRIMARY KEY AUTOINCREMENT" +
", uid INTEGER NOT NULL" +
", version INTEGER NOT NULL" +
", protocol INTEGER NOT NULL" +
", daddr TEXT NOT NULL" +
", dport INTEGER NOT NULL" +
", time INTEGER NOT NULL" +
", allowed INTEGER NULL" +
", block INTEGER NOT NULL" +
", sent INTEGER NULL" +
", received INTEGER NULL" +
", connections INTEGER NULL" +
");");
db.execSQL("CREATE UNIQUE INDEX idx_access ON access(uid, version, protocol, daddr, dport)");
db.execSQL("CREATE INDEX idx_access_daddr ON access(daddr)");
db.execSQL("CREATE INDEX idx_access_block ON access(block)");
}
private void createTableDns(SQLiteDatabase db) {
Log.i(TAG, "Creating dns table");
db.execSQL("CREATE TABLE dns (" +
" ID INTEGER PRIMARY KEY AUTOINCREMENT" +
", time INTEGER NOT NULL" +
", qname TEXT NOT NULL" +
", aname TEXT NOT NULL" +
", resource TEXT NOT NULL" +
", ttl INTEGER NULL" +
");");
db.execSQL("CREATE UNIQUE INDEX idx_dns ON dns(qname, aname, resource)");
db.execSQL("CREATE INDEX idx_dns_resource ON dns(resource)");
}
private void createTableForward(SQLiteDatabase db) {
Log.i(TAG, "Creating forward table");
db.execSQL("CREATE TABLE forward (" +
" ID INTEGER PRIMARY KEY AUTOINCREMENT" +
", protocol INTEGER NOT NULL" +
", dport INTEGER NOT NULL" +
", raddr TEXT NOT NULL" +
", rport INTEGER NOT NULL" +
", ruid INTEGER NOT NULL" +
");");
db.execSQL("CREATE UNIQUE INDEX idx_forward ON forward(protocol, dport)");
}
private boolean columnExists(SQLiteDatabase db, String table, String column) {
Cursor cursor = null;
try {
cursor = db.rawQuery("SELECT * FROM " + table + " LIMIT 0", null);
return (cursor.getColumnIndex(column) >= 0);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
return false;
} finally {
if (cursor != null)
cursor.close();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(TAG, DB_NAME + " upgrading from version " + oldVersion + " to " + newVersion);
db.beginTransaction();
try {
if (oldVersion < 2) {
if (!columnExists(db, "log", "version"))
db.execSQL("ALTER TABLE log ADD COLUMN version INTEGER NULL");
if (!columnExists(db, "log", "protocol"))
db.execSQL("ALTER TABLE log ADD COLUMN protocol INTEGER NULL");
if (!columnExists(db, "log", "uid"))
db.execSQL("ALTER TABLE log ADD COLUMN uid INTEGER NULL");
oldVersion = 2;
}
if (oldVersion < 3) {
if (!columnExists(db, "log", "port"))
db.execSQL("ALTER TABLE log ADD COLUMN port INTEGER NULL");
if (!columnExists(db, "log", "flags"))
db.execSQL("ALTER TABLE log ADD COLUMN flags TEXT");
oldVersion = 3;
}
if (oldVersion < 4) {
if (!columnExists(db, "log", "connection"))
db.execSQL("ALTER TABLE log ADD COLUMN connection INTEGER NULL");
oldVersion = 4;
}
if (oldVersion < 5) {
if (!columnExists(db, "log", "interactive"))
db.execSQL("ALTER TABLE log ADD COLUMN interactive INTEGER NULL");
oldVersion = 5;
}
if (oldVersion < 6) {
if (!columnExists(db, "log", "allowed"))
db.execSQL("ALTER TABLE log ADD COLUMN allowed INTEGER NULL");
oldVersion = 6;
}
if (oldVersion < 7) {
db.execSQL("DROP TABLE log");
createTableLog(db);
oldVersion = 8;
}
if (oldVersion < 8) {
if (!columnExists(db, "log", "data"))
db.execSQL("ALTER TABLE log ADD COLUMN data TEXT");
db.execSQL("DROP INDEX idx_log_source");
db.execSQL("DROP INDEX idx_log_dest");
db.execSQL("CREATE INDEX idx_log_source ON log(saddr)");
db.execSQL("CREATE INDEX idx_log_dest ON log(daddr)");
db.execSQL("CREATE INDEX IF NOT EXISTS idx_log_uid ON log(uid)");
oldVersion = 8;
}
if (oldVersion < 9) {
createTableAccess(db);
oldVersion = 9;
}
if (oldVersion < 10) {
db.execSQL("DROP TABLE log");
db.execSQL("DROP TABLE access");
createTableLog(db);
createTableAccess(db);
oldVersion = 10;
}
if (oldVersion < 12) {
db.execSQL("DROP TABLE access");
createTableAccess(db);
oldVersion = 12;
}
if (oldVersion < 13) {
db.execSQL("CREATE INDEX IF NOT EXISTS idx_log_dport ON log(dport)");
db.execSQL("CREATE INDEX IF NOT EXISTS idx_log_dname ON log(dname)");
oldVersion = 13;
}
if (oldVersion < 14) {
createTableDns(db);
oldVersion = 14;
}
if (oldVersion < 15) {
db.execSQL("DROP TABLE access");
createTableAccess(db);
oldVersion = 15;
}
if (oldVersion < 16) {
createTableForward(db);
oldVersion = 16;
}
if (oldVersion < 17) {
if (!columnExists(db, "access", "sent"))
db.execSQL("ALTER TABLE access ADD COLUMN sent INTEGER NULL");
if (!columnExists(db, "access", "received"))
db.execSQL("ALTER TABLE access ADD COLUMN received INTEGER NULL");
oldVersion = 17;
}
if (oldVersion < 18) {
db.execSQL("CREATE INDEX IF NOT EXISTS idx_access_block ON access(block)");
db.execSQL("DROP INDEX idx_dns");
db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_dns ON dns(qname, aname, resource)");
db.execSQL("CREATE INDEX IF NOT EXISTS idx_dns_resource ON dns(resource)");
oldVersion = 18;
}
if (oldVersion < 19) {
if (!columnExists(db, "access", "connections"))
db.execSQL("ALTER TABLE access ADD COLUMN connections INTEGER NULL");
oldVersion = 19;
}
if (oldVersion < 20) {
db.execSQL("CREATE INDEX IF NOT EXISTS idx_access_daddr ON access(daddr)");
oldVersion = 20;
}
if (oldVersion == DB_VERSION) {
db.setVersion(oldVersion);
db.setTransactionSuccessful();
Log.i(TAG, DB_NAME + " upgraded to " + DB_VERSION);
} else
throw new IllegalArgumentException(DB_NAME + " upgraded to " + oldVersion + " but required " + DB_VERSION);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
} finally {
db.endTransaction();
}
}
// Log
public void insertLog(Packet packet, String dname, int connection, boolean interactive) {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
ContentValues cv = new ContentValues();
cv.put("time", packet.time);
cv.put("version", packet.version);
if (packet.protocol < 0)
cv.putNull("protocol");
else
cv.put("protocol", packet.protocol);
cv.put("flags", packet.flags);
cv.put("saddr", packet.saddr);
if (packet.sport < 0)
cv.putNull("sport");
else
cv.put("sport", packet.sport);
cv.put("daddr", packet.daddr);
if (packet.dport < 0)
cv.putNull("dport");
else
cv.put("dport", packet.dport);
if (dname == null)
cv.putNull("dname");
else
cv.put("dname", dname);
cv.put("data", packet.data);
if (packet.uid < 0)
cv.putNull("uid");
else
cv.put("uid", packet.uid);
cv.put("allowed", packet.allowed ? 1 : 0);
cv.put("connection", connection);
cv.put("interactive", interactive ? 1 : 0);
if (db.insert("log", null, cv) == -1)
Log.e(TAG, "Insert log failed");
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
notifyLogChanged();
}
public void clearLog() {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
db.delete("log", null, new String[]{});
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
db.execSQL("VACUUM");
} finally {
lock.writeLock().unlock();
}
notifyLogChanged();
}
public void cleanupLog(long time) {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
// There an index on time
int rows = db.delete("log", "time < ?", new String[]{Long.toString(time)});
Log.i(TAG, "Cleanup log" +
" before=" + SimpleDateFormat.getDateTimeInstance().format(new Date(time)) +
" rows=" + rows);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
}
public Cursor getLog(boolean udp, boolean tcp, boolean other, boolean allowed, boolean blocked) {
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
// There is an index on time
// There is no index on protocol/allowed for write performance
String query = "SELECT ID AS _id, *";
query += " FROM log";
query += " WHERE (0 = 1";
if (udp)
query += " OR protocol = 17";
if (tcp)
query += " OR protocol = 6";
if (other)
query += " OR (protocol <> 6 AND protocol <> 17)";
query += ") AND (0 = 1";
if (allowed)
query += " OR allowed = 1";
if (blocked)
query += " OR allowed = 0";
query += ")";
query += " ORDER BY time DESC";
return db.rawQuery(query, new String[]{});
} finally {
lock.readLock().unlock();
}
}
public Cursor searchLog(String find) {
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
// There is an index on daddr, dname, dport and uid
String query = "SELECT ID AS _id, *";
query += " FROM log";
query += " WHERE daddr LIKE ? OR dname LIKE ? OR dport = ? OR uid = ?";
query += " ORDER BY time DESC";
return db.rawQuery(query, new String[]{"%" + find + "%", "%" + find + "%", find, find});
} finally {
lock.readLock().unlock();
}
}
// Access
public boolean updateAccess(Packet packet, String dname, int block) {
int rows;
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
ContentValues cv = new ContentValues();
cv.put("time", packet.time);
cv.put("allowed", packet.allowed ? 1 : 0);
if (block >= 0)
cv.put("block", block);
// There is a segmented index on uid, version, protocol, daddr and dport
rows = db.update("access", cv, "uid = ? AND version = ? AND protocol = ? AND daddr = ? AND dport = ?",
new String[]{
Integer.toString(packet.uid),
Integer.toString(packet.version),
Integer.toString(packet.protocol),
dname == null ? packet.daddr : dname,
Integer.toString(packet.dport)});
if (rows == 0) {
cv.put("uid", packet.uid);
cv.put("version", packet.version);
cv.put("protocol", packet.protocol);
cv.put("daddr", dname == null ? packet.daddr : dname);
cv.put("dport", packet.dport);
if (block < 0)
cv.put("block", block);
if (db.insert("access", null, cv) == -1)
Log.e(TAG, "Insert access failed");
} else if (rows != 1)
Log.e(TAG, "Update access failed rows=" + rows);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
notifyAccessChanged();
return (rows == 0);
}
public void updateUsage(Usage usage, String dname) {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
// There is a segmented index on uid, version, protocol, daddr and dport
String selection = "uid = ? AND version = ? AND protocol = ? AND daddr = ? AND dport = ?";
String[] selectionArgs = new String[]{
Integer.toString(usage.Uid),
Integer.toString(usage.Version),
Integer.toString(usage.Protocol),
dname == null ? usage.DAddr : dname,
Integer.toString(usage.DPort)
};
Cursor cursor = db.query("access", new String[]{"sent", "received", "connections"}, selection, selectionArgs, null, null, null);
long sent = 0;
long received = 0;
int connections = 0;
int colSent = cursor.getColumnIndex("sent");
int colReceived = cursor.getColumnIndex("received");
int colConnections = cursor.getColumnIndex("connections");
if (cursor.moveToNext()) {
sent = cursor.isNull(colSent) ? 0 : cursor.getLong(colSent);
received = cursor.isNull(colReceived) ? 0 : cursor.getLong(colReceived);
connections = cursor.isNull(colConnections) ? 0 : cursor.getInt(colConnections);
}
cursor.close();
ContentValues cv = new ContentValues();
cv.put("sent", sent + usage.Sent);
cv.put("received", received + usage.Received);
cv.put("connections", connections + 1);
int rows = db.update("access", cv, selection, selectionArgs);
if (rows != 1)
Log.e(TAG, "Update usage failed rows=" + rows);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
notifyAccessChanged();
}
public void setAccess(long id, int block) {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
ContentValues cv = new ContentValues();
cv.put("block", block);
cv.put("allowed", -1);
if (db.update("access", cv, "ID = ?", new String[]{Long.toString(id)}) != 1)
Log.e(TAG, "Set access failed");
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
notifyAccessChanged();
}
public void clearAccess() {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
db.delete("access", null, null);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
notifyAccessChanged();
}
public void clearAccess(int uid, boolean keeprules) {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
// There is a segmented index on uid
// There is an index on block
if (keeprules)
db.delete("access", "uid = ? AND block < 0", new String[]{Integer.toString(uid)});
else
db.delete("access", "uid = ?", new String[]{Integer.toString(uid)});
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
notifyAccessChanged();
}
public void resetUsage(int uid) {
lock.writeLock().lock();
try {
// There is a segmented index on uid
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
ContentValues cv = new ContentValues();
cv.putNull("sent");
cv.putNull("received");
cv.putNull("connections");
db.update("access", cv,
(uid < 0 ? null : "uid = ?"),
(uid < 0 ? null : new String[]{Integer.toString(uid)}));
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
notifyAccessChanged();
}
public Cursor getAccess(int uid) {
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on uid
// There is no index on time for write performance
String query = "SELECT ID AS _id, *";
query += " FROM access";
query += " WHERE uid = ?";
query += " ORDER BY time DESC";
query += " LIMIT 50";
return db.rawQuery(query, new String[]{Integer.toString(uid)});
} finally {
lock.readLock().unlock();
}
}
public Cursor getAccess() {
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on uid
// There is an index on block
return db.query("access", null, "block >= 0", null, null, null, "uid");
} finally {
lock.readLock().unlock();
}
}
public Cursor getAccessUnset(int uid, int limit) {
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on uid, block and daddr
// There is no index on allowed and time for write performance
String query = "SELECT MAX(time) AS time, daddr, allowed";
query += " FROM access";
query += " WHERE uid = ?";
query += " AND block < 0";
query += " GROUP BY daddr, allowed";
query += " ORDER BY time DESC";
if (limit > 0)
query += " LIMIT " + limit;
return db.rawQuery(query, new String[]{Integer.toString(uid)});
} finally {
lock.readLock().unlock();
}
}
public long getRuleCount(int uid) {
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on uid
// There is an index on block
return db.compileStatement("SELECT COUNT(*) FROM access WHERE block >= 0 AND uid =" + uid).simpleQueryForLong();
} finally {
lock.readLock().unlock();
}
}
// DNS
public boolean insertDns(ResourceRecord rr) {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
int ttl = rr.TTL;
int min = Integer.parseInt(prefs.getString("ttl", "86400"));
if (ttl < min)
ttl = min;
ContentValues cv = new ContentValues();
cv.put("time", rr.Time);
cv.put("ttl", ttl * 1000L);
int rows = db.update("dns", cv, "qname = ? AND aname = ? AND resource = ?",
new String[]{rr.QName, rr.AName, rr.Resource});
if (rows == 0) {
cv.put("qname", rr.QName);
cv.put("aname", rr.AName);
cv.put("resource", rr.Resource);
if (db.insert("dns", null, cv) == -1)
Log.e(TAG, "Insert dns failed");
} else if (rows != 1)
Log.e(TAG, "Update dns failed rows=" + rows);
db.setTransactionSuccessful();
return (rows == 0);
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
}
public void cleanupDns() {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
// There is no index on time for write performance
long now = new Date().getTime();
db.execSQL("DELETE FROM dns WHERE time + ttl < " + now);
Log.i(TAG, "Cleanup DNS");
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
}
public void clearDns() {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
db.delete("dns", null, new String[]{});
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
}
public String getQName(String ip) {
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on resource
String query = "SELECT qname";
query += " FROM dns";
query += " WHERE resource = '" + ip.replace("'", "''") + "'";
query += " ORDER BY time DESC";
query += " LIMIT 1";
return db.compileStatement(query).simpleQueryForString();
} catch (SQLiteDoneException ignored) {
// Not found
return null;
} finally {
lock.readLock().unlock();
}
}
public Cursor getDns() {
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on qname
String query = "SELECT ID AS _id, *";
query += " FROM dns";
query += " ORDER BY qname, resource";
return db.rawQuery(query, new String[]{});
} finally {
lock.readLock().unlock();
}
}
public Cursor getAccessDns(String dname) {
long now = new Date().getTime();
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on dns.qname
// There is an index on access.daddr and access.block
String query = "SELECT a.uid, a.version, a.protocol, a.daddr, d.resource, a.dport, a.block, d.time, d.ttl";
query += " FROM access AS a";
query += " LEFT JOIN dns AS d";
query += " ON d.qname = a.daddr";
query += " WHERE a.block >= 0";
query += " AND d.time + d.ttl >= " + now;
if (dname != null)
query += " AND a.daddr = ?";
return db.rawQuery(query, dname == null ? new String[]{} : new String[]{dname});
} finally {
lock.readLock().unlock();
}
}
// Forward
public void addForward(int protocol, int dport, String raddr, int rport, int ruid) {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
ContentValues cv = new ContentValues();
cv.put("protocol", protocol);
cv.put("dport", dport);
cv.put("raddr", raddr);
cv.put("rport", rport);
cv.put("ruid", ruid);
if (db.insert("forward", null, cv) < 0)
Log.e(TAG, "Insert forward failed");
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
notifyForwardChanged();
}
public void deleteForward() {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
db.delete("forward", null, null);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
notifyForwardChanged();
}
public void deleteForward(int protocol, int dport) {
lock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
db.delete("forward", "protocol = ? AND dport = ?",
new String[]{Integer.toString(protocol), Integer.toString(dport)});
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
lock.writeLock().unlock();
}
notifyForwardChanged();
}
public Cursor getForwarding() {
lock.readLock().lock();
try {
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT ID AS _id, *";
query += " FROM forward";
query += " ORDER BY dport";
return db.rawQuery(query, new String[]{});
} finally {
lock.readLock().unlock();
}
}
public void addLogChangedListener(LogChangedListener listener) {
logChangedListeners.add(listener);
}
public void removeLogChangedListener(LogChangedListener listener) {
logChangedListeners.remove(listener);
}
public void addAccessChangedListener(AccessChangedListener listener) {
accessChangedListeners.add(listener);
}
public void removeAccessChangedListener(AccessChangedListener listener) {
accessChangedListeners.remove(listener);
}
public void addForwardChangedListener(ForwardChangedListener listener) {
forwardChangedListeners.add(listener);
}
public void removeForwardChangedListener(ForwardChangedListener listener) {
forwardChangedListeners.remove(listener);
}
private void notifyLogChanged() {
Message msg = handler.obtainMessage();
msg.what = MSG_LOG;
handler.sendMessage(msg);
}
private void notifyAccessChanged() {
Message msg = handler.obtainMessage();
msg.what = MSG_ACCESS;
handler.sendMessage(msg);
}
private void notifyForwardChanged() {
Message msg = handler.obtainMessage();
msg.what = MSG_FORWARD;
handler.sendMessage(msg);
}
private static void handleChangedNotification(Message msg) {
// Batch notifications
try {
Thread.sleep(1000);
if (handler.hasMessages(msg.what))
handler.removeMessages(msg.what);