-
Notifications
You must be signed in to change notification settings - Fork 3
/
data-editor.js
275 lines (271 loc) · 9.87 KB
/
data-editor.js
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
const fs = require('fs')
const uuid = require('uuid')
const http = require('http')
const sqlite3 = require('sqlite3').verbose()
const CODECHARS = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789').split('')
class DataEditor {
constructor() {
this.authTokens = []
if(fs.existsSync('./appdata.db')) {
this.db = new sqlite3.Database('./appdata.db', err => {
if(err) {
console.log(`ERROR Opening Existing Database: ${err}`)
}
console.log('SUCCESS: Connected to database appdata.db')
})
} else {
this.db = new sqlite3.Database('./appdata.db', err => {
if(err) {
console.log(`ERROR Creating Database: ${err}`)
}
console.log('SUCCESS: Created and connected to database')
})
this.db.run(fs.readFileSync('./sql/create-user-table.sql', 'utf-8'))
this.db.run(fs.readFileSync('./sql/create-link-table.sql', 'utf-8'))
this.db.run(fs.readFileSync('./sql/create-click-table.sql', 'utf-8'))
}
}
closeDB() {
this.db.close(err => {
if(err) {
console.log(`ERROR Closing Database: ${err}`)
}
console.log('Database connection closed')
})
}
save() {
fs.writeFile(this.dataFile, JSON.stringify(this.data), err => {
if(err) console.log(err)
})
}
createUser(username, email, password) {
this.db.run(
fs.readFileSync('./sql/insert-user.sql', 'utf-8'),
[username, password, email]
)
//this.save()
}
validateNewUsername(username) {
return new Promise((resolve, reject) => {
this.db.get(fs.readFileSync('./sql/select-user-duplicate-name.sql', 'utf-8'), [username], (err, row) => {
resolve(!!row)
})
})
}
validateNewUserEmail(email) {
return new Promise((resolve, reject) => {
this.db.get(fs.readFileSync('./sql/select-user-duplicate-email.sql', 'utf-8'), [email], (err, row) => {
resolve(!!row)
})
})
}
validateNewUUID(id) {
return !this.authTokens.some(token => token.id == id)
}
generateNewUUID() {
let id = uuid.v4()
if(!this.validateNewUUID(id)) {
return this.generateNewUUID()
}
return id
}
generateAuthToken(username) {
let id = this.generateNewUUID()
let token = {
username: username,
id: id
}
this.authTokens.push(token)
return token
}
checkCredentials(userID, passwd) {
return new Promise((resolve, reject) => {
this.db.get(fs.readFileSync('./sql/select-user-for-auth.sql', 'utf-8'), [userID, passwd], (err, row) => {
resolve(!!row ? this.generateAuthToken(row.username) : false)
})
})
}
async checkAuthToken(tokenStr) {
let token = JSON.parse(tokenStr)
if(this.authTokens.filter(t => t.username==token.username && t.id==token.id).length > 0) {
return await this.getUser(token.username)
}
return false
}
async getUser(userID) {
return new Promise(async (resolve, reject) => {
let user = {}
this.db.get(fs.readFileSync('./sql/select-user-full.sql', 'utf-8'), [userID, userID], async (err, row) => {
if(row) {
user.username = row.username
user.email = row.email
user.passwd = row.passwd
user.links = await this.getLinksByUser(user.username)
resolve(user)
}
})
})
}
async getLinksByUser(username) {
return new Promise((resolve, reject) => {
this.db.all(fs.readFileSync('./sql/select-links-for-user.sql', 'utf-8'), [username], (err, rows) => {
resolve(rows.map(row => row.trackingID))
})
})
}
async getLinkByTrackingID(linkID) {
return new Promise((resolve, reject) => {
this.db.get(fs.readFileSync('./sql/select-link-by-tracking.sql', 'utf-8'), [linkID], async (err, row) => {
if(!!row) {
let linkData = {
trackingID: row.trackingID,
redirectID: row.redirectID,
targetURL: row.targetURL,
notes: row.notes,
clicks: await this.getClicksForLink(linkID)
}
resolve(linkData)
} else {
resolve(false)
}
})
})
}
async getClickCount(linkID) {
return new Promise((resolve, reject) => {
this.db.all(fs.readFileSync('./sql/select-click-by-link.sql', 'utf-8'), [linkID], (err, rows) => {
resolve(!!rows ? rows.length : 0)
})
})
}
getClicksForLink(linkID) {
return new Promise((resolve, reject) => {
let clicks = []
this.db.all(fs.readFileSync('./sql/select-click-by-link.sql', 'utf-8'), [linkID], (err, rows) => {
rows.forEach(row => {
clicks.push({
date: row.clickDate,
ip: row.ip,
userAgent: row.userAgent,
os: row.os,
client: row.client,
device: row.device,
location: row.clickLocation,
isp: row.isp,
organization: row.organization,
asn: row.asn,
mobile: row.mobile,
proxy: row.proxy,
hosting: row.hosting
})
})
resolve(clicks)
})
})
}
async getLinkByRedirectID(linkID) {
return new Promise((resolve, reject) => {
this.db.get(fs.readFileSync('./sql/select-link-by-redirect.sql', 'utf-8'), [linkID], async (err, row) => {
if(!!row) {
resolve({
trackingID: row.trackingID,
redirectID: row.redirectID,
targetURL: row.targetURL,
notes: row.notes,
ownerID: row.ownerID,
clicks: await this.getClicksForLink(linkID)
})
} else {
resolve(false)
}
})
})
}
async newTrackingID() {
let newID = ''
for(let i = 0; i < 6; i++) {
let n = CODECHARS[Math.floor(Math.random()*CODECHARS.length)]
newID += n
}
let duplicate = await this.getLinkByTrackingID(newID)
while(duplicate) {
for(let i = 0; i < 6; i++) {
let n = CODECHARS[Math.floor(Math.random()*CODECHARS.length)]
newID += n
}
duplicate = await this.getLinkByTrackingID(newID)
}
return newID
}
async newRedirectID() {
let newID = ''
for(let i = 0; i < 6; i++) {
let n = CODECHARS[Math.floor(Math.random()*CODECHARS.length)]
newID += n
}
let duplicate = await this.getLinkByRedirectID(newID)
while(duplicate) {
for(let i = 0; i < 6; i++) {
let n = CODECHARS[Math.floor(Math.random()*CODECHARS.length)]
newID += n
}
duplicate = await this.getLinkByRedirectID(newID)
}
return newID
}
async createLink(username, targetURL, note) {
if(targetURL.indexOf('http://') != 0 && targetURL.indexOf('https://') != 0) {
targetURL = 'http://' + targetURL
}
let newTrackingID = await this.newTrackingID()
let newRedirectID = await this.newRedirectID()
let newLink = {
trackingID: newTrackingID,
redirectID: newRedirectID,
targetURL: targetURL,
note: note,
clicks: []
}
await this.db.run(
fs.readFileSync('./sql/insert-link.sql', 'utf-8'),
[newTrackingID, newRedirectID, targetURL, username, note]
)
return newLink
}
async addClick(linkID, click) {
let ipData = await this.getIPData(click.ip)
await this.db.run(
fs.readFileSync('./sql/insert-click.sql', 'utf-8'),
[
click.date, click.ip, click.userAgent, click.os, click.client, click.device,
ipData.location, ipData.isp, ipData.organization, ipData.asn, ipData.mobile, ipData.proxy, ipData.hosting,
linkID
]
)
}
getIPData(ip) {
return new Promise((resolve, reject) => {
http.get(`http://ip-api.com/json/${ip}?fields=status,message,city,regionName,country,isp,org,as,mobile,proxy,hosting`, res => {
let data = ''
res.on('data', chunk => {
data += chunk
})
res.on('end', () => {
let dataObj = JSON.parse(data)
resolve({
location: dataObj.city + ', ' + dataObj.regionName + ', ' + dataObj.country,
isp: dataObj.isp,
organization: dataObj.org,
asn: dataObj.as,
mobile: dataObj.mobile ? "yes" : "no",
proxy: dataObj.proxy ? "yes" : "no",
hosting: dataObj.hosting ? "yes" : "no"
})
})
}).on('error', err => {
console.log(err)
})
})
}
}
module.exports = DataEditor