forked from tyzbit/go-openvpn
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
89 lines (75 loc) · 1.89 KB
/
config.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
package config
// html/template changed to text/template
import (
"bytes"
"io/ioutil"
"text/template"
)
// Don't think these defaults are ever used -- see models/models.go
var defaultConfig = Config{
Dev: "tap0",
Port: 1194,
Proto: "udp",
DNSServerOne: "8.8.8.8",
DNSServerTwo: "8.8.4.4",
Server: "server-bridge 192.168.1.250 255.255.255.0 192.168.1.2 192.168.1.5",
Cipher: "AES-256-CBC",
Keysize: 256,
Auth: "SHA256",
Dh: "dh2048.pem",
Keepalive: "10 120",
IfconfigPoolPersist: "ipp.txt",
CCEncryption: "/etc/openvpn/easy-rsa/pki/ta.key",
ExtraServerOptions: "# client-config-dir /etc/openvpn/ccd",
ExtraClientOptions: "",
}
//Config model
type Config struct {
Dev string
Port int
Proto string
Ca string
Cert string
Key string
Cipher string
Keysize int
Auth string
Dh string
DNSServerOne string
DNSServerTwo string
Server string
IfconfigPoolPersist string
Keepalive string
CCEncryption string
ExtraServerOptions string
ExtraClientOptions string
Management string
PiVPNServer string
}
//New returns config object with default values
func New() Config {
return defaultConfig
}
//GetText injects config values into template
func GetText(tpl string, c Config) (string, error) {
t := template.New("config")
t, err := t.Parse(tpl)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
t.Execute(buf, c)
return buf.String(), nil
}
//SaveToFile reads teamplate and writes result to destination file
func SaveToFile(tplPath string, c Config, destPath string) error {
template, err := ioutil.ReadFile(tplPath)
if err != nil {
return err
}
str, err := GetText(string(template), c)
if err != nil {
return err
}
return ioutil.WriteFile(destPath, []byte(str), 0644)
}