platform: Add update WIFI state func

This commit is contained in:
世界 2025-02-20 14:42:09 +08:00
parent 97ce666e43
commit 93b68312cf
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 20 additions and 36 deletions

View file

@ -25,6 +25,7 @@ type NetworkManager interface {
PackageManager() tun.PackageManager
WIFIState() WIFIState
ResetNetwork()
UpdateWIFIState()
}
type NetworkOptions struct {

View file

@ -4,7 +4,6 @@ import (
"encoding/binary"
"net"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/varbin"
)
@ -18,19 +17,7 @@ func (c *CommandClient) ServiceReload() error {
if err != nil {
return err
}
var hasError bool
err = binary.Read(conn, binary.BigEndian, &hasError)
if err != nil {
return err
}
if hasError {
errorMessage, err := varbin.ReadValue[string](conn, binary.BigEndian)
if err != nil {
return err
}
return E.New(errorMessage)
}
return nil
return readError(conn)
}
func (s *CommandServer) handleServiceReload(conn net.Conn) error {
@ -55,19 +42,7 @@ func (c *CommandClient) ServiceClose() error {
if err != nil {
return err
}
var hasError bool
err = binary.Read(conn, binary.BigEndian, &hasError)
if err != nil {
return nil
}
if hasError {
errorMessage, err := varbin.ReadValue[string](conn, binary.BigEndian)
if err != nil {
return nil
}
return E.New(errorMessage)
}
return nil
return readError(conn)
}
func (s *CommandServer) handleServiceClose(conn net.Conn) error {

View file

@ -31,3 +31,7 @@ func (s *BoxService) Wake() {
func (s *BoxService) ResetNetwork() {
s.instance.Router().ResetNetwork()
}
func (s *BoxService) UpdateWIFIState() {
s.instance.Network().UpdateWIFIState()
}

View file

@ -354,6 +354,18 @@ func (r *NetworkManager) WIFIState() adapter.WIFIState {
return r.wifiState
}
func (r *NetworkManager) UpdateWIFIState() {
if r.platformInterface != nil {
state := r.platformInterface.ReadWIFIState()
if state != r.wifiState {
r.wifiState = state
if state.SSID != "" {
r.logger.Info("updated WIFI state: SSID=", state.SSID, ", BSSID=", state.BSSID)
}
}
}
}
func (r *NetworkManager) ResetNetwork() {
conntrack.Close()
@ -414,15 +426,7 @@ func (r *NetworkManager) notifyInterfaceUpdate(defaultInterface *control.Interfa
}
}
r.logger.Info("updated default interface ", defaultInterface.Name, ", ", strings.Join(options, ", "))
if r.platformInterface != nil {
state := r.platformInterface.ReadWIFIState()
if state != r.wifiState {
r.wifiState = state
if state.SSID != "" {
r.logger.Info("updated WIFI state: SSID=", state.SSID, ", BSSID=", state.BSSID)
}
}
}
r.UpdateWIFIState()
if !r.started {
return