extraction.go
4.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
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
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"github.com/dgrijalva/jwt-go"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo/v4"
_ "github.com/mattn/go-sqlite3"
)
func (app *App) GetExtractions(c echo.Context) error {
userNo := c.Get("user").(*jwt.Token).Claims.(*AuthClaims).UserNo
phone := c.QueryParam("phone")
extractions := []uint64{}
var err error
if phone == "" {
query := "SELECT `no` FROM extractions WHERE `owner`=?"
err = app.db.Select(&extractions, query, userNo)
} else {
query := "SELECT `no` FROM extractions WHERE `owner`=? AND `phone`=?"
err = app.db.Select(&extractions, query, userNo, phone)
}
if err != nil {
return err
}
return c.JSON(http.StatusOK, extractions)
}
func (app *App) PostExtractions(c echo.Context) error {
userNo := c.Get("user").(*jwt.Token).Claims.(*AuthClaims).UserNo
form, err := c.FormFile("file")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unknown error")
}
src, err := form.Open()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unknown error")
}
defer src.Close()
file, err := ioutil.TempFile("", "extraction")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unknown error")
}
defer os.Remove(file.Name())
if _, err := io.Copy(file, src); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unknown error")
}
file.Close()
db, err := sqlx.Connect("sqlite3", file.Name())
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not open db file")
}
defer db.Close()
tx, err := app.db.Beginx()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unknown error")
}
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()
_, err = 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 {
idxName := fmt.Sprintf("messages-%d", extNo)
app.es.CreateIndex(idxName).Body(`{
"settings": {
"analysis": {
"analyzer": {
"default": {
"type": "custom",
"tokenizer": "nori_tokenizer"
}
}
}
}
}`).Do(context.Background())
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO messages VALUES (?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
app.es.Index().Index(idxName).Id(fmt.Sprint(vals[0])).BodyJson(echo.Map{"content": string(vals[3].([]byte))}).Do(context.Background())
}
}
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...)...)
}
}
/*alarms := map[string]Alarm{}
rows, _ = db.Queryx("SELECT * FROM alarm ORDER BY TIME")
for rows.Next() {
var tm string
var typ string
var detail string
rows.Scan(&tm, &typ, &detail)
detail = detail[strings.Index(detail, "{")+1 : strings.Index(detail, "}")]
s := strings.Split(detail, " ")
timestamp, _ := strconv.ParseInt(s[4], 10, 64)
timestamp /= 1000
if _, ok := alarms[s[0]]; !ok {
alarms[s[0]] = Alarm{ID: s[0], When: time.Unix(timestamp, 0)}
}
when, _ := time.Parse("2006-01-02 15:04:05", tm)
alarm := alarms[s[0]]
alarm.History = append(alarms[s[0]].History, AlarmHistory{
Type: typ,
When: when,
})
alarms[s[0]] = alarm
}
for _, v := range alarms {
tx.Exec("INSERT INTO alarms VALUES (?, ?, ?)", extNo, v.ID, v.When)
for _, h := range v.History {
tx.Exec("INSERT INTO alarm_histories VALUES (?, ?, ?, ?)", extNo, v.ID, h.Type, h.When)
}
}*/
tx.Commit()
return c.NoContent(http.StatusNoContent)
}