-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsystem_linux.go
More file actions
190 lines (161 loc) · 4.8 KB
/
system_linux.go
File metadata and controls
190 lines (161 loc) · 4.8 KB
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
189
190
//go:build linux
package sysinfo
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
)
func HasSystemd() bool {
systemdDir := "/etc/systemd/system"
if _, err := os.Stat(systemdDir); err == nil {
return true
}
return false
}
func NatEnabledIPv4() bool {
return fileIs1("/proc/sys/net/ipv4/ip_forward")
}
func NatEnabledIPv6() bool {
return fileIs1("/proc/sys/net/ipv6/conf/all/forwarding")
}
func EnableNat(ip4, ip6 bool) error {
if ip4 {
if err := writeLineToSysctlConf("net.ipv4.ip_forward=1"); err != nil {
return err
}
}
if ip6 {
if err := writeLineToSysctlConf("net.ipv6.conf.all.forwarding=1"); err != nil {
return err
}
}
return reloadSysctl()
}
func writeLineToSysctlConf(line string) error {
f, err := os.OpenFile("/etc/sysctl.conf", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("failed to open /etc/sysctl.conf: %w", err)
}
defer f.Close()
_, err = f.WriteString(line + "\n")
return err
}
func reloadSysctl() error {
path := sysctlPath()
if path == "" {
return nil
}
cmd := exec.Command(path, "-p")
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to reload sysctl: %w", err)
}
return nil
}
func fileIs1(path string) bool {
f, err := os.Open(path)
if err != nil {
return false
}
defer f.Close()
var buf [1]byte
_, err = f.Read(buf[:])
if err != nil {
return false
}
return buf[0] == '1'
}
func sysctlPath() string {
return binPath("sysctl")
}
func HasIPTables() bool {
if os.Getenv("WG_CMD_NO_DEPS") != "" {
return true
}
return binPath("iptables") != ""
}
func pathNameForInterface(iface string) string {
return "wgc-" + iface + ".path"
}
func serviceNameForInterface(iface string) string {
return "wgc-" + iface + ".service"
}
func createSystemdPathForInterface(iface, wgdir string) error {
wgConf := filepath.Join(wgdir, iface+".conf")
pathContent := fmt.Sprintf("[Unit]\nDescription=Watch %s for changes", wgConf)
pathContent += "\n\n[Path]\nPathModified=" + wgConf
pathContent += "\n\n[Install]\nWantedBy=multi-user.target\n"
pathName := pathNameForInterface(iface)
pathLoc := filepath.Join("/etc/systemd/system", pathName)
// save pathContent to pathLoc
if err := os.WriteFile(pathLoc, []byte(pathContent), 0o644); err != nil {
return fmt.Errorf("failed to write %s: %w", pathLoc, err)
}
// we don't need to reload systemd here, because we're going to do it later
return nil
}
func createSystemdTargetService(iface string) error {
pathName := pathNameForInterface(iface)
targetServiceName := serviceNameForInterface(iface)
targetService := fmt.Sprintf("[Unit]\nDescription=Restart WireGuard %s\nAfter=network.target", iface)
targetService += fmt.Sprintf("\n\n[Service]\nType=oneshot\n"+
"ExecStart=/usr/bin/systemctl restart wg-quick@%s.service", iface)
targetService += "\n\n[Install]\nRequiredBy=" + pathName + "\n"
targetServiceLoc := filepath.Join("/etc/systemd/system", targetServiceName)
// save targetService to targetServiceLoc
if err := os.WriteFile(targetServiceLoc, []byte(targetService), 0o644); err != nil {
return fmt.Errorf("failed to write %s: %w", targetServiceLoc, err)
}
return nil
}
func reloadAndEnableSystemdStuff(iface string) error {
pathName := pathNameForInterface(iface)
targetServiceName := serviceNameForInterface(iface)
// systemctl daemon-reload
cmd := exec.Command("systemctl", "daemon-reload")
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to reload systemd: %w", err)
}
// systemctl enable && start wg-quick@<iface>.service
wgqSrv := "wg-quick@" + iface + ".service"
cmd = exec.Command("systemctl", "enable", wgqSrv)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to enable %s: %w", wgqSrv, err)
}
cmd = exec.Command("systemctl", "start", wgqSrv)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to start %s: %w", wgqSrv, err)
}
// systemctl enable wgc-<iface>-restart.service
cmd = exec.Command("systemctl", "enable", pathName, targetServiceName)
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
log.Println("cmd:", cmd.String())
log.Println("err:", err)
log.Println("stdout:", stdout.String())
log.Println("stderr:", stderr.String())
return fmt.Errorf("failed to enable %s,%s: %w", pathName, targetServiceName, err)
}
// systemctl start wgc-<iface>-restart.service
cmd = exec.Command("systemctl", "start", pathName, targetServiceName)
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to start %s,%s: %w", pathName, targetServiceName, err)
}
return nil
}
func CreateSystemdStuff(iface, wgdir string) error {
if err := createSystemdPathForInterface(iface, wgdir); err != nil {
return err
}
if err := createSystemdTargetService(iface); err != nil {
return err
}
return reloadAndEnableSystemdStuff(iface)
}