mirror of
https://github.com/bettercap/bettercap
synced 2025-07-16 10:03:39 -07:00
reverted pr #1100 due to instability
This commit is contained in:
parent
7371a85828
commit
c2ab5f4756
5 changed files with 126 additions and 98 deletions
|
@ -1,54 +1,60 @@
|
|||
package network
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -framework CoreWLAN
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
const char *GetSupportedFrequencies(const char *iface);
|
||||
bool SetInterfaceChannel(const char *iface, int channel);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"unsafe"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"github.com/bettercap/bettercap/core"
|
||||
|
||||
"github.com/evilsocket/islazy/str"
|
||||
)
|
||||
|
||||
const airPortPath = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
|
||||
|
||||
var WiFiChannelParser = regexp.MustCompile(`(?m)^.*Supported Channels: (.*)$`)
|
||||
|
||||
// see Windows version to understand why ....
|
||||
func getInterfaceName(iface net.Interface) string {
|
||||
return iface.Name
|
||||
}
|
||||
|
||||
func SetInterfaceChannel(iface string, channel int) error {
|
||||
cIface := C.CString(iface)
|
||||
defer C.free(unsafe.Pointer(cIface))
|
||||
curr := GetInterfaceChannel(iface)
|
||||
// the interface is already on this channel
|
||||
if curr == channel {
|
||||
return nil
|
||||
}
|
||||
|
||||
success := C.SetInterfaceChannel(cIface, C.int(channel))
|
||||
if !success {
|
||||
return errors.New("failed to set interface channel")
|
||||
_, err := core.Exec(airPortPath, []string{iface, fmt.Sprintf("-c%d", channel)})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
SetInterfaceCurrentChannel(iface, channel)
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetSupportedFrequencies(iface string) ([]int, error) {
|
||||
cIface := C.CString(iface)
|
||||
defer C.free(unsafe.Pointer(cIface))
|
||||
|
||||
cFrequencies := C.GetSupportedFrequencies(cIface)
|
||||
if cFrequencies == nil {
|
||||
return nil, errors.New("failed to get supported frequencies")
|
||||
func getFrequenciesFromChannels(output string) ([]int, error) {
|
||||
freqs := make([]int, 0)
|
||||
if output != "" {
|
||||
if matches := WiFiChannelParser.FindStringSubmatch(output); len(matches) == 2 {
|
||||
for _, channel := range str.Comma(matches[1]) {
|
||||
re := regexp.MustCompile(`\d+`)
|
||||
if channel, err := strconv.Atoi(re.FindString(channel)); err == nil {
|
||||
freqs = append(freqs, Dot11Chan2Freq(channel))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
defer C.free(unsafe.Pointer(cFrequencies))
|
||||
return freqs, nil
|
||||
}
|
||||
|
||||
frequenciesStr := C.GoString(cFrequencies)
|
||||
var frequencies []int
|
||||
err := json.Unmarshal([]byte(frequenciesStr), &frequencies)
|
||||
func GetSupportedFrequencies(iface string) ([]int, error) {
|
||||
out, err := core.Exec("system_profiler", []string{"SPAirPortDataType"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return frequencies, nil
|
||||
return getFrequenciesFromChannels(out)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue