mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 13:09:49 -07:00
fix: network.SetInterfaceChannel won't exec iwconfig if the interface is already on the requested channel.
This commit is contained in:
parent
0f5beb72dd
commit
9e393e5bf4
1 changed files with 15 additions and 0 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/bettercap/bettercap/core"
|
"github.com/bettercap/bettercap/core"
|
||||||
)
|
)
|
||||||
|
@ -18,6 +19,9 @@ var IPv4RouteCmd = "ip"
|
||||||
var IPv4RouteCmdOpts = []string{"route"}
|
var IPv4RouteCmdOpts = []string{"route"}
|
||||||
var WiFiFreqParser = regexp.MustCompile(`^\s+Channel.([0-9]+)\s+:\s+([0-9\.]+)\s+GHz.*$`)
|
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) {
|
func IPv4RouteIsGateway(ifname string, tokens []string, f func(gateway string) (*Endpoint, error)) (*Endpoint, error) {
|
||||||
ifname2 := tokens[3]
|
ifname2 := tokens[3]
|
||||||
|
|
||||||
|
@ -35,12 +39,23 @@ func getInterfaceName(iface net.Interface) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetInterfaceChannel(iface string, channel int) error {
|
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)})
|
out, err := core.Exec("iwconfig", []string{iface, "channel", fmt.Sprintf("%d", channel)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if out != "" {
|
} else if out != "" {
|
||||||
return fmt.Errorf("Unexpected output while setting interface %s to channel %d: %s", iface, channel, out)
|
return fmt.Errorf("Unexpected output while setting interface %s to channel %d: %s", iface, channel, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currChannels[iface] = channel
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue