-
Notifications
You must be signed in to change notification settings - Fork 2
/
openvpnConfig.go
141 lines (104 loc) · 2.59 KB
/
openvpnConfig.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/rs/zerolog/log"
)
type (
openVPNConfig struct {
Filename string
Host string
Protocol string
Port int
Formatted bool
}
)
func parseAndFormatOpenVPNConfig(inFilename, outDir string) (config *openVPNConfig, err error) {
fileBytes, err := os.ReadFile(inFilename)
if err != nil {
return
}
config, err = parseOpenVPNConfig(fileBytes)
if outDir != "" {
config.Formatted = true
err = formatAndSaveOpenVPNConfig(fileBytes, outDir, config)
} else {
config.Filename = inFilename
}
return
}
func parseOpenVPNConfig(fileBytes []byte) (config *openVPNConfig, err error) {
sliceData := strings.Split(string(fileBytes), "\n")
if len(sliceData) == 0 {
return nil, fmt.Errorf("empty file")
}
config = &openVPNConfig{}
for _, line := range sliceData {
tokens := strings.Split(line, " ")
if len(tokens) == 0 {
continue
}
if strings.HasPrefix("#", tokens[0]) || strings.HasPrefix(";", tokens[0]) {
continue
}
switch tokens[0] {
case "remote":
if len(tokens) != 3 {
log.Fatal().Msg("Unexpected number of arguments for remote")
}
config.Host = tokens[1]
p, err := strconv.ParseInt(tokens[2], 10, 64)
if err != nil {
log.Error().Err(err).Msg("Failed parsing port of remote! Defaulting to 443." + errorSuffix)
p = 443
}
config.Port = int(p)
case "proto":
if len(tokens) != 2 {
log.Fatal().Msg("Unexpected number of arguments for proto")
}
config.Protocol = tokens[1]
}
}
return
}
func formatAndSaveOpenVPNConfig(fileBytes []byte, outDir string, config *openVPNConfig) (err error) {
sliceData := strings.Split(string(fileBytes), "\n")
if len(sliceData) == 0 {
return fmt.Errorf("empty file")
}
f, err := os.CreateTemp(outDir, "*.openvpn")
config.Filename = f.Name()
if err != nil {
return
}
defer f.Close()
for _, line := range sliceData {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "auth-user-pass") ||
strings.HasPrefix(line, "auth-federate") ||
strings.HasPrefix(line, "auth-retry interact") ||
strings.HasPrefix(line, "remote ") ||
strings.HasPrefix(line, "verb") ||
strings.HasPrefix(line, "remote-random-hostname") {
continue
}
_, err = f.WriteString(line + "\n")
if err != nil {
return
}
}
return
}
func saveOpenVPNAuthConfig(outDir, password string) (filename string, err error) {
authFile, err := os.CreateTemp(outDir, "*.auth.openvpn")
if err != nil {
return
}
defer authFile.Close()
filename = authFile.Name()
authFile.WriteString("N/A\n" + password + "\n")
return
}