forked from segura2010/GoHole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.go
206 lines (175 loc) · 4.06 KB
/
sqlite.go
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
package logs
import (
"log"
"os/user"
"database/sql"
_ "github.com/mattn/go-sqlite3"
//"GoHole/config"
)
var instance *sql.DB = nil
type QueryLog struct {
Id int
ClientIp string
Domain string
Cached int
Timestamp int64
}
type ClientLog struct {
ClientIp string
Queries int
}
type DomainLog struct {
Domain string
Queries int
}
func GetInstance() *sql.DB {
if instance == nil {
var err error = nil
var dbPath string = ""
usr, err := user.Current()
if err != nil{
dbPath = "./gohole.db"
}else{
dbPath = usr.HomeDir + "/gohole.db"
}
log.Printf("Logs DB: %s", dbPath)
instance, err = sql.Open("sqlite3", dbPath)
if err != nil{
log.Fatal(err)
}
}
return instance
}
func SetupDB(){
sqlCmd := `
create table if not exists queries (id integer not null primary key autoincrement, clientip text, domain text, cached integer, timestamp integer);
`
_, err := GetInstance().Exec(sqlCmd)
if err != nil{
log.Fatal(err)
}
}
func AddQuery(clientip, domain string, cached int, timestamp int64) (error){
tx, err := GetInstance().Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare("INSERT INTO queries(clientip, domain, cached, timestamp) VALUES (?,?,?,?)")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(clientip, domain, cached, timestamp)
if err != nil {
return err
}
err = tx.Commit()
return err
}
func GetQueriesByClientIp(clientIp, limit string) ([]QueryLog, error){
result := []QueryLog{}
rows, err := GetInstance().Query("select id, clientip, domain, cached, timestamp from (select * from queries where clientip='"+ clientIp +"' ORDER BY timestamp DESC LIMIT "+ limit +") ORDER BY timestamp ASC")
if err != nil {
return result, err
}
defer rows.Close()
for rows.Next() {
var id, cached int
var timestamp int64
var clientip, domain string
err = rows.Scan(&id, &clientip, &domain, &cached, ×tamp)
if err != nil {
return result, err
}
l := QueryLog{
Id: id,
ClientIp: clientip,
Domain: domain,
Cached: cached,
Timestamp: timestamp,
}
result = append(result, l)
}
err = rows.Err()
return result, err
}
func GetQueriesByDomain(domain string) ([]QueryLog, error){
result := []QueryLog{}
rows, err := GetInstance().Query("select id, clientip, domain, cached, timestamp from queries where domain LIKE '%"+ domain +"%' ORDER BY timestamp ASC")
if err != nil {
return result, err
}
defer rows.Close()
for rows.Next() {
var id, cached int
var timestamp int64
var clientip, domain string
err = rows.Scan(&id, &clientip, &domain, &cached, ×tamp)
if err != nil {
return result, err
}
l := QueryLog{
Id: id,
ClientIp: clientip,
Domain: domain,
Cached: cached,
Timestamp: timestamp,
}
result = append(result, l)
}
err = rows.Err()
return result, err
}
func GetClients() ([]ClientLog, error){
result := []ClientLog{}
rows, err := GetInstance().Query("select clientip, count(*) AS numqueries from queries GROUP BY clientip ORDER BY numqueries DESC")
if err != nil {
return result, err
}
defer rows.Close()
for rows.Next() {
var clientip string
var queries int
err = rows.Scan(&clientip, &queries)
if err != nil {
return result, err
}
c := ClientLog{
ClientIp: clientip,
Queries: queries,
}
result = append(result, c)
}
err = rows.Err()
return result, err
}
func GetTopDomains(limit string) ([]DomainLog, error){
result := []DomainLog{}
rows, err := GetInstance().Query("select domain, count(*) AS numqueries from queries GROUP BY domain ORDER BY numqueries DESC LIMIT "+ limit)
if err != nil {
return result, err
}
defer rows.Close()
for rows.Next() {
var domain string
var queries int
err = rows.Scan(&domain, &queries)
if err != nil {
return result, err
}
c := DomainLog{
Domain: domain,
Queries: queries,
}
result = append(result, c)
}
err = rows.Err()
return result, err
}
func Flush() (error){
sqlCmd := `
delete from queries;
`
_, err := GetInstance().Exec(sqlCmd)
return err
}