-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.go
213 lines (168 loc) · 6.02 KB
/
setup.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"fmt"
"os"
"os/exec"
"path"
"runtime"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
const (
OpenVPNSourceFolderName = "openvpn-2.5.1"
OpenVPNTarName = "openvpn-2.5.1.tar.xz"
OpenVPNSource = "https://swupdate.openvpn.org/community/releases/openvpn-2.5.1.tar.xz"
)
var (
OpenVPNPatchScript = path.Join("scripts", "openvpn-v2.5.1-aws.patch")
OpenVPNConfigureOptions = []string{
"--disable-debug",
"--disable-dependency-tracking",
"--disable-silent-rules",
"--with-crypto-library=openssl",
// This maybe a Apple specific thing you want to enable.
// Tested this on Arch and Debian with no issues...
// "--enable-pkcs11",
}
)
// setupAction Compiles and builds patched version of openvpn and verify we have everything we need before doing so.
// TODO: Download and patch and compile via Golang?
func setupAction(c *cli.Context) error {
patchFile := c.String("patch")
outputDir := c.String("out")
sourceDir := c.String("source")
// TODO: Remove me when Windows has been fully supported and tested.
if runtime.GOOS == "windows" {
log.Fatal().Msg("Detected windows environment! This operation is not properly developed to execute for Windows. Please manually build openvpn using the provided ruby script." + errorSuffix)
}
// Make sure all required commands are installed on the system.
// TODO: Add more commands to check?
var quit bool
if !commandExists("make") {
log.Error().Msg("Make not found! Please install build-essentials or development tools to continue." + errorSuffix)
quit = true
}
if !commandExists("patch") {
log.Error().Msg("Make not found! Please install build-essentials or development tools to continue." + errorSuffix)
quit = true
}
if !commandExists("wget") {
log.Error().Msg("Make not found! Please install build-essentials or development tools to continue. " + errorSuffix)
quit = true
}
if !commandExists("tar") {
log.Error().Msg("Make not found! Please install build-essentials or development tools to continue. " + errorSuffix)
quit = true
}
if quit {
return fmt.Errorf("one or more commands not found")
}
if patchFile == "" {
patchFile = OpenVPNPatchScript
}
if !fileExists(patchFile) {
log.Error().Msgf("Patch file '%s' not found! Please use -p to define a patch file! "+errorSuffix, patchFile)
}
if sourceDir == "" {
log.Info().Msgf("Downloading and extracing %s...", OpenVPNSourceFolderName)
tempDir := os.TempDir()
err := downloadOpenVPN(tempDir)
if err != nil {
return fmt.Errorf("failed downloading OpenVPN")
}
tarFilename := path.Join(tempDir, OpenVPNTarName)
err = extractTarFile(tarFilename, tempDir)
if err != nil {
return fmt.Errorf("failed extracting OpenVPN")
}
sourceDir = path.Join(tempDir, OpenVPNSourceFolderName)
}
log.Info().Msgf("Applying patch %s...", sourceDir)
err := patchOpenVPN(sourceDir, patchFile)
if err != nil {
return fmt.Errorf("failed patching OpenVPN source code")
}
log.Info().Msgf("Compiling OpenVPN...")
err = compileOpenVPN(sourceDir, outputDir)
if err != nil {
return fmt.Errorf("failed patching OpenVPN source code")
}
return nil
}
func downloadOpenVPN(dir string) error {
log.Debug().Str("url", OpenVPNSource).Str("dir", dir).Msg("Downloading OpenVPN...")
out, err := exec.Command("wget", "-P", dir, "-O", path.Join(dir, OpenVPNTarName), OpenVPNSource).CombinedOutput()
if err != nil {
log.Error().Err(err).Bytes("out", out).Msg("Failed downloading OpenVPN " + errorSuffix)
return err
}
return nil
}
func extractTarFile(filename, dir string) error {
log.Debug().Str("filename", filename).Str("dir", dir).Msg("Extracting tar...")
out, err := exec.Command("tar", "-xvf", filename, "-C", dir).CombinedOutput()
if err != nil {
log.Error().Err(err).Bytes("out", out).Msg("Failed extracting OpenVPN " + errorSuffix)
return err
}
return nil
}
func patchOpenVPN(source, patchFilename string) error {
log.Debug().Msgf("Running 'patch -p 1 -d %s < %s'", source, patchFilename)
cmd := exec.Command("/bin/bash", "-c", "/usr/bin/patch -p 1 -d "+source+" < "+patchFilename)
cmd.Env = os.Environ()
out, err := cmd.CombinedOutput()
if err != nil {
log.Error().Err(err).Bytes("out", out).Msg("Failed patching OpenVPN source code! " + errorSuffix)
return err
}
return nil
}
func compileOpenVPN(source, outputDir string) error {
log.Debug().Strs("args", OpenVPNConfigureOptions).Msg("running configure...")
configureCmd := exec.Command("./configure", OpenVPNConfigureOptions...)
configureCmd.Stdout = os.Stdout
configureCmd.Stderr = os.Stderr
configureCmd.Stdin = os.Stdin
configureCmd.Dir = source
err := configureCmd.Start()
if err != nil {
log.Error().Err(err).Msg("Failed compiling OpenVPN! " + errorSuffix)
return err
}
err = configureCmd.Wait()
if err != nil {
log.Error().Err(err).Msg("Failed compiling OpenVPN! " + errorSuffix)
return err
}
log.Debug().Msg("running make...")
makeCmd := exec.Command("make")
makeCmd.Stdout = os.Stdout
makeCmd.Stderr = os.Stderr
makeCmd.Stdin = os.Stdin
makeCmd.Dir = path.Join(source, "src")
err = makeCmd.Start()
if err != nil {
log.Error().Err(err).Msg("Failed compiling OpenVPN! " + errorSuffix)
return err
}
err = makeCmd.Wait()
if err != nil {
log.Error().Err(err).Msg("Failed compiling OpenVPN! " + errorSuffix)
return err
}
binaryFilename := path.Join(source, "src", "openvpn", "openvpn")
distFilename := path.Join(outputDir, "openvpn_aws")
if !fileExists(binaryFilename) {
log.Error().Err(err).Msg("Failed compiling OpenVPN! " + errorSuffix)
return fmt.Errorf("binary '%s' failed to compile", binaryFilename)
}
err = copyFile(binaryFilename, path.Join(outputDir, distFilename))
if err != nil {
log.Error().Err(err).Str("bin", binaryFilename).Str("dist", distFilename).Msg("Failed copying dist binary " + errorSuffix)
return fmt.Errorf("failed copy")
}
log.Info().Msgf("Finished compiling binary: %s", distFilename)
log.Info().Msg("Make sure to move this binary to a safe spot and make sure to setup your awsvpnclient.yml vpn.openvpn field to this executable!")
return nil
}