-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (74 loc) · 2.1 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
package main
import (
"bufio"
"dns-proxy/pkg/domain/proxy"
"dns-proxy/pkg/gateway/blocker"
"dns-proxy/pkg/gateway/cache"
"dns-proxy/pkg/gateway/resolver"
"dns-proxy/pkg/helpers"
"dns-proxy/pkg/presenter/socket"
"log"
"os"
"time"
"github.com/sevlyar/go-daemon"
)
func main() {
if isNotRunningInDockerContainer() {
cntxt := goDaemon("/tmp/dns-proxy.pid", "/tmp", "/tmp/dns-proxy.log", []string{"dns-proxy"})
d, err := cntxt.Reborn()
if err != nil {
log.Fatal("Unable to run: ", err)
}
if d != nil {
return
}
defer cntxt.Release()
}
config := GetConfig()
cache := cache.NewMemoryCache(time.Duration(config.CACHE_TLL) * time.Second)
go cache.AutoPurge()
resolver := resolver.NewDNSOverTlsResolver("1.1.1.1", 853, config.RESOLVER_READ_TO)
parser := helpers.NewMsgParser()
var sources []string
sources, _ = source("/Users/lcalisi/dns-proxy/blocker/lists.list")
blocker := blocker.NewBlocker(time.Duration(10)*time.Minute, sources)
go blocker.Update()
proxy := proxy.NewDNSProxy(resolver, parser, cache, blocker)
go socket.StarUDPtServer(proxy, config.UDP_PORT, "0.0.0.0")
socket.StartTCPServer(proxy, config.TCP_PORT, "0.0.0.0", config.TCP_DIRECT, config.TCP_MAX_CONN_POOL)
}
func goDaemon(pidName string, workDir string, logFile string, args []string) *daemon.Context {
ctx := &daemon.Context{
PidFileName: pidName,
PidFilePerm: 0644,
LogFileName: logFile,
LogFilePerm: 0640,
WorkDir: workDir,
Umask: 027,
}
return ctx
}
func isNotRunningInDockerContainer() bool {
// docker creates a .dockerenv file at the root
// of the directory tree inside the container.
// if this file exists then the viewer is running
// from inside a container so return true
if _, err := os.Stat("/.dockerenv"); err == nil {
return false
}
return true
}
func source(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
log.Fatalf("failed to open source list")
}
scanner := bufio.NewScanner(file)
var sources []string
for scanner.Scan() {
if scanner.Text()[:1] != "#" {
sources = append(sources, scanner.Text())
}
}
return sources, nil
}