| 1 | package network |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | var ( |
| 11 | pathNameCleaner = regexp.MustCompile("[^a-zA-Z0-9]+") |
| 12 | ) |
| 13 | |
| 14 | type Station struct { |
| 15 | *Endpoint |
| 16 | Frequency int `json:"frequency"` |
| 17 | Channel int `json:"channel"` |
| 18 | RSSI int8 `json:"rssi"` |
| 19 | Sent uint64 `json:"sent"` |
| 20 | Received uint64 `json:"received"` |
| 21 | Encryption string `json:"encryption"` |
| 22 | Cipher string `json:"cipher"` |
| 23 | Authentication string `json:"authentication"` |
| 24 | WPS map[string]string `json:"wps"` |
| 25 | Handshake *Handshake `json:"-"` |
| 26 | } |
| 27 | |
| 28 | func cleanESSID(essid string) string { |
| 29 | res := "" |
| 30 | for _, c := range essid { |
| 31 | if strconv.IsPrint(c) { |
| 32 | res += string(c) |
| 33 | } else { |
| 34 | break |
| 35 | } |
| 36 | } |
| 37 | return res |
| 38 | } |
| 39 | |
| 40 | func NewStation(essid, bssid string, frequency int, rssi int8) *Station { |
| 41 | return &Station{ |
| 42 | Endpoint: NewEndpointNoResolve(MonitorModeAddress, bssid, cleanESSID(essid), 0), |
| 43 | Frequency: frequency, |
| 44 | Channel: Dot11Freq2Chan(frequency), |
| 45 | RSSI: rssi, |
| 46 | WPS: make(map[string]string), |
| 47 | Handshake: NewHandshake(), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func (s Station) BSSID() string { |
| 52 | return s.HwAddress |
| 53 | } |
| 54 | |
| 55 | func (s *Station) ESSID() string { |
| 56 | return s.Hostname |
| 57 | } |
| 58 | |
| 59 | func (s *Station) HasWPS() bool { |
| 60 | return len(s.WPS) > 0 |
| 61 | } |
| 62 | |
| 63 | func (s *Station) IsOpen() bool { |
| 64 | return s.Encryption == "" || s.Encryption == "OPEN" |
| 65 | } |
| 66 | |
| 67 | func (s *Station) PathFriendlyName() string { |
| 68 | name := "" |
| 69 | bssid := strings.Replace(s.HwAddress, ":", "", -1) |
| 70 | if essid := pathNameCleaner.ReplaceAllString(s.Hostname, ""); essid != "" { |
| 71 | name = fmt.Sprintf("%s_%s", essid, bssid) |
| 72 | } else { |
| 73 | name = bssid |
| 74 | } |
| 75 | return name |
| 76 | } |