-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
188 lines (161 loc) · 4.02 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
package main
import (
"fmt"
"os"
"runtime/debug"
"time"
e "github.com/britannic/blacklist/internal/edgeos"
)
var (
// updated by go build -ldflags
architecture = "UNKNOWN"
build = "UNKNOWN"
githash = "UNKNOWN"
hostOS = "UNKNOWN"
version = "UNKNOWN"
// ----------------------------
exitCmd = os.Exit
initEnvirons = initEnv
prog = basename(os.Args[0])
prefix = fmt.Sprintf("%s: ", prog)
bkpCfgFile = "/config/user-data/blacklist.failover.cfg"
stdCfgFile = "/config/config.boot"
)
// Hack to reduce memory usage in Go 1.17
func init() {
go func() {
t := time.Tick(time.Second)
for {
<-t
debug.FreeOSMemory()
}
}()
}
func main() {
// Memory profiling
// defer profile.Start(profile.MemProfile).Stop()
objex := []e.IFace{
e.PreRObj,
e.PreDObj,
e.PreHObj,
e.ExRtObj,
e.ExDmObj,
e.ExHtObj,
e.FileObj,
e.URLdObj,
e.URLhObj,
}
if os.Geteuid() != 0 {
fmt.Printf("%s must be run as sudo\n", prog)
logErrorf("%s must be run as sudo", prog)
exitCmd(0)
}
c, err := initEnvirons()
if err != nil {
logErrorf("Cannot continue due to error: %s", err.Error())
exitCmd(0)
}
c.Debug(fmt.Sprintf("Dumping commandline args: %v", os.Args[1:]))
c.Debug(fmt.Sprintf("Dumping env variables: %v", c))
logNoticef("%v", "Starting blacklist update...")
if !e.ChkWeb("www.google.com", 443) {
logFatalf("%s", "No internet access, aborting blacklist update!")
}
logInfo("Checking for stale blacklists...")
if err = removeStaleFiles(c); err != nil {
logFatalf("%v", err.Error())
}
// _, _ = context.WithTimeout(context.Background(), c.Timeout)
if !c.Disabled {
if err := processObjects(c, objex); err != nil {
logErrorf("%v", err.Error())
}
}
dropped, extracted, kept := c.GetTotalStats()
if kept+dropped != 0 {
c.Log.Noticef("Total entries found: %d", extracted)
c.Log.Noticef("Total entries extracted %d", kept)
c.Log.Noticef("Total entries dropped %d", dropped)
}
reloadDNS(c)
logNoticef("%v", "Blacklist update completed......")
}
// basename removes directory components and file extensions.
func basename(s string) string {
// Discard last '/' and everything before.
for i := len(s) - 1; i >= 0; i-- {
if s[i] == '/' {
s = s[i+1:]
break
}
}
// Preserve everything before last '.'
for i := len(s) - 1; i >= 0; i-- {
if s[i] == '.' {
s = s[:i]
break
}
}
return s
}
// files returns an empty *e.CFile string array
func files(c *e.Config) *e.CFile {
return &e.CFile{Names: []string{}, Env: c.Env}
}
func initEnv() (c *e.Config, err error) {
o := getOpts()
o.setArgs()
c = o.initEdgeOS()
if *o.File == "" {
if *o.Safe {
*o.File = stdCfgFile
}
if c, err = loadConfig(c, o); err != nil {
if _, err = os.Stat(bkpCfgFile); !os.IsNotExist(err) && *o.Safe {
*o.File = bkpCfgFile
}
}
}
return loadConfig(c, o)
}
func loadConfig(c *e.Config, o *opts) (*e.Config, error) {
var err error
if err = c.Blacklist(o.getCFG(c)); err != nil {
fmt.Fprintf(os.Stderr, "Removing stale dnsmasq blacklist files, because %v\n", err.Error())
if err = files(c).Remove(); err != nil {
fmt.Fprintf(os.Stderr, "%v", err.Error())
}
reloadDNS(c)
exitCmd(0)
}
return c, err
}
// processObjects processes local sources, downloads Internet sources and creates
// dnsmasq configuration files
func processObjects(c *e.Config, objects []e.IFace) error {
for _, o := range objects {
ct, err := c.NewContent(o)
if err != nil {
return err
}
if err = c.ProcessContent(ct); err != nil {
return err
}
}
return nil
}
// reloadDNS reloads the latest processed dnsmasq configuration files
func reloadDNS(c *e.Config) {
if b, err := c.ReloadDNS(); err != nil {
logErrorf("ReloadDNS(): %v\n error: %v\n", string(b), err.Error())
exitCmd(1)
}
logPrintf("%s", "Successfully restarted dnsmasq")
}
// removeStaleFiles deletes redundant files
func removeStaleFiles(c *e.Config) error {
if err := c.GetAll().Files().Remove(); err != nil {
return fmt.Errorf("problem removing stale files: %v", err.Error())
}
return nil
}