Disabling prepared statement for now

This commit is contained in:
relikd
2020-06-20 01:03:51 +02:00
parent f284365469
commit 778f377e42
4 changed files with 45 additions and 41 deletions

View File

@@ -1,7 +1,5 @@
import NetworkExtension import NetworkExtension
fileprivate var db: SQLiteDatabase!
fileprivate var pStmt: OpaquePointer!
fileprivate var filterDomains: [String]! fileprivate var filterDomains: [String]!
fileprivate var filterOptions: [(block: Bool, ignore: Bool)]! fileprivate var filterOptions: [(block: Bool, ignore: Bool)]!
@@ -9,7 +7,7 @@ fileprivate var filterOptions: [(block: Bool, ignore: Bool)]!
// MARK: Backward DNS Binary Tree Lookup // MARK: Backward DNS Binary Tree Lookup
fileprivate func reloadDomainFilter() { fileprivate func reloadDomainFilter() {
let tmp = db.loadFilters()?.map({ let tmp = AppDB?.loadFilters()?.map({
(String($0.reversed()), $1) (String($0.reversed()), $1)
}).sorted(by: { $0.0 < $1.0 }) ?? [] }).sorted(by: { $0.0 < $1.0 }) ?? []
filterDomains = tmp.map { $0.0 } filterDomains = tmp.map { $0.0 }
@@ -35,6 +33,18 @@ fileprivate func filterIndex(for domain: String) -> Int {
return -1 return -1
} }
private let queue = DispatchQueue.init(label: "PSIGlassDNSQueue", qos: .userInteractive, target: .main)
private func logAsync(_ domain: String, blocked: Bool) {
queue.async {
do {
try AppDB?.logWrite(domain, blocked: blocked)
} catch {
DDLogWarn("Couldn't write: \(error)")
}
}
}
// MARK: ObserverFactory // MARK: ObserverFactory
@@ -52,11 +62,11 @@ class LDObserverFactory: ObserverFactory {
let i = filterIndex(for: session.host) let i = filterIndex(for: session.host)
if i >= 0 { if i >= 0 {
let (block, ignore) = filterOptions[i] let (block, ignore) = filterOptions[i]
if !ignore { try? db.logWrite(pStmt, session.host, blocked: block) } if !ignore { logAsync(session.host, blocked: block) }
if block { socket.forceDisconnect() } if block { socket.forceDisconnect() }
} else { } else {
// TODO: disable filter during recordings // TODO: disable filter during recordings
try? db.logWrite(pStmt, session.host) logAsync(session.host, blocked: false)
} }
default: default:
break break
@@ -76,9 +86,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
override func startTunnel(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) { override func startTunnel(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) {
do { do {
db = try SQLiteDatabase.open() try SQLiteDatabase.open().initCommonScheme()
db.initCommonScheme()
pStmt = try db.logWritePrepare()
} catch { } catch {
completionHandler(error) completionHandler(error)
return return
@@ -135,9 +143,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
ObserverFactory.currentFactory = nil ObserverFactory.currentFactory = nil
proxyServer.stop() proxyServer.stop()
proxyServer = nil proxyServer = nil
db.prepared(finalize: pStmt)
pStmt = nil
db = nil
filterDomains = nil filterDomains = nil
filterOptions = nil filterOptions = nil
completionHandler() completionHandler()

View File

@@ -25,16 +25,22 @@ extension CreateTable {
} }
extension SQLiteDatabase { extension SQLiteDatabase {
// /// `INSERT INTO cache (dns, opt) VALUES (?, ?);`
// func logWritePrepare() throws -> OpaquePointer {
// try prepare(sql: "INSERT INTO cache (dns, opt) VALUES (?, ?);")
// }
// /// `prep` must exist and be initialized with `logWritePrepare()`
// func logWrite(_ pStmt: OpaquePointer!, _ domain: String, blocked: Bool = false) throws {
// guard let prep = pStmt else {
// return
// }
// try prepared(run: prep, bind: [BindText(domain), BindInt32(blocked ? 1 : 0)])
// }
/// `INSERT INTO cache (dns, opt) VALUES (?, ?);` /// `INSERT INTO cache (dns, opt) VALUES (?, ?);`
func logWritePrepare() throws -> OpaquePointer { func logWrite(_ domain: String, blocked: Bool = false) throws {
try prepare(sql: "INSERT INTO cache (dns, opt) VALUES (?, ?);") try self.run(sql: "INSERT INTO cache (dns, opt) VALUES (?, ?);",
} bind: [BindText(domain), BindInt32(blocked ? 1 : 0)])
/// `prep` must exist and be initialized with `logWritePrepare()` { try ifStep($0, SQLITE_DONE) }
func logWrite(_ pStmt: OpaquePointer!, _ domain: String, blocked: Bool = false) throws {
guard let prep = pStmt else {
return
}
try prepared(run: prep, bind: [BindText(domain), BindInt32(blocked ? 1 : 0)])
} }
} }
@@ -52,10 +58,10 @@ extension CreateTable {
} }
struct FilterOptions: OptionSet { struct FilterOptions: OptionSet {
let rawValue: Int32 let rawValue: Int32
static let none = FilterOptions([]) static let none = FilterOptions([])
static let blocked = FilterOptions(rawValue: 1 << 0) static let blocked = FilterOptions(rawValue: 1 << 0)
static let ignored = FilterOptions(rawValue: 1 << 1) static let ignored = FilterOptions(rawValue: 1 << 1)
static let any = FilterOptions(rawValue: 0b11) static let any = FilterOptions(rawValue: 0b11)
} }

View File

@@ -35,7 +35,7 @@ class SQLiteDatabase {
} }
deinit { deinit {
sqlite3_close(dbPointer) sqlite3_close_v2(dbPointer)
} }
static func destroyDatabase(path: String = URL.internalDB().relativePath) { static func destroyDatabase(path: String = URL.internalDB().relativePath) {
@@ -47,15 +47,10 @@ class SQLiteDatabase {
static func open(path: String = URL.internalDB().relativePath) throws -> SQLiteDatabase { static func open(path: String = URL.internalDB().relativePath) throws -> SQLiteDatabase {
var db: OpaquePointer? var db: OpaquePointer?
//sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_SHAREDCACHE, nil) if sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, nil) == SQLITE_OK {
if sqlite3_open(path, &db) == SQLITE_OK {
return SQLiteDatabase(dbPointer: db) return SQLiteDatabase(dbPointer: db)
} else { } else {
defer { defer { sqlite3_close_v2(db) }
if db != nil {
sqlite3_close(db)
}
}
if let errorPointer = sqlite3_errmsg(db) { if let errorPointer = sqlite3_errmsg(db) {
let message = String(cString: errorPointer) let message = String(cString: errorPointer)
throw SQLiteError.OpenDatabase(message: message) throw SQLiteError.OpenDatabase(message: message)
@@ -222,6 +217,7 @@ extension SQLiteDatabase {
func prepare(sql: String) throws -> OpaquePointer { func prepare(sql: String) throws -> OpaquePointer {
var pStmt: OpaquePointer? var pStmt: OpaquePointer?
guard sqlite3_prepare_v2(dbPointer, sql, -1, &pStmt, nil) == SQLITE_OK, let S = pStmt else { guard sqlite3_prepare_v2(dbPointer, sql, -1, &pStmt, nil) == SQLITE_OK, let S = pStmt else {
sqlite3_finalize(pStmt)
throw SQLiteError.Prepare(message: errorMessage) throw SQLiteError.Prepare(message: errorMessage)
} }
return S return S

View File

@@ -2,25 +2,22 @@ import Foundation
#if IOS_SIMULATOR #if IOS_SIMULATOR
private let db = AppDB!
private var pStmt: OpaquePointer?
class TestDataSource { class TestDataSource {
static func load() { static func load() {
QLog.Debug("SQLite path: \(URL.internalDB())") QLog.Debug("SQLite path: \(URL.internalDB())")
let db = AppDB!
let deleted = db.dnsLogsDelete("test.com", strict: false) let deleted = db.dnsLogsDelete("test.com", strict: false)
try? db.run(sql: "DELETE FROM cache;") try? db.run(sql: "DELETE FROM cache;")
QLog.Debug("Deleting \(deleted) rows matching 'test.com' (+ \(db.numberOfChanges) in cache)") QLog.Debug("Deleting \(deleted) rows matching 'test.com' (+ \(db.numberOfChanges) in cache)")
QLog.Debug("Writing 33 test logs") QLog.Debug("Writing 33 test logs")
pStmt = try! db.logWritePrepare() try? db.logWrite("keeptest.com", blocked: false)
try? db.logWrite(pStmt, "keeptest.com", blocked: false) for _ in 1...4 { try? db.logWrite("test.com", blocked: false) }
for _ in 1...4 { try? db.logWrite(pStmt, "test.com", blocked: false) } for _ in 1...7 { try? db.logWrite("i.test.com", blocked: false) }
for _ in 1...7 { try? db.logWrite(pStmt, "i.test.com", blocked: false) } for i in 1...8 { try? db.logWrite("b.test.com", blocked: i>5) }
for i in 1...8 { try? db.logWrite(pStmt, "b.test.com", blocked: i>5) } for i in 1...13 { try? db.logWrite("bi.test.com", blocked: i%2==0) }
for i in 1...13 { try? db.logWrite(pStmt, "bi.test.com", blocked: i%2==0) }
db.dnsLogsPersist() db.dnsLogsPersist()
@@ -36,7 +33,7 @@ class TestDataSource {
@objc static func insertRandom() { @objc static func insertRandom() {
//QLog.Debug("Inserting 1 periodic log entry") //QLog.Debug("Inserting 1 periodic log entry")
try? db.logWrite(pStmt, "\(arc4random() % 5).count.test.com", blocked: true) try? AppDB?.logWrite("\(arc4random() % 5).count.test.com", blocked: true)
} }
} }
#endif #endif