This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdguard.go
123 lines (94 loc) · 3.4 KB
/
Adguard.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
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"time"
"gopkg.in/yaml.v2"
)
func watchFile(certificateFile string, privateKeyFile string, configFile string, changed chan time.Time, config *ConfigFile) {
cert, err := os.Stat(certificateFile)
checkERR(err)
initState := cert.ModTime()
for {
cert, _ := os.Stat(certificateFile)
newState := cert.ModTime()
if newState != initState {
updateConfig(certificateFile, privateKeyFile, configFile, config)
initState = cert.ModTime()
changed <- newState
}
time.Sleep(1 * time.Minute)
}
}
func updateConfig(certificateFile string, privateKeyFile string, adGurardConfigFile string, config *ConfigFile) {
var timer int
timeout := config.Letsencrypt.Timeout
for {
if _, err := os.Stat(certificateFile); os.IsNotExist(err) {
untilTimeout := timeout - timer
if timer == 1 {
log.Println("Looking for certificate file, it can take few seconds for the certificate to generate for te first time.")
}
log.Printf("Please wait... Time left until timeout: %v\n", untilTimeout)
time.Sleep(5 * time.Second)
timer = timer + 5
if timer >= timeout {
log.Println("Certificate lookup timed out, pleasse make sure the FQDN selected is right and owned by you")
os.Exit(18)
}
} else {
log.Printf("Found new/updated certificate in path: %v\n", certificateFile)
break
}
}
newCertificate, err := ioutil.ReadFile(certificateFile)
checkERR(err)
cert := string(newCertificate)
newKey, err := ioutil.ReadFile(privateKeyFile)
checkERR(err)
key := string(newKey)
config.TLS.CertificateChain = cert
config.TLS.PrivateKey = key
toYaml, err := yaml.Marshal(&config)
checkERR(err)
err = ioutil.WriteFile(adGurardConfigFile, toYaml, 755)
checkERR(err)
log.Println("AdGuard config updated")
}
func startAdGuard(adguardconfigFile string, pid chan *os.Process, errExit chan error) {
adguardExecutable := "/opt/adguardhome/AdGuardHome"
log.Println("Starting AdGuard-Home...")
cmd := exec.Command(adguardExecutable, "-h", "0.0.0.0", "-c", adguardconfigFile, "-w", "/opt/adguardhome/work")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
checkERR(err)
go func() {
pid <- cmd.Process
}()
errExit <- cmd.Wait()
}
func filePath(configurationFile string, config *ConfigFile) (string, string) {
var certPath string
var keyPath string
if config.Letsencrypt.Enabled == false {
if os.Getenv("CERT_FILE") == "" || os.Getenv("KEY_FILE") == "" {
log.Printf("Both \"CERT_FILE\" and \"KEY_FILE\" environment variables need to be set when letsencrypt.enabled set to \"false\"")
} else {
certPath = os.Getenv("CERT_FILE")
keyPath = os.Getenv("KEY_FILE")
}
} else {
if config.Letsencrypt.Production == false {
certPath = strings.Join([]string{"/root/.caddy/acme/acme-staging-v02.api.letsencrypt.org/sites/", config.TLS.ServerName, "/", config.TLS.ServerName, ".", "crt"}, "")
keyPath = strings.Join([]string{"/root/.caddy/acme/acme-staging-v02.api.letsencrypt.org/sites/", config.TLS.ServerName, "/", config.TLS.ServerName, ".", "key"}, "")
} else {
certPath = strings.Join([]string{"/root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/", config.TLS.ServerName, "/", config.TLS.ServerName, ".", "crt"}, "")
keyPath = strings.Join([]string{"/root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/", config.TLS.ServerName, "/", config.TLS.ServerName, ".", "key"}, "")
}
}
return certPath, keyPath
}