Skip to content

Commit

Permalink
feature: add stop handling for engine
Browse files Browse the repository at this point in the history
  • Loading branch information
braginini committed Jun 21, 2021
1 parent 74355a2 commit 8088c7a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func SetupCloseHandler() {
go func() {
for range c {
fmt.Println("\r- Ctrl+C pressed in Terminal")
os.Exit(0)
stopUP <- 0
}
}()
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
iFaceBlackList[config.IFaceBlackList[i]] = struct{}{}
}
engine := connection.NewEngine(signalClient, config.StunTurnURLs, config.WgIface, config.WgAddr, iFaceBlackList)
defer engine.Stop()

err = engine.Start(myKey, config.Peers)
if err != nil {
Expand All @@ -48,8 +49,9 @@ var (
//signalClient.WaitConnected()

SetupCloseHandler()
<-stopUP
code := <-stopUP
log.Println("Receive signal to stop running")
os.Exit(code)
},
}
)
Expand Down
17 changes: 17 additions & 0 deletions connection/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ func NewEngine(signal *signal.Client, stunsTurns []*ice.URL, wgIface string, wgA
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.
// It also creates an Go routine to handle each peer communication from the config file
Expand Down
5 changes: 5 additions & 0 deletions iface/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const (
// Saves tun device object - is it required?
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.
// Will reuse an existing one.
func Create(iface string, address string) error {
Expand Down

0 comments on commit 8088c7a

Please sign in to comment.