This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (119 loc) · 3.64 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
package main
import (
"database/sql"
"embed"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/golang-migrate/migrate/v4"
sq "github.com/golang-migrate/migrate/v4/database/sqlite"
"github.com/golang-migrate/migrate/v4/source/httpfs"
"github.com/mfuentesg/localdns/handler"
"github.com/mfuentesg/localdns/server/dns"
"github.com/mfuentesg/localdns/server/grpc"
"github.com/mfuentesg/localdns/storage/sqlite"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
//go:embed migrations
var migrations embed.FS
func loadConfig() error {
viper.AddConfigPath(".")
viper.SetConfigName(".localdns")
viper.SetConfigType("yaml")
viper.SetDefault("remote_server", "8.8.8.8:53")
viper.SetDefault("servers.dns_udp.addr", ":8053")
viper.SetDefault("servers.dns_udp.enabled", true)
viper.SetDefault("servers.dns_tcp.addr", ":8053")
viper.SetDefault("servers.dns_tcp.enabled", true)
viper.SetDefault("servers.grpc.addr", ":8080")
viper.SetDefault("servers.grpc.enabled", true)
viper.SetDefault("servers.prometheus.addr", ":9090")
viper.SetDefault("servers.prometheus.enabled", true)
viper.SetDefault("database.dsn", "localdns.db")
_ = viper.ReadInConfig()
_ = viper.MergeInConfig()
return viper.WriteConfigAs(".localdns.yaml")
}
func applyMigrations() error {
db, err := sql.Open("sqlite", viper.GetString("database.dsn"))
if err != nil {
return err
}
sourceDriver, err := httpfs.New(http.FS(migrations), "migrations")
if err != nil {
return err
}
target, err := sq.WithInstance(db, new(sq.Config))
if err != nil {
return err
}
m, err := migrate.NewWithInstance(
"httpfs", sourceDriver, "sqlite", target)
if err != nil {
return err
}
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
return err
}
defer db.Close()
return sourceDriver.Close()
}
func init() {
log.SetFormatter(new(log.JSONFormatter))
log.SetLevel(log.InfoLevel)
if err := loadConfig(); err != nil {
log.WithField("reason", err).Fatal("unable to read the config file")
}
if err := applyMigrations(); err != nil {
log.WithField("reason", err).Fatal("unable to apply migrations")
}
}
func main() {
db, err := sqlite.New(viper.GetString("database.dsn"))
if err != nil {
log.WithField("reason", err).Fatal("unable to load db")
}
defer db.Close()
errs := make(chan error, 2)
udpEnabled := viper.GetBool("servers.dns_udp.enabled")
tcpEnabled := viper.GetBool("servers.dns_tcp.enabled")
grpcEnabled := viper.GetBool("servers.grpc.enabled")
if udpEnabled {
go func() {
h := handler.New(db, handler.WithDNSServer(viper.GetString("remote_server")))
s := dns.New(h, dns.WithAddr(viper.GetString("servers.dns_udp.addr")))
log.WithFields(log.Fields{"protocol": "udp", "addr": s.Addr}).Info("dns server started")
errs <- s.ListenAndServe()
}()
}
if tcpEnabled {
go func() {
protocol := "tcp"
h := handler.New(db, handler.WithDNSServer(viper.GetString("remote_server")), handler.WithProtocol(protocol))
s := dns.New(h, dns.WithAddr(viper.GetString("servers.dns_tcp.addr")), dns.WithProtocol(protocol))
log.WithFields(log.Fields{"protocol": protocol, "addr": s.Addr}).Info("dns server started")
errs <- s.ListenAndServe()
}()
}
if grpcEnabled {
go func() {
s := grpc.New(db, grpc.WithAddr(viper.GetString("servers.grpc.addr")))
log.WithField("addr", s.Addr).Info("grpc server started")
errs <- s.ListenAndServe()
}()
}
if grpcEnabled || udpEnabled || tcpEnabled {
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()
log.Errorf("localdns: service %s", <-errs)
} else {
log.Error("there are not enabled services in config file")
}
}