extraction.go
2.3 KB
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
package main
import (
"io"
"io/ioutil"
"net/http"
"github.com/jmoiron/sqlx"
)
func (app *App) PostExtractions(w http.ResponseWriter, r *http.Request) {
userNo := r.Context().Value(PropUserNo).(uint64)
r.ParseMultipartForm(32 << 20)
form, _, err := r.FormFile("file")
if err != nil {
WriteError(w, http.StatusInternalServerError, "Unknown error")
return
}
defer form.Close()
file, err := ioutil.TempFile("", "db")
if err != nil {
WriteError(w, http.StatusInternalServerError, "Unknown error")
return
}
defer file.Close()
_, err = io.Copy(file, form)
if err != nil {
WriteError(w, http.StatusInternalServerError, "Unknown error")
return
}
db, err := sqlx.Connect("sqlite3", file.Name())
if err != nil {
WriteError(w, http.StatusInternalServerError, "Could not open db file")
return
}
defer db.Close()
tx, err := app.db.Beginx()
if err != nil {
WriteError(w, http.StatusInternalServerError, "Unknown error")
return
}
res, _ := tx.Exec("INSERT INTO extractions (`owner`) VALUES (?)", userNo)
extNo, _ := res.LastInsertId()
rows, err := db.Queryx("SELECT * FROM calllog")
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
sql := `SELECT
a.packagename, a.name, a.version, a.wifiusage, a.cellularusage,
u.lasttimeused, u.totaltimeforeground
FROM AppInfo a JOIN AppUsageYear u ON a.packagename=u.packagename`
rows, err = db.Queryx(sql)
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO apps VALUES (?, ?, ?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
rows, err = db.Queryx("SELECT mid, type, address, body, date FROM sms")
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO messages VALUES (?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
rows, err = db.Queryx("SELECT PID, UID, PPID, STIME, TIME, CMD FROM process")
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO processes VALUES (?, ?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
tx.Commit()
w.Write([]byte("success"))
}