/
system_interface.go
151 lines (135 loc) · 3.05 KB
/
system_interface.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
package openapi
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
)
const SQLLITE_EXECUTABLE = "/usr/bin/sqlite3"
const SQLLITE_DATABASE = "/etc/pihole/gravity.db"
const PIHOLE_EXECUTABLE = "/usr/local/bin/pihole"
const CNAME_FILE = "/etc/dnsmasq.d/05-pihole-custom-cname.conf"
const TYPEA_FILE = "/etc/pihole/custom.list"
const EMPTY_LIST = "Not showing empty list"
const LIST_REGEX = `\d: (.+) \((.*), last modified (.*)\)$`
func runCommand(command string, args []string) (result cmdResp) {
log.Printf("Running command '%v %v'", command, args)
result.command = command
result.args = args
cmd := exec.Command(command, args...)
output, err := cmd.Output()
result.stdout = string(output)
if err != nil {
result.err = err
var errb bytes.Buffer
cmd.Stderr = &errb
result.stderr = errb.String()
log.Printf("Command failed: %v", result.stderr)
} else {
log.Print("Command ran successfully")
}
return
}
type cmdResp struct {
command string
args []string
err error
stdout string
stderr string
}
func getCNAME() (records []Record, err error) {
err = touchCNAMEFile()
if err != nil {
return nil, err
}
data, err := os.ReadFile(CNAME_FILE)
if err != nil {
return nil, err
}
for _, line := range strings.Split(string(data), "\n") {
if len(line) > 0 {
split := strings.Split(line, ",")
domain := strings.Split(split[0], "=")[1]
destination := split[1]
records = append(records, Record{
Type: "CNAME",
Domain: domain,
Destination: destination,
})
}
}
return
}
func getA() (records []Record, err error) {
data, err := os.ReadFile(TYPEA_FILE)
if err != nil {
return nil, err
}
for _, line := range strings.Split(string(data), "\n") {
if len(line) > 0 {
split := strings.Split(line, " ")
domain := split[1]
destination := split[0]
records = append(records, Record{
Type: "A",
Domain: domain,
Destination: destination,
})
}
}
return
}
func touchCNAMEFile() error {
_, err := os.Stat(CNAME_FILE)
if os.IsNotExist(err) {
file, err := os.Create(CNAME_FILE)
if err != nil {
return err
}
defer file.Close()
}
return nil
}
func writeA(records []Record) error {
t := time.Now()
tempfile := fmt.Sprintf("/tmp/typea_%v.tmp", t)
f, err := os.OpenFile(tempfile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
for _, record := range records {
_, err := f.WriteString(fmt.Sprintf("%v %v\n", record.Destination, record.Domain))
if err != nil {
return err
}
}
err = os.Rename(tempfile, TYPEA_FILE)
if err != nil {
return err
}
return nil
}
func writeCNAME(records []Record) error {
t := time.Now()
tempfile := fmt.Sprintf("/tmp/cname_%v.tmp", t)
f, err := os.OpenFile(tempfile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
for _, record := range records {
_, err := f.WriteString(fmt.Sprintf("cname=%v,%v\n", record.Domain, record.Destination))
if err != nil {
return err
}
}
err = os.Rename(tempfile, CNAME_FILE)
if err != nil {
return err
}
return nil
}