summaryrefslogtreecommitdiff
path: root/cmd/main.go
blob: df872f86eda59c820ca2905b5c2a87f366aac9bf (plain) (blame)
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
package main

import (
	"context"
	"log"
	"net"
	"os"
	"os/signal"
	"syscall"

	"github.com/jackc/pgx/v5"
	"sinanmohd.com/redq/api"
	"sinanmohd.com/redq/db"
	"sinanmohd.com/redq/dns"
	"sinanmohd.com/redq/bpf/usage"
	"sinanmohd.com/redq/bpf/filter"
)

func main() {
	iface, err := net.InterfaceByName("wlan0")
	if err != nil {
		log.Fatalf("lookup network: %s", err)
	}

	ctx := context.Background()
	conn, err := pgx.Connect(ctx, "user=redq_ebpf dbname=redq_ebpf")
	if err != nil {
		log.Fatalf("connecting database: %s", err)
	}
	defer conn.Close(ctx)
	queries := db.New(conn)

	d, err := dns.New(queries, ctx)
	if err != nil {
		os.Exit(0)
	}
	a, err := api.New()
	if err != nil {
		os.Exit(0)
	}
	f, err := filter.New(iface, queries, ctx)
	if err != nil {
		os.Exit(0)
	}
	u, err := usage.New(iface)
	if err != nil {
		os.Exit(0)
	}

	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, os.Interrupt, os.Kill, syscall.SIGTERM)
	go func() {
		<-sigs
		usage.Close(u, queries, ctx)
		filter.Close(f)
		api.Close(a)
		os.Exit(0)
	}()

	go u.Run(iface, queries, ctx)
	go d.Run()

	a.Run(u, d, f, queries, ctx)
}