-
Notifications
You must be signed in to change notification settings - Fork 18
/
openvpn.go
292 lines (249 loc) · 6.44 KB
/
openvpn.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package openvpn
import (
"bufio"
"fmt"
"os/exec"
"sync"
log "github.com/cihub/seelog"
"github.com/stamp/go-openssl"
)
type Process struct {
StdOut chan string `json:"-"`
StdErr chan string `json:"-"`
Events chan *Event `json:"-"`
Stopped chan bool `json:"-"`
parameters []string
config *Config
Env map[string]string
Clients map[string]*Client
management *Management
shutdown chan bool
waitGroup sync.WaitGroup
}
func NewProcess() *Process {
p := &Process{
Env: make(map[string]string, 0),
Events: make(chan *Event, 10),
Clients: make(map[string]*Client, 0),
shutdown: make(chan bool),
}
p.management = NewManagement(p)
return p
}
// Short-hands for some basic openvpn operating modes
func NewSslServer(ca *openssl.CA, cert *openssl.Cert, dh *openssl.DH, ta *openssl.TA, configFile string) *Process { // {{{
p := NewProcess()
c := NewConfig()
if configFile != "" {
err := c.LoadFile(configFile)
if err != nil {
log.Warn("Failed to load cnfiguration file provided:", err.Error())
} else {
log.Debug("Using configuration file :", configFile)
}
}
c.Device("tun")
c.ServerMode(1194, ca, cert, dh, ta)
c.IpPool("10.255.255.0/24")
c.KeepAlive(10, 60)
c.PingTimerRemote()
c.PersistTun()
c.PersistKey()
p.SetConfig(c)
return p
} // }}}
func NewSslClient(remote string, ca *openssl.CA, cert *openssl.Cert, dh *openssl.DH, ta *openssl.TA, configFile string) *Process { // {{{
p := NewProcess()
c := NewConfig()
//Load configuration file provided
if configFile != "" {
err := c.LoadFile(configFile)
if err != nil {
log.Error("Error loading configuration file: ", err.Error())
} else {
log.Debug("Loaded configuration file: ", configFile)
}
}
c.ClientMode(ca, cert, dh, ta)
c.Remote(remote, 1194)
c.Device("tun")
c.KeepAlive(10, 60)
c.PingTimerRemote()
c.PersistTun()
c.PersistKey()
p.SetConfig(c)
return p
} // }}}
func NewStaticKeyServer(key string, configFile string) *Process { // {{{
p := NewProcess()
c := NewConfig()
//Load configuration file provided
if configFile != "" {
err := c.LoadFile(configFile)
if err != nil {
log.Error("Error loading configuration file: ", err.Error())
} else {
log.Debug("Loaded configuration file: ", configFile)
}
}
c.Device("tun")
c.IpConfig("10.8.0.1", "10.8.0.2")
c.Secret(key)
c.KeepAlive(10, 60)
c.PingTimerRemote()
c.PersistTun()
c.PersistKey()
p.SetConfig(c)
return p
} // }}}
func NewStaticKeyClient(remote, key string, configFile string) *Process { // {{{
p := NewProcess()
c := NewConfig()
//Load configuration file provided
if configFile != "" {
err := c.LoadFile(configFile)
if err != nil {
log.Error("Error loading configuration file: ", err.Error())
} else {
log.Debug("Loaded configuration file: ", configFile)
}
}
c.Remote(remote, 1194)
c.Device("tun")
c.IpConfig("10.8.0.2", "10.8.0.1")
c.Secret(key)
c.KeepAlive(10, 60)
c.PingTimerRemote()
c.PersistTun()
c.PersistKey()
p.SetConfig(c)
return p
} // }}}
func (p *Process) SetConfig(c *Config) {
p.config = c
}
func (p *Process) Start() (err error) { // {{{
// Check if the process is already running
if p.Stopped != nil {
select {
case <-p.Stopped:
// Everything is good, no process running
default:
return fmt.Errorf("Openvpn is already started, aborting")
}
}
// Start the management interface (if it isnt already started)
path, err := p.management.Start()
if err != nil {
return err
}
// Add the management interface path to the config
p.config.setManagementPath(path)
return p.Restart()
} // }}}
func (p *Process) Stop() (err error) { // {{{
close(p.shutdown)
p.waitGroup.Wait()
return
} // }}}
func (p *Process) Shutdown() (err error) { // {{{
p.Stop()
p.management.Shutdown()
return
} // }}}
func (p *Process) Restart() (err error) { // {{{
// Fetch the current config
config, err := p.config.Validate()
if err != nil {
return err
}
// Create the command
cmd := exec.Command("openvpn", config...)
// Attatch monitors for stdout, stderr and exit
release := make(chan bool)
defer close(release)
p.ProcessMonitor(cmd, release)
// Try to start the process
err = cmd.Start()
if err != nil {
return err
}
return
} // }}}
func (p *Process) Fire(name string, args ...string) { // {{{
select {
case p.Events <- &Event{
Name: name,
Args: args,
}:
default:
log.Warn("Lost event: ", name, " args:", args)
}
} // }}}
func (p *Process) ProcessMonitor(cmd *exec.Cmd, release chan bool) { // {{{
p.stdoutMonitor(cmd)
p.stderrMonitor(cmd)
p.Stopped = make(chan bool)
go func() {
p.waitGroup.Add(1)
defer p.waitGroup.Done()
defer close(p.Stopped)
// Watch if the process exits
done := make(chan error)
go func() {
<-release // Wait for the process to start
done <- cmd.Wait()
}()
// Wait for shutdown or exit
select {
case <-p.shutdown:
// Kill the server
if err := cmd.Process.Kill(); err != nil {
return
}
err := <-done // allow goroutine to exit
log.Error("process killed with error = ", err)
case err := <-done:
log.Error("process done with error = ", err)
return
}
}()
} // }}}
func (p *Process) stdoutMonitor(cmd *exec.Cmd) { // {{{
stdout, _ := cmd.StdoutPipe()
go func() {
p.waitGroup.Add(1)
defer p.waitGroup.Done()
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
select {
case p.StdOut <- scanner.Text():
default:
log.Trace("OPENVPN stdout: ", scanner.Text())
}
}
if err := scanner.Err(); err != nil {
log.Warn("OPENVPN stdout: (failed to read: ", err, ")")
return
}
}()
} // }}}
func (p *Process) stderrMonitor(cmd *exec.Cmd) { // {{{
stderr, _ := cmd.StderrPipe()
go func() {
p.waitGroup.Add(1)
defer p.waitGroup.Done()
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
select {
case p.StdErr <- scanner.Text():
default:
log.Warn("OPENVPN stderr: ", scanner.Text())
}
}
if err := scanner.Err(); err != nil {
log.Warn("OPENVPN stderr: (failed to read ", err, ")")
return
}
}()
} // }}}