FatalOnError fixes
This commit is contained in:
@@ -57,8 +57,8 @@ func _main(background context.Context, args []string) int {
|
||||
ctx, cancel := context.WithCancel(background)
|
||||
defer cancel()
|
||||
logger := createLogger()
|
||||
wg := &sync.WaitGroup{}
|
||||
fatalOnError := makeFatalOnError(logger, cancel, wg)
|
||||
|
||||
fatalOnError := makeFatalOnError(logger, cancel)
|
||||
|
||||
client := network.NewClient(15 * time.Second)
|
||||
// Create configurators
|
||||
@@ -143,6 +143,8 @@ func _main(background context.Context, args []string) int {
|
||||
fatalOnError(err)
|
||||
}
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
openvpnLooper := openvpn.NewLooper(allSettings.VPNSP, allSettings.OpenVPN, uid, gid,
|
||||
ovpnConf, firewallConf, logger, client, fileManager, streamMerger, fatalOnError)
|
||||
restartOpenvpn := openvpnLooper.Restart
|
||||
@@ -249,18 +251,11 @@ func _main(background context.Context, args []string) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func makeFatalOnError(logger logging.Logger, cancel context.CancelFunc, wg *sync.WaitGroup) func(err error) {
|
||||
func makeFatalOnError(logger logging.Logger, cancel context.CancelFunc) func(err error) {
|
||||
return func(err error) {
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
cancel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
go func() {
|
||||
wg.Wait()
|
||||
cancel()
|
||||
}()
|
||||
<-ctx.Done() // either timeout or wait group completed
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +101,10 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
||||
settings := l.GetSettings()
|
||||
providerConf := provider.New(l.provider)
|
||||
connections, err := providerConf.GetOpenVPNConnections(settings.Provider.ServerSelection)
|
||||
if err != nil {
|
||||
l.fatalOnError(err)
|
||||
return
|
||||
}
|
||||
lines := providerConf.BuildConf(
|
||||
connections,
|
||||
settings.Verbosity,
|
||||
@@ -112,14 +115,19 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
||||
settings.Auth,
|
||||
settings.Provider.ExtraConfigOptions,
|
||||
)
|
||||
err = l.fileManager.WriteLinesToFile(string(constants.OpenVPNConf), lines, files.Ownership(l.uid, l.gid), files.Permissions(0400))
|
||||
if err := l.fileManager.WriteLinesToFile(string(constants.OpenVPNConf), lines, files.Ownership(l.uid, l.gid), files.Permissions(0400)); err != nil {
|
||||
l.fatalOnError(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = l.conf.WriteAuthFile(settings.User, settings.Password, l.uid, l.gid)
|
||||
if err := l.conf.WriteAuthFile(settings.User, settings.Password, l.uid, l.gid); err != nil {
|
||||
l.fatalOnError(err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := l.fw.SetVPNConnections(ctx, connections); err != nil {
|
||||
l.fatalOnError(err)
|
||||
return
|
||||
}
|
||||
|
||||
openvpnCtx, openvpnCancel := context.WithCancel(context.Background())
|
||||
|
||||
Reference in New Issue
Block a user