fix: network.SetInterfaceChannel won't exec iwconfig if the interface is already on the requested channel.

This commit is contained in:
evilsocket 2018-08-02 17:57:14 +02:00
commit 9e393e5bf4
No known key found for this signature in database
GPG key ID: 1564D7F30393A456

View file

@ -7,6 +7,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"github.com/bettercap/bettercap/core"
)
@ -18,6 +19,9 @@ var IPv4RouteCmd = "ip"
var IPv4RouteCmdOpts = []string{"route"}
var WiFiFreqParser = regexp.MustCompile(`^\s+Channel.([0-9]+)\s+:\s+([0-9\.]+)\s+GHz.*$`)
var currChannels = make(map[string]int)
var currChannelLock = sync.Mutex{}
func IPv4RouteIsGateway(ifname string, tokens []string, f func(gateway string) (*Endpoint, error)) (*Endpoint, error) {
ifname2 := tokens[3]
@ -35,12 +39,23 @@ func getInterfaceName(iface net.Interface) string {
}
func SetInterfaceChannel(iface string, channel int) error {
currChannelLock.Lock()
defer currChannelLock.Unlock()
// the interface is already on this channel
if curr, found := currChannels[iface]; found && curr == channel {
return nil
}
out, err := core.Exec("iwconfig", []string{iface, "channel", fmt.Sprintf("%d", channel)})
if err != nil {
return err
} else if out != "" {
return fmt.Errorf("Unexpected output while setting interface %s to channel %d: %s", iface, channel, out)
}
currChannels[iface] = channel
return nil
}