-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockd.go
154 lines (126 loc) · 3.21 KB
/
blockd.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
// SPDX-FileCopyrightText: 2020 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package rescached
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"time"
)
const (
lastUpdatedFormat = "2006-01-02 15:04:05 MST"
)
// Blockd define the container for each block.d section in configuration.
type Blockd struct {
lastUpdated time.Time
Name string `ini:"::name"` // Derived from hostname in URL.
URL string `ini:"::url"`
file string
fileDisabled string
LastUpdated string
IsEnabled bool // True if the hosts file un-hidden in block.d directory.
isFileExist bool // True if the file exist and enabled or disabled.
}
// disable the hosts block by prefixing the file name with single dot.
func (hb *Blockd) disable() (err error) {
err = os.Rename(hb.file, hb.fileDisabled)
if err != nil {
return fmt.Errorf("disable: %w", err)
}
hb.IsEnabled = false
return nil
}
// enable the hosts block file by removing the dot prefix from file name.
func (hb *Blockd) enable() (err error) {
if hb.isFileExist {
err = os.Rename(hb.fileDisabled, hb.file)
} else {
err = os.WriteFile(hb.file, []byte(""), 0600)
}
if err != nil {
return fmt.Errorf("enable: %w", err)
}
hb.IsEnabled = true
hb.isFileExist = true
return nil
}
func (hb *Blockd) init(pathDirBlock string) {
var (
fi os.FileInfo
err error
)
hb.file = filepath.Join(pathDirBlock, hb.Name)
hb.fileDisabled = filepath.Join(pathDirBlock, "."+hb.Name)
fi, err = os.Stat(hb.file)
if err != nil {
hb.IsEnabled = false
fi, err = os.Stat(hb.fileDisabled)
if err != nil {
return
}
hb.isFileExist = true
} else {
hb.IsEnabled = true
hb.isFileExist = true
}
hb.lastUpdated = fi.ModTime()
hb.LastUpdated = hb.lastUpdated.Format(lastUpdatedFormat)
}
// isOld will return true if the host file has not been updated since seven
// days.
func (hb *Blockd) isOld() bool {
oneWeek := 7 * 24 * time.Hour
lastWeek := time.Now().Add(-1 * oneWeek)
return hb.lastUpdated.Before(lastWeek)
}
func (hb *Blockd) update() (err error) {
if !hb.isOld() {
return nil
}
var logp = `Blockd.update`
fmt.Printf("%s %s: updating ...\n", logp, hb.Name)
err = os.MkdirAll(filepath.Dir(hb.file), 0700)
if err != nil {
return fmt.Errorf("%s %s: %w", logp, hb.Name, err)
}
var (
req *http.Request
res *http.Response
)
req, err = http.NewRequestWithContext(context.Background(), http.MethodGet, hb.URL, nil)
if err != nil {
return fmt.Errorf(`%s %s: %w`, logp, hb.Name, err)
}
res, err = http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("%s %s: %w", logp, hb.Name, err)
}
defer func() {
var errClose = res.Body.Close()
if errClose != nil {
log.Printf("%s %q: %s", logp, hb.Name, err)
}
}()
var body []byte
body, err = io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("%s %q: %w", logp, hb.Name, err)
}
body = bytes.ReplaceAll(body, []byte("\r\n"), []byte("\n"))
if hb.IsEnabled {
err = os.WriteFile(hb.file, body, 0600)
} else {
err = os.WriteFile(hb.fileDisabled, body, 0600)
}
if err != nil {
return fmt.Errorf("%s %q: %w", logp, hb.Name, err)
}
hb.lastUpdated = time.Now()
hb.LastUpdated = hb.lastUpdated.Format(lastUpdatedFormat)
return nil
}