-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdaemon.go
826 lines (694 loc) · 20.2 KB
/
daemon.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
// Package daemon contains the OC-Daemon.
package daemon
import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"net"
"reflect"
"strconv"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/telekom-mms/oc-daemon/internal/api"
"github.com/telekom-mms/oc-daemon/internal/dbusapi"
"github.com/telekom-mms/oc-daemon/internal/execs"
"github.com/telekom-mms/oc-daemon/internal/ocrunner"
"github.com/telekom-mms/oc-daemon/internal/profilemon"
"github.com/telekom-mms/oc-daemon/internal/sleepmon"
"github.com/telekom-mms/oc-daemon/internal/trafpol"
"github.com/telekom-mms/oc-daemon/internal/vpnsetup"
"github.com/telekom-mms/oc-daemon/pkg/logininfo"
"github.com/telekom-mms/oc-daemon/pkg/vpnconfig"
"github.com/telekom-mms/oc-daemon/pkg/vpnstatus"
"github.com/telekom-mms/oc-daemon/pkg/xmlprofile"
"github.com/telekom-mms/tnd/pkg/tnd"
"golang.org/x/sys/unix"
)
// Daemon is used to run the daemon.
type Daemon struct {
config *Config
server *api.Server
dbus *dbusapi.Service
tnd tnd.TND
vpnsetup *vpnsetup.VPNSetup
trafpol *trafpol.TrafPol
sleepmon *sleepmon.SleepMon
status *vpnstatus.Status
runner *ocrunner.Connect
// token is used for client authentication
token string
// channel for errors
errors chan error
// channels for shutdown
done chan struct{}
closed chan struct{}
// profile is the xml profile
profile *xmlprofile.Profile
profmon *profilemon.ProfileMon
// disableTrafPol determines if traffic policing should be disabled,
// overrides other traffic policing settings
disableTrafPol bool
}
// setStatusTrustedNetwork sets the trusted network status in status.
func (d *Daemon) setStatusTrustedNetwork(trusted bool) {
// convert bool to trusted network status
trustedNetwork := vpnstatus.TrustedNetworkNotTrusted
if trusted {
trustedNetwork = vpnstatus.TrustedNetworkTrusted
}
// check status change
if d.status.TrustedNetwork == trustedNetwork {
// status not changed
return
}
// status changed
d.status.TrustedNetwork = trustedNetwork
d.dbus.SetProperty(dbusapi.PropertyTrustedNetwork, trustedNetwork)
}
// setStatusConnectionState sets the connection state in status.
func (d *Daemon) setStatusConnectionState(connectionState vpnstatus.ConnectionState) {
if d.status.ConnectionState == connectionState {
// state not changed
return
}
// state changed
d.status.ConnectionState = connectionState
d.dbus.SetProperty(dbusapi.PropertyConnectionState, connectionState)
}
// setStatusIP sets the IP in status.
func (d *Daemon) setStatusIP(ip string) {
if d.status.IP == ip {
// ip not changed
return
}
// ip changed
d.status.IP = ip
d.dbus.SetProperty(dbusapi.PropertyIP, ip)
}
// setStatusDevice sets the device in status.
func (d *Daemon) setStatusDevice(device string) {
if d.status.Device == device {
// device not changed
return
}
// device changed
d.status.Device = device
d.dbus.SetProperty(dbusapi.PropertyDevice, device)
}
// setStatusServer sets the current server in status.
func (d *Daemon) setStatusServer(server string) {
if d.status.Server == server {
// connected server not changed
return
}
// connected server changed
d.status.Server = server
d.dbus.SetProperty(dbusapi.PropertyServer, server)
}
// setStatusConnectedAt sets the connection time in status.
func (d *Daemon) setStatusConnectedAt(connectedAt int64) {
if d.status.ConnectedAt == connectedAt {
// connection time not changed
return
}
// connection time changed
d.status.ConnectedAt = connectedAt
d.dbus.SetProperty(dbusapi.PropertyConnectedAt, connectedAt)
}
// setStatusServers sets the vpn servers in status.
func (d *Daemon) setStatusServers(servers []string) {
if reflect.DeepEqual(d.status.Servers, servers) {
// servers not changed
return
}
// servers changed
d.status.Servers = servers
d.dbus.SetProperty(dbusapi.PropertyServers, servers)
}
// setStatusOCRunning sets the openconnect running state in status.
func (d *Daemon) setStatusOCRunning(running bool) {
ocrunning := vpnstatus.OCRunningNotRunning
if running {
ocrunning = vpnstatus.OCRunningRunning
}
if d.status.OCRunning == ocrunning {
// OC running state not changed
return
}
// OC running state changed
d.status.OCRunning = ocrunning
d.dbus.SetProperty(dbusapi.PropertyOCRunning, ocrunning)
}
// setStatusVPNConfig sets the VPN config in status.
func (d *Daemon) setStatusVPNConfig(config *vpnconfig.Config) {
if d.status.VPNConfig.Equal(config) {
// config not changed
return
}
// config changed
d.status.VPNConfig = config
if config == nil {
// remove config
d.dbus.SetProperty(dbusapi.PropertyVPNConfig, dbusapi.VPNConfigInvalid)
return
}
// update json config
b, err := config.JSON()
if err != nil {
log.WithError(err).Error("Daemon could not convert status to JSON")
d.dbus.SetProperty(dbusapi.PropertyVPNConfig, dbusapi.VPNConfigInvalid)
return
}
d.dbus.SetProperty(dbusapi.PropertyVPNConfig, string(b))
}
// connectVPN connects to the VPN using login info from client request.
func (d *Daemon) connectVPN(login *logininfo.LoginInfo) {
// allow only one connection
if d.status.OCRunning.Running() {
return
}
// ignore invalid login information
if !login.Valid() {
return
}
// update status
d.setStatusOCRunning(true)
d.setStatusServer(login.Server)
d.setStatusConnectionState(vpnstatus.ConnectionStateConnecting)
// connect using runner
env := []string{
"oc_daemon_token=" + d.token,
"oc_daemon_socket_file=" + d.config.SocketServer.SocketFile,
"oc_daemon_verbose=" + strconv.FormatBool(d.config.Verbose),
}
d.runner.Connect(login, env)
}
// disconnectVPN disconnects from the VPN.
func (d *Daemon) disconnectVPN() {
// update status
d.setStatusConnectionState(vpnstatus.ConnectionStateDisconnecting)
d.setStatusOCRunning(false)
// stop runner
if d.runner == nil {
return
}
d.runner.Disconnect()
}
// updateVPNConfigUp updates the VPN config for VPN connect.
func (d *Daemon) updateVPNConfigUp(config *vpnconfig.Config) {
// check if old and new config differ
if config.Equal(d.status.VPNConfig) {
log.WithField("error", "old and new vpn configs are equal").
Error("Daemon config up error")
return
}
// check if vpn is flagged as running
if !d.status.OCRunning.Running() {
log.WithField("error", "vpn not running").
Error("Daemon config up error")
return
}
// check if we are already connected
if d.status.ConnectionState.Connected() {
log.WithField("error", "vpn already connected").
Error("Daemon config up error")
return
}
// connecting, set up configuration
log.Info("Daemon setting up vpn configuration")
d.vpnsetup.Setup(config)
// set traffic policing setting from Disable Always On VPN setting
// in configuration
d.disableTrafPol = config.Flags.DisableAlwaysOnVPN
// save config
d.setStatusVPNConfig(config)
ip := ""
for _, addr := range []net.IP{config.IPv4.Address, config.IPv6.Address} {
// this assumes either a single IPv4 or a single IPv6 address
// is configured on a vpn device
if addr != nil {
ip = addr.String()
}
}
d.setStatusIP(ip)
d.setStatusDevice(config.Device.Name)
}
// updateVPNConfigDown updates the VPN config for VPN disconnect.
func (d *Daemon) updateVPNConfigDown() {
// TODO: only call this from Runner Event only and remove down message?
// or potentially calling this twice is better than not at all?
// check if vpn is still flagged as running
if d.status.OCRunning.Running() {
log.WithField("error", "vpn still running").
Error("Daemon config down error")
return
}
// check if vpn is still flagged as connected
if d.status.ConnectionState.Connected() {
log.WithField("error", "vpn still connected").
Error("Daemon config down error")
return
}
// disconnecting, tear down configuration
log.Info("Daemon tearing down vpn configuration")
if d.status.VPNConfig != nil {
d.vpnsetup.Teardown(d.status.VPNConfig)
}
// save config
d.setStatusVPNConfig(nil)
d.setStatusConnectionState(vpnstatus.ConnectionStateDisconnected)
d.setStatusServer("")
d.setStatusConnectedAt(0)
d.setStatusIP("")
d.setStatusDevice("")
}
// updateVPNConfig updates the VPN config with config update in client request.
func (d *Daemon) updateVPNConfig(request *api.Request) {
// parse config
configUpdate, err := VPNConfigUpdateFromJSON(request.Data())
if err != nil {
log.WithError(err).Error("Daemon could not parse config update from JSON")
request.Error("invalid config update message")
return
}
// check if config update is valid
if !configUpdate.Valid() {
log.Error("Daemon got invalid vpn config update")
request.Error("invalid config update in config update message")
return
}
// check token
if configUpdate.Token != d.token {
log.Error("Daemon got invalid token in vpn config update")
request.Error("invalid token in config update message")
return
}
// handle config update for vpn (dis)connect
if configUpdate.Reason == "disconnect" {
d.updateVPNConfigDown()
return
}
d.updateVPNConfigUp(configUpdate.Config)
}
// handleClientRequest handles a client request.
func (d *Daemon) handleClientRequest(request *api.Request) {
defer request.Close()
log.Debug("Daemon handling client request")
switch request.Type() {
case api.TypeVPNConfigUpdate:
// update VPN config
d.updateVPNConfig(request)
}
}
// handleDBusRequest handles a D-Bus API client request.
func (d *Daemon) handleDBusRequest(request *dbusapi.Request) {
defer request.Close()
log.Debug("Daemon handling D-Bus client request")
switch request.Name {
case dbusapi.RequestConnect:
// create login info
server := request.Parameters[0].(string)
cookie := request.Parameters[1].(string)
host := request.Parameters[2].(string)
connectURL := request.Parameters[3].(string)
fingerprint := request.Parameters[4].(string)
resolve := request.Parameters[5].(string)
login := &logininfo.LoginInfo{
Server: server,
Cookie: cookie,
Host: host,
ConnectURL: connectURL,
Fingerprint: fingerprint,
Resolve: resolve,
}
// connect VPN
d.connectVPN(login)
case dbusapi.RequestDisconnect:
// diconnect VPN
d.disconnectVPN()
}
}
// checkDisconnectVPN checks if we need to disconnect the VPN when handling a
// TND result.
func (d *Daemon) checkDisconnectVPN() {
if d.status.TrustedNetwork.Trusted() && d.status.OCRunning.Running() {
// disconnect VPN when switching from untrusted network with
// active VPN connection to a trusted network
log.Info("Daemon detected trusted network, disconnecting VPN connection")
d.disconnectVPN()
}
}
// handleTNDResult handles a TND result.
func (d *Daemon) handleTNDResult(trusted bool) error {
log.WithField("trusted", trusted).Debug("Daemon handling TND result")
d.setStatusTrustedNetwork(trusted)
d.checkDisconnectVPN()
return d.checkTrafPol()
}
// handleRunnerDisconnect handles a disconnect event from the OC runner,
// cleaning up everthing. This is also called when stopping the daemon.
func (d *Daemon) handleRunnerDisconnect() {
// make sure running and connected are not set
d.setStatusOCRunning(false)
d.setStatusConnectionState(vpnstatus.ConnectionStateDisconnected)
d.setStatusServer("")
d.setStatusConnectedAt(0)
// make sure the vpn config is not active any more
d.updateVPNConfigDown()
}
// handleRunnerEvent handles a connect event from the OC runner.
func (d *Daemon) handleRunnerEvent(e *ocrunner.ConnectEvent) {
log.WithField("event", e).Debug("Daemon handling Runner event")
if e.Connect {
// make sure running is set
d.setStatusOCRunning(true)
return
}
// clean up after disconnect
d.handleRunnerDisconnect()
}
// handleVPNSetupEvent handles a VPN setup event.
func (d *Daemon) handleVPNSetupEvent(event *vpnsetup.Event) {
switch event.Type {
case vpnsetup.EventSetupOK:
d.setStatusConnectionState(vpnstatus.ConnectionStateConnected)
d.setStatusConnectedAt(time.Now().Unix())
log.Info("Daemon configured VPN connection")
case vpnsetup.EventTeardownOK:
log.Info("Daemon unconfigured VPN connection")
}
}
// handleSleepMonEvent handles a suspend/resume event from SleepMon.
func (d *Daemon) handleSleepMonEvent(sleep bool) {
log.WithField("sleep", sleep).Debug("Daemon handling SleepMon event")
// disconnect vpn on resume
if !sleep && d.status.OCRunning.Running() {
d.disconnectVPN()
}
}
// readXMLProfile reads the XML profile from file.
func readXMLProfile(xmlProfile string) *xmlprofile.Profile {
profile, err := xmlprofile.LoadProfile(xmlProfile)
if err != nil {
// invalid config, use empty config
log.WithError(err).Error("Could not read XML profile")
profile = xmlprofile.NewProfile()
}
return profile
}
// handleProfileUpdate handles a xml profile update.
func (d *Daemon) handleProfileUpdate() error {
log.Debug("Daemon handling XML profile update")
d.profile = readXMLProfile(d.config.OpenConnect.XMLProfile)
d.stopTND()
d.stopTrafPol()
if err := d.checkTrafPol(); err != nil {
return err
}
if err := d.checkTND(); err != nil {
return err
}
d.setStatusServers(d.profile.GetVPNServerHostNames())
return nil
}
// cleanup cleans up after a failed shutdown.
func (d *Daemon) cleanup(ctx context.Context) {
ocrunner.CleanupConnect(d.config.OpenConnect)
vpnsetup.Cleanup(ctx, d.config.OpenConnect.VPNDevice, d.config.SplitRouting)
trafpol.Cleanup(ctx)
}
// initToken creates the daemon token for client authentication.
func (d *Daemon) initToken() error {
// TODO: is this good enough for us?
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
return err
}
d.token = base64.RawURLEncoding.EncodeToString(b)
return nil
}
// getProfileAllowedHosts returns the allowed hosts.
func (d *Daemon) getProfileAllowedHosts() (hosts []string) {
// add vpn servers to allowed hosts
hosts = append(hosts, d.profile.GetVPNServers()...)
// add tnd servers to allowed hosts
hosts = append(hosts, d.profile.GetTNDServers()...)
// add allowed hosts from xml profile to allowed hosts
hosts = append(hosts, d.profile.GetAllowedHosts()...)
return
}
// initTNDServers sets the TND servers from the xml profile.
func (d *Daemon) initTNDServers() {
servers := d.profile.GetTNDHTTPSServers()
d.tnd.SetServers(servers)
}
// setTNDDialer sets a custom dialer for TND.
func (d *Daemon) setTNDDialer() {
// get mark to be set on socket
mark, err := strconv.Atoi(d.config.SplitRouting.FirewallMark)
if err != nil {
log.WithError(err).Error("Daemon could not convert FWMark to int")
return
}
// control function that sets socket option on raw connection
control := func(network, address string, c syscall.RawConn) error {
// set socket option function for setting mark with SO_MARK
var soerr error
setsockopt := func(fd uintptr) {
soerr = unix.SetsockoptInt(
int(fd),
unix.SOL_SOCKET,
unix.SO_MARK,
mark,
)
if soerr != nil {
log.WithError(soerr).Error("TND could not set SO_MARK")
}
}
if err := c.Control(setsockopt); err != nil {
return err
}
return soerr
}
// create and set dialer
dialer := &net.Dialer{
Control: control,
}
d.tnd.SetDialer(dialer)
}
// startTND starts TND if it's not running.
func (d *Daemon) startTND() error {
if d.tnd != nil {
return nil
}
d.tnd = tnd.NewDetector(d.config.TND)
d.initTNDServers()
d.setTNDDialer()
if err := d.tnd.Start(); err != nil {
return fmt.Errorf("Daemon could not start TND: %w", err)
}
return nil
}
// stopTND stops TND if it's running.
func (d *Daemon) stopTND() {
if d.tnd == nil {
return
}
d.tnd.Stop()
d.tnd = nil
}
// checkTND checks if TND should be running and starts or stops it.
func (d *Daemon) checkTND() error {
if len(d.profile.GetTNDServers()) == 0 {
d.stopTND()
return nil
}
return d.startTND()
}
// getTNDResults returns the TND results channel.
// TODO: move this into TND code?
func (d *Daemon) getTNDResults() chan bool {
if d.tnd == nil {
return nil
}
return d.tnd.Results()
}
// startTrafPol starts traffic policing if it's not running.
func (d *Daemon) startTrafPol() error {
if d.trafpol != nil {
return nil
}
c := trafpol.NewConfig()
c.AllowedHosts = append(c.AllowedHosts, d.getProfileAllowedHosts()...)
c.FirewallMark = d.config.SplitRouting.FirewallMark
d.trafpol = trafpol.NewTrafPol(c)
if err := d.trafpol.Start(); err != nil {
return fmt.Errorf("Daemon could not start TrafPol: %w", err)
}
return nil
}
// stopTrafPol stops traffic policing if it's running.
func (d *Daemon) stopTrafPol() {
if d.trafpol == nil {
return
}
d.trafpol.Stop()
d.trafpol = nil
}
// checkTrafPol checks if traffic policing should be running and
// starts or stops it.
func (d *Daemon) checkTrafPol() error {
// check if traffic policing is disabled in the daemon
if d.disableTrafPol {
d.stopTrafPol()
return nil
}
// check if traffic policing is enabled in the xml profile
if !d.profile.GetAlwaysOn() {
d.stopTrafPol()
return nil
}
// check if we are connected to a trusted network
if d.status.TrustedNetwork.Trusted() {
d.stopTrafPol()
return nil
}
return d.startTrafPol()
}
// start starts the daemon.
func (d *Daemon) start() {
defer close(d.closed)
defer d.sleepmon.Stop()
defer d.stopTrafPol()
defer d.stopTND()
defer d.vpnsetup.Stop()
defer d.handleRunnerDisconnect() // clean up vpn config
defer d.runner.Stop()
defer d.server.Stop()
defer d.dbus.Stop()
defer d.profmon.Stop()
// run main loop
for {
select {
case req := <-d.server.Requests():
d.handleClientRequest(req)
case req := <-d.dbus.Requests():
d.handleDBusRequest(req)
case r := <-d.getTNDResults():
if err := d.handleTNDResult(r); err != nil {
// send error event and stop daemon
d.errors <- fmt.Errorf("Daemon could not handle TND result: %w", err)
return
}
case e := <-d.runner.Events():
d.handleRunnerEvent(e)
case e := <-d.vpnsetup.Events():
d.handleVPNSetupEvent(e)
case e := <-d.sleepmon.Events():
d.handleSleepMonEvent(e)
case <-d.profmon.Updates():
if err := d.handleProfileUpdate(); err != nil {
// send error event and stop daemon
d.errors <- fmt.Errorf("Daemon could not handle Profile update: %w", err)
return
}
case <-d.done:
return
}
}
}
// Start starts the daemon.
func (d *Daemon) Start() error {
// create context
ctx := context.Background()
// set executables
execs.SetExecutables(d.config.Executables)
// cleanup after a failed shutdown
d.cleanup(ctx)
// init token
if err := d.initToken(); err != nil {
return fmt.Errorf("Daemon could not init token: %w", err)
}
// start sleep monitor
if err := d.sleepmon.Start(); err != nil {
return fmt.Errorf("Daemon could not start sleep monitor: %w", err)
}
// start traffic policing
if err := d.checkTrafPol(); err != nil {
return err
}
// start TND
if err := d.checkTND(); err != nil {
return err
}
// start VPN setup
d.vpnsetup.Start()
// start OC runner
d.runner.Start()
// start unix server
err := d.server.Start()
if err != nil {
err = fmt.Errorf("Daemon could not start Socket API server: %w", err)
goto cleanup_unix
}
// start dbus api service
err = d.dbus.Start()
if err != nil {
err = fmt.Errorf("Daemon could not start D-Bus API: %w", err)
goto cleanup_dbus
}
// start xml profile monitor
err = d.profmon.Start()
if err != nil {
err = fmt.Errorf("Daemon could not start ProfileMon: %w", err)
goto cleanup_profmon
}
// set initial status
d.setStatusConnectionState(vpnstatus.ConnectionStateDisconnected)
d.setStatusServers(d.profile.GetVPNServerHostNames())
go d.start()
return nil
// clean up after error
cleanup_profmon:
d.dbus.Stop()
cleanup_dbus:
d.server.Stop()
cleanup_unix:
d.runner.Stop()
d.vpnsetup.Stop()
d.stopTND()
d.stopTrafPol()
d.sleepmon.Stop()
return err
}
// Stop stops the daemon.
func (d *Daemon) Stop() {
// stop daemon and wait for main loop termination
close(d.done)
<-d.closed
}
// Errors returns the error channel of the daemon.
func (d *Daemon) Errors() chan error {
return d.errors
}
// NewDaemon returns a new Daemon.
func NewDaemon(config *Config) *Daemon {
return &Daemon{
config: config,
server: api.NewServer(config.SocketServer),
dbus: dbusapi.NewService(),
sleepmon: sleepmon.NewSleepMon(),
vpnsetup: vpnsetup.NewVPNSetup(config.DNSProxy,
config.SplitRouting),
runner: ocrunner.NewConnect(config.OpenConnect),
status: vpnstatus.New(),
errors: make(chan error, 1),
done: make(chan struct{}),
closed: make(chan struct{}),
profile: readXMLProfile(config.OpenConnect.XMLProfile),
profmon: profilemon.NewProfileMon(config.OpenConnect.XMLProfile),
}
}