-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
311 lines (275 loc) · 8.52 KB
/
main.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"crypto/md5"
"database/sql"
"encoding/hex"
"flag"
"fmt"
"log"
"net"
"net/http"
"time"
_ "github.com/mattn/go-sqlite3" // Import go-sqlite3 library
"github.com/oschwald/maxminddb-golang"
)
// Define a global variable for the MaxMind database
var maxMindDB *maxminddb.Reader
// initMaxMindDB loads the MaxMind database into memory
func initMaxMindDB(filePath string) {
var err error
maxMindDB, err = maxminddb.Open(filePath)
if err != nil {
log.Fatal("Error loading MaxMind database: ", err)
}
}
// maxMindLookup performs a lookup of the given IP address in the MaxMind database
func maxMindLookup(ipAddress string) (map[string]interface{}, error) {
ip := net.ParseIP(ipAddress)
var record map[string]interface{}
err := maxMindDB.Lookup(ip, &record)
if err != nil {
return nil, err
}
return record, nil
}
// Global database instance
var db *sql.DB
// Initialize the SQLite database
func initDB(filepath string) {
var err error
db, err = sql.Open("sqlite3", filepath)
if err != nil {
log.Fatal(err)
}
if db == nil {
log.Fatal("DB nil")
}
createTables(db)
// Consider adding db.Ping() here to check for database liveliness
}
// Create tables if they do not already exist
func createTables(db *sql.DB) {
// SQL statement for creating new tables
sqlTable := `
CREATE TABLE IF NOT EXISTS log(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
IPAddress TEXT,
UserAgent TEXT,
Referrer TEXT,
PageURL TEXT,
GeoCity TEXT,
GeoState TEXT,
GeoCountry TEXT
);
CREATE TABLE IF NOT EXISTS aggregated_analytics(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Datestamp DATE DEFAULT (date('now')),
PageViews INTEGER,
Bounces INTEGER,
Referrer TEXT,
PageURL TEXT,
Geolocation TEXT,
UNIQUE(Datestamp, PageURL)
);
CREATE TABLE IF NOT EXISTS aggregated_referrals(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Datestamp DATE DEFAULT (date('now')),
ReferralCount INTEGER,
ReferralURL TEXT,
ReferralTLD TEXT,
PageURL TEXT,
Geolocation TEXT,
UNIQUE(Datestamp, PageURL)
);
CREATE TABLE IF NOT EXISTS aggregated_geolocation(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Datestamp DATE DEFAULT (date('now')),
GeoCity TEXT,
GeoState TEXT,
GeoCountry TEXT,
PageViews INTEGER,
PageURL TEXT, -- Marked for unique combination
UNIQUE(Datestamp, GeoCity, GeoState, GeoCountry, PageURL)
);
`
_, err := db.Exec(sqlTable)
if err != nil {
log.Fatal(err)
}
}
func logRequest(data map[string]string) {
ipAddress := data["IPAddress"]
host, _, err := net.SplitHostPort(ipAddress)
if err != nil {
// If there's an error, it might be because there's no port number, so use the original IP address
host = ipAddress
}
userAgent := data["UserAgent"]
referrer := data["Referrer"]
pageURL := data["PageURL"]
// Perform a MaxMind lookup
geoData, err := maxMindLookup(host)
if err != nil {
log.Printf("Error performing MaxMind lookup: %v", err)
// Handle the error (e.g., proceed with logging without geolocation data)
}
fmt.Printf("%+v\n", geoData)
// Initialize geolocation information variables
var geoCity, geoState, geoCountry string
// Safely extract geolocation information using type assertions with checks
if city, ok := geoData["city"].(map[string]interface{}); ok {
if names, ok := city["names"].(map[string]interface{}); ok {
geoCity, _ = names["en"].(string)
}
}
if subdivisions, ok := geoData["subdivisions"].([]interface{}); ok && len(subdivisions) > 0 {
if subdivision, ok := subdivisions[0].(map[string]interface{}); ok {
if names, ok := subdivision["names"].(map[string]interface{}); ok {
geoState, _ = names["en"].(string)
}
}
}
if country, ok := geoData["country"].(map[string]interface{}); ok {
if names, ok := country["names"].(map[string]interface{}); ok {
geoCountry, _ = names["en"].(string)
}
}
// Hash the IP address
hasher := md5.New()
hasher.Write([]byte(host))
hashedIPAddress := hex.EncodeToString(hasher.Sum(nil))
// Prepare and execute the SQL statement
stmt, err := db.Prepare("INSERT INTO log(IPAddress, UserAgent, Referrer, PageURL, GeoCity, GeoState, GeoCountry) VALUES(?,?,?,?,?,?,?)")
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec(hashedIPAddress, userAgent, referrer, pageURL, geoCity, geoState, geoCountry)
if err != nil {
log.Fatal(err)
}
}
func aggregateData() {
// Check for any log entries from yesterday or earlier
var exists bool
err := db.QueryRow("SELECT EXISTS(SELECT 1 FROM log WHERE DATE(Timestamp) < DATE('now'))").Scan(&exists)
if err != nil {
log.Fatalf("Error checking for log entries: %v", err)
}
if !exists {
log.Println("No log entries from yesterday or earlier. Skipping aggregation.")
return // Exit the function early
}
// Aggregate PageViews and Bounces per PageURL per Datestamp
queryPageViewsAndBounces := `
INSERT INTO aggregated_analytics (Datestamp, PageViews, Bounces, PageURL)
SELECT
DATE(Timestamp) AS Datestamp,
COUNT(*) AS PageViews,
SUM(CASE WHEN VisitCount = 1 THEN 1 ELSE 0 END) AS Bounces,
PageURL
FROM (
SELECT
PageURL,
IPAddress,
COUNT(*) AS VisitCount
FROM log
WHERE DATE(Timestamp) < DATE('now')
GROUP BY PageURL, IPAddress
) AS PageVisits
GROUP BY Datestamp, PageURL
ON CONFLICT(Datestamp, PageURL) DO UPDATE SET
PageViews = EXCLUDED.PageViews,
Bounces = EXCLUDED.Bounces;
`
err = executeQuery(db, queryPageViewsAndBounces)
if err != nil {
log.Fatal(err)
}
// Aggregate Referral data
queryReferrals := `
INSERT INTO aggregated_referrals (Datestamp, ReferralCount, ReferralURL, PageURL)
SELECT
DATE(Timestamp) AS Datestamp,
COUNT(*) AS ReferralCount,
Referrer AS ReferralURL,
PageURL
FROM log
WHERE DATE(Timestamp) < DATE('now') AND Referrer != ''
GROUP BY Datestamp, ReferralURL, PageURL
ON CONFLICT(Datestamp, PageURL) DO UPDATE SET
ReferralCount = EXCLUDED.ReferralCount;
`
err = executeQuery(db, queryReferrals)
if err != nil {
log.Fatal(err)
}
// Aggregate Geolocation data
queryGeolocation := `
INSERT INTO aggregated_geolocation (Datestamp, GeoCity, GeoState, GeoCountry, PageViews, PageURL)
SELECT
DATE(Timestamp) AS Datestamp,
GeoCity,
GeoState,
GeoCountry,
COUNT(*) AS PageViews,
PageURL
FROM log
WHERE DATE(Timestamp) < DATE('now')
GROUP BY Datestamp, GeoCity, GeoState, GeoCountry, PageURL
ON CONFLICT(Datestamp, GeoCity, GeoState, GeoCountry, PageURL) DO UPDATE SET
PageViews = EXCLUDED.PageViews;
`
err = executeQuery(db, queryGeolocation)
if err != nil {
log.Fatal(err)
}
deleteQuery := "DELETE FROM log WHERE DATE(Timestamp) < DATE('now');"
err = executeQuery(db, deleteQuery)
if err != nil {
log.Fatalf("Error deleting aggregated log entries: %v", err)
}
}
func checkAndAggregate() {
for range time.Tick(time.Hour) {
aggregateData()
}
}
func executeQuery(db *sql.DB, query string) error {
_, err := db.Exec(query)
return err
}
func main() {
// Initialize and connect to the SQLite database
dbPath := flag.String("db", "ninja.db", "path to the sqlite database file")
domainName := flag.String("domain", "localhost", "The domain name of the application")
port := flag.String("port", "8080", "The port on which the application will run")
maxMindDBFile := flag.String("maxMindDB", "GeoLite2-City.mmdb", "MaxMind GeoLite2-City.mmdb DB File Path")
flag.Parse()
// Initialize the database & maxmind
initDB(*dbPath)
initMaxMindDB(*maxMindDBFile)
go checkAndAggregate()
aggregateData()
// Setup HTTP routes
http.HandleFunc("/pixel.gif", servePixel)
// Handler for serving the JavaScript
http.HandleFunc("/track.js", func(w http.ResponseWriter, r *http.Request) {
// Pass domainName and port to serveJavaScript
serveJavaScript(*domainName, *port, w)
})
// Handler for collecting analytics data
http.HandleFunc("/collect", collectHandler)
http.HandleFunc("/api/stats/today", serveTodayStats)
http.HandleFunc("/api/stats/historical", serveHistoricalStats)
address := *domainName // Start with just the domain name
if *port != "80" { // Append the port if it's not the default HTTP port
address = fmt.Sprintf("%s:%s", *domainName, *port)
}
// Start the HTTP server
log.Printf("Server starting on %s", address)
if err := http.ListenAndServe(":"+*port, nil); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
select {}
}