-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathovpnsplit.go
62 lines (51 loc) · 1.4 KB
/
ovpnsplit.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
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"os"
)
type Lol struct {
XMLName xml.Name `xml:"lol"`
Content string `xml:",chardata"`
OpenVPNData
}
type OpenVPNData struct {
Key string `xml:"key"`
Cert string `xml:"cert"`
Ca string `xml:"ca"`
TlsAuth string `xml:"tls-auth"`
}
func main() {
if len(os.Args) < 2 {
fmt.Print(`Usage:
ovpnsplit <file.ovpn>
`)
os.Exit(1)
}
filepath := os.Args[1]
filedata, err := ioutil.ReadFile(filepath)
if err != nil {
log.Fatal("Couldn't open file", err)
os.Exit(2)
}
data := Lol{}
// Ugly hack. Otherwise, not considered as xml valid…
filedata = append(append([]byte("<lol>\n"), filedata...), []byte("\n</lol>")...)
xml.Unmarshal(filedata, &data)
if data.Key != "" {
ioutil.WriteFile(filepath+".key.pem", []byte(data.Key), 0600)
}
if data.Cert != "" {
ioutil.WriteFile(filepath+".cert.pem", []byte(data.Cert), 0600)
}
if data.Ca != "" {
ioutil.WriteFile(filepath+".ca.pem", []byte(data.Ca), 0600)
}
if data.TlsAuth != "" {
ioutil.WriteFile(filepath+".tls-auth.pem", []byte(data.TlsAuth), 0600)
}
newnetmanfiledata := append(append(append(append([]byte(data.Content), []byte("\ncert "+filepath+".cert.pem\n")...), []byte("ca "+filepath+".ca.pem\n")...), []byte("key "+filepath+".key.pem\n")...), []byte("tls-auth "+filepath+".tls-auth.pem\n")...)
ioutil.WriteFile(filepath+".new.ovpn", newnetmanfiledata, 0600)
}