-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_vpn.go
166 lines (159 loc) · 3.64 KB
/
import_vpn.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
155
156
157
158
159
160
161
162
163
164
165
166
package ezovpn
import (
"bufio"
"encoding/base64"
"io"
"path/filepath"
"regexp"
"strings"
)
const (
caOpen = "<ca>\n"
caClose = "</ca>\n"
certOpen = "<cert>\n"
certClose = "</cert>\n"
keyOpen = "<key>\n"
keyClose = "</key>\n"
taOpen = "<tls-auth>\n"
taClose = "</tls-auth>\n"
taKey = "key-direction 1\n"
pkcsOpen = "<pkcs12>\n"
pkcsClose = "</pkcs12>\n"
)
const (
fileIdx = 7
)
var (
filesMatcher = regexp.MustCompile(`^\s*((ca)|(cert)|(key)|(tls-auth)|(pkcs12))\s+?(\S+)`)
)
func resolvePath(prefix, path string, cf CertFetcher) (io.ReadCloser, error) {
var r io.ReadCloser
if filepath.IsAbs(path) {
f, err := cf(path)
if err != nil {
return nil, err
}
r = f
} else {
f, err := cf(filepath.Join(prefix, path))
if err != nil {
return nil, err
}
r = f
}
return r, nil
}
// readFileIn detects if the path to a certificate, defined in VPN config - is absolute or relative.
//
// Reads the file and wraps it's content into the corresponding tags.
func readFileIn(prefix, path string, w io.Writer, tagOpen, tagClose string, contentFetch CertFetcher) error {
r, err := resolvePath(prefix, path, contentFetch)
if err != nil {
return err
}
defer r.Close()
if _, err := w.Write([]byte(tagOpen)); err != nil {
return err
}
if _, err := io.Copy(w, r); err != nil {
return err
}
if _, err := w.Write([]byte(tagClose)); err != nil {
return err
}
return nil
}
// ImportVPNConfig imports the configuration from the file, using pathPrefix to resolve paths to certificates, if they are not absolute.
//
// pathPrefix prefix to the path for resolving relative certificate paths.
// rdr read the original VPN config from.
// w write the resulting VPN config into.
// contentFetch reads the certificate content.
func ImportVPNConfig(pathPrefix string, rdr io.Reader, w io.Writer, contentFetch CertFetcher) error {
var (
s strings.Builder
br = bufio.NewReader(rdr)
)
readLine := func() (string, error) {
s.Reset()
for {
b, prefix, err := br.ReadLine()
switch err {
case io.EOF:
s.Write(b)
return s.String(), err
case nil:
s.Write(b)
default:
return "", err
}
if !prefix {
break
}
}
return s.String(), nil
}
stop := false
loop:
for !stop {
l, err := readLine()
switch err {
case nil:
case io.EOF:
stop = true
if l == "" {
break loop
}
default:
return err
}
if grps := filesMatcher.FindStringSubmatch(l); grps != nil {
filename := grps[fileIdx]
switch grps[1] {
case "ca":
readFileIn(pathPrefix, filename, w, caOpen, caClose, contentFetch)
case "cert":
readFileIn(pathPrefix, filename, w, certOpen, certClose, contentFetch)
case "key":
readFileIn(pathPrefix, filename, w, keyOpen, keyClose, contentFetch)
case "tls-auth":
w.Write([]byte(taKey))
readFileIn(pathPrefix, filename, w, taOpen, taClose, contentFetch)
case "pkcs12":
var (
s strings.Builder
)
r, err := resolvePath(pathPrefix, filename, contentFetch)
if err != nil {
return err
}
enc := base64.NewEncoder(base64.StdEncoding, &s)
if _, err := io.Copy(enc, r); err != nil {
return err
}
if err := r.Close(); err != nil {
return err
}
if err := enc.Close(); err != nil {
return err
}
if _, err := w.Write([]byte(pkcsOpen)); err != nil {
return err
}
if _, err := w.Write([]byte(formatBase64(s.String()))); err != nil {
return err
}
if _, err := w.Write([]byte(pkcsClose)); err != nil {
return err
}
default:
w.Write([]byte(l))
w.Write(crlf)
}
} else {
w.Write([]byte(l))
w.Write(crlf)
}
}
return nil
}