net_darwin.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package network
  2. import (
  3. "fmt"
  4. "net"
  5. "regexp"
  6. "strconv"
  7. "github.com/bettercap/bettercap/core"
  8. "github.com/evilsocket/islazy/str"
  9. )
  10. const airPortPath = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
  11. var WiFiChannelParser = regexp.MustCompile(`(?m)^.*Supported Channels: (.*)$`)
  12. // see Windows version to understand why ....
  13. func getInterfaceName(iface net.Interface) string {
  14. return iface.Name
  15. }
  16. func SetInterfaceChannel(iface string, channel int) error {
  17. curr := GetInterfaceChannel(iface)
  18. // the interface is already on this channel
  19. if curr == channel {
  20. return nil
  21. }
  22. _, err := core.Exec(airPortPath, []string{iface, fmt.Sprintf("-c%d", channel)})
  23. if err != nil {
  24. return err
  25. }
  26. SetInterfaceCurrentChannel(iface, channel)
  27. return nil
  28. }
  29. func getFrequenciesFromChannels(output string) ([]int, error) {
  30. freqs := make([]int, 0)
  31. if output != "" {
  32. if matches := WiFiChannelParser.FindStringSubmatch(output); len(matches) == 2 {
  33. for _, channel := range str.Comma(matches[1]) {
  34. re := regexp.MustCompile(`\d+`)
  35. if channel, err := strconv.Atoi(re.FindString(channel)); err == nil {
  36. freqs = append(freqs, Dot11Chan2Freq(channel))
  37. }
  38. }
  39. }
  40. }
  41. return freqs, nil
  42. }
  43. func GetSupportedFrequencies(iface string) ([]int, error) {
  44. out, err := core.Exec("system_profiler", []string{"SPAirPortDataType"})
  45. if err != nil {
  46. return nil, err
  47. }
  48. return getFrequenciesFromChannels(out)
  49. }