feature: add stop handling for engine

This commit is contained in:
braginini 2021-06-21 11:18:03 +02:00
parent 74355a2292
commit 8088c7a591
4 changed files with 26 additions and 2 deletions

View File

@ -53,7 +53,7 @@ func SetupCloseHandler() {
go func() { go func() {
for range c { for range c {
fmt.Println("\r- Ctrl+C pressed in Terminal") fmt.Println("\r- Ctrl+C pressed in Terminal")
os.Exit(0) stopUP <- 0
} }
}() }()
} }

View File

@ -39,6 +39,7 @@ var (
iFaceBlackList[config.IFaceBlackList[i]] = struct{}{} iFaceBlackList[config.IFaceBlackList[i]] = struct{}{}
} }
engine := connection.NewEngine(signalClient, config.StunTurnURLs, config.WgIface, config.WgAddr, iFaceBlackList) engine := connection.NewEngine(signalClient, config.StunTurnURLs, config.WgIface, config.WgAddr, iFaceBlackList)
defer engine.Stop()
err = engine.Start(myKey, config.Peers) err = engine.Start(myKey, config.Peers)
if err != nil { if err != nil {
@ -48,8 +49,9 @@ var (
//signalClient.WaitConnected() //signalClient.WaitConnected()
SetupCloseHandler() SetupCloseHandler()
<-stopUP code := <-stopUP
log.Println("Receive signal to stop running") log.Println("Receive signal to stop running")
os.Exit(code)
}, },
} }
) )

View File

@ -46,6 +46,23 @@ func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgA
iFaceBlackList: iFaceBlackList, iFaceBlackList: iFaceBlackList,
} }
} }
func (e *Engine) Stop() error {
err := iface.Delete()
if err != nil {
log.Errorf("error while deleting Wireguard interface")
return err
}
err = e.signal.Close()
if err != nil {
log.Errorf("error while closing a connection to the signal server")
return err
}
for _, c := range e.conns {
c.Close() //nolint
}
return nil
}
// Start creates a new tunnel interface and listens to signals from the Signal service. // Start creates a new tunnel interface and listens to signals from the Signal service.
// It also creates an Go routine to handle each peer communication from the config file // It also creates an Go routine to handle each peer communication from the config file

View File

@ -19,6 +19,11 @@ const (
// Saves tun device object - is it required? // Saves tun device object - is it required?
var tunIface tun.Device var tunIface tun.Device
// Delete deletes an existing Wireguard interface
func Delete() error {
return tunIface.Close()
}
// Create Creates a new Wireguard interface, sets a given IP and brings it up. // Create Creates a new Wireguard interface, sets a given IP and brings it up.
// Will reuse an existing one. // Will reuse an existing one.
func Create(iface string, address string) error { func Create(iface string, address string) error {