|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package settings |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "syscall" |
| 11 | + |
| 12 | + "github.com/sagernet/sing-box/adapter" |
| 13 | + "github.com/sagernet/sing/common/winwlanapi" |
| 14 | + |
| 15 | + "golang.org/x/sys/windows" |
| 16 | +) |
| 17 | + |
| 18 | +type windowsWIFIMonitor struct { |
| 19 | + handle windows.Handle |
| 20 | + callback func(adapter.WIFIState) |
| 21 | + cancel context.CancelFunc |
| 22 | + lastState adapter.WIFIState |
| 23 | + mutex sync.Mutex |
| 24 | +} |
| 25 | + |
| 26 | +func NewWIFIMonitor(callback func(adapter.WIFIState)) (WIFIMonitor, error) { |
| 27 | + handle, err := winwlanapi.OpenHandle() |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + interfaces, err := winwlanapi.EnumInterfaces(handle) |
| 33 | + if err != nil { |
| 34 | + winwlanapi.CloseHandle(handle) |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + if len(interfaces) == 0 { |
| 38 | + winwlanapi.CloseHandle(handle) |
| 39 | + return nil, fmt.Errorf("no wireless interfaces found") |
| 40 | + } |
| 41 | + |
| 42 | + return &windowsWIFIMonitor{ |
| 43 | + handle: handle, |
| 44 | + callback: callback, |
| 45 | + }, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (m *windowsWIFIMonitor) ReadWIFIState() adapter.WIFIState { |
| 49 | + interfaces, err := winwlanapi.EnumInterfaces(m.handle) |
| 50 | + if err != nil || len(interfaces) == 0 { |
| 51 | + return adapter.WIFIState{} |
| 52 | + } |
| 53 | + |
| 54 | + for _, iface := range interfaces { |
| 55 | + if iface.InterfaceState != winwlanapi.InterfaceStateConnected { |
| 56 | + continue |
| 57 | + } |
| 58 | + |
| 59 | + guid := iface.InterfaceGUID |
| 60 | + attrs, err := winwlanapi.QueryCurrentConnection(m.handle, &guid) |
| 61 | + if err != nil { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + ssidLength := attrs.AssociationAttributes.SSID.Length |
| 66 | + if ssidLength == 0 || ssidLength > winwlanapi.Dot11SSIDMaxLength { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + ssid := string(attrs.AssociationAttributes.SSID.SSID[:ssidLength]) |
| 71 | + bssid := formatBSSID(attrs.AssociationAttributes.BSSID) |
| 72 | + |
| 73 | + return adapter.WIFIState{ |
| 74 | + SSID: strings.TrimSpace(ssid), |
| 75 | + BSSID: bssid, |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return adapter.WIFIState{} |
| 80 | +} |
| 81 | + |
| 82 | +func formatBSSID(mac winwlanapi.Dot11MacAddress) string { |
| 83 | + return fmt.Sprintf("%02X%02X%02X%02X%02X%02X", |
| 84 | + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) |
| 85 | +} |
| 86 | + |
| 87 | +func (m *windowsWIFIMonitor) Start() error { |
| 88 | + if m.callback == nil { |
| 89 | + return nil |
| 90 | + } |
| 91 | + |
| 92 | + ctx, cancel := context.WithCancel(context.Background()) |
| 93 | + m.cancel = cancel |
| 94 | + |
| 95 | + m.lastState = m.ReadWIFIState() |
| 96 | + |
| 97 | + callbackFunc := func(data *winwlanapi.NotificationData, callbackContext uintptr) uintptr { |
| 98 | + if data.NotificationSource != winwlanapi.NotificationSourceACM { |
| 99 | + return 0 |
| 100 | + } |
| 101 | + switch data.NotificationCode { |
| 102 | + case winwlanapi.NotificationACMConnectionComplete, |
| 103 | + winwlanapi.NotificationACMDisconnected: |
| 104 | + m.checkAndNotify() |
| 105 | + } |
| 106 | + return 0 |
| 107 | + } |
| 108 | + |
| 109 | + callbackPointer := syscall.NewCallback(callbackFunc) |
| 110 | + |
| 111 | + err := winwlanapi.RegisterNotification(m.handle, winwlanapi.NotificationSourceACM, callbackPointer, 0) |
| 112 | + if err != nil { |
| 113 | + cancel() |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + go func() { |
| 118 | + <-ctx.Done() |
| 119 | + }() |
| 120 | + |
| 121 | + m.callback(m.lastState) |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func (m *windowsWIFIMonitor) checkAndNotify() { |
| 126 | + m.mutex.Lock() |
| 127 | + defer m.mutex.Unlock() |
| 128 | + |
| 129 | + state := m.ReadWIFIState() |
| 130 | + if state != m.lastState { |
| 131 | + m.lastState = state |
| 132 | + if m.callback != nil { |
| 133 | + m.callback(state) |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func (m *windowsWIFIMonitor) Close() error { |
| 139 | + if m.cancel != nil { |
| 140 | + m.cancel() |
| 141 | + } |
| 142 | + winwlanapi.UnregisterNotification(m.handle) |
| 143 | + return winwlanapi.CloseHandle(m.handle) |
| 144 | +} |
0 commit comments