2024-10-21 15:38:34 +00:00
|
|
|
package rule
|
2023-11-24 12:58:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
F "github.com/sagernet/sing/common/format"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ RuleItem = (*WIFISSIDItem)(nil)
|
|
|
|
|
|
|
|
type WIFISSIDItem struct {
|
2024-11-10 04:11:21 +00:00
|
|
|
ssidList []string
|
|
|
|
ssidMap map[string]bool
|
|
|
|
networkManager adapter.NetworkManager
|
2023-11-24 12:58:07 +00:00
|
|
|
}
|
|
|
|
|
2024-11-10 04:11:21 +00:00
|
|
|
func NewWIFISSIDItem(networkManager adapter.NetworkManager, ssidList []string) *WIFISSIDItem {
|
2023-11-24 12:58:07 +00:00
|
|
|
ssidMap := make(map[string]bool)
|
|
|
|
for _, ssid := range ssidList {
|
|
|
|
ssidMap[ssid] = true
|
|
|
|
}
|
|
|
|
return &WIFISSIDItem{
|
|
|
|
ssidList,
|
|
|
|
ssidMap,
|
2024-11-10 04:11:21 +00:00
|
|
|
networkManager,
|
2023-11-24 12:58:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WIFISSIDItem) Match(metadata *adapter.InboundContext) bool {
|
2024-11-10 04:11:21 +00:00
|
|
|
return r.ssidMap[r.networkManager.WIFIState().SSID]
|
2023-11-24 12:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WIFISSIDItem) String() string {
|
|
|
|
if len(r.ssidList) == 1 {
|
|
|
|
return F.ToString("wifi_ssid=", r.ssidList[0])
|
|
|
|
}
|
|
|
|
return F.ToString("wifi_ssid=[", strings.Join(r.ssidList, " "), "]")
|
|
|
|
}
|