-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (61 loc) · 1.44 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/mrWinston/dnsotlsproxy/listeners"
"github.com/mrWinston/dnsotlsproxy/resolver"
"github.com/sirupsen/logrus"
)
var shutdown = make(chan bool, 1)
var osSig = make(chan os.Signal, 1)
func handleOsSignal() {
sig := <-osSig
logrus.Info("Received ", sig)
logrus.Info("Shutting Down")
shutdown <- true
}
func main() {
signal.Notify(osSig, syscall.SIGINT, syscall.SIGTERM)
go handleOsSignal()
settings := getSettingsFromEnvVars()
logrus.SetLevel(settings.LogLevel)
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
logrus.Info(fmt.Sprintf("Starting DnsProxy with these settings: %v", settings))
dnsUpstream := resolver.Resolver{
RemoteIp: settings.DnsUpstreamAddress,
RemotePort: settings.DnsUpstreamPort,
}
udpListener, err := listeners.NewUdpListener(
settings.UdpAddress,
settings.UdpPort,
&dnsUpstream,
)
if err != nil {
logError(err, "Error creating UDP Listener")
return
}
logrus.Info("Started UDP Listener")
tcpListener, err := listeners.NewTcpListener(
settings.TcpAddress,
settings.TcpPort,
&dnsUpstream,
)
if err != nil {
logError(err, "Error creating TCP Listener")
udpListener.Shutdown()
return
}
logrus.Info("Started TCP Listener")
defer tcpListener.Shutdown()
defer udpListener.Shutdown()
<-shutdown
}
func logError(err error, msg string) {
logrus.WithFields(logrus.Fields{
"error": err,
}).Error(msg)
}