Add wifi_ssid and wifi_bssid route and DNS rules

This commit is contained in:
世界
2023-11-18 13:51:32 +08:00
parent 83f6e022bb
commit 0e50872d04
15 changed files with 170 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
package cachefile
import (
"context"
"errors"
"net/netip"
"os"
@@ -13,6 +14,7 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/service/filemanager"
)
var (
@@ -41,7 +43,7 @@ type CacheFile struct {
saveMetadataTimer *time.Timer
}
func Open(path string, cacheID string) (*CacheFile, error) {
func Open(ctx context.Context, path string, cacheID string) (*CacheFile, error) {
const fileMode = 0o666
options := bbolt.Options{Timeout: time.Second}
var (
@@ -67,6 +69,10 @@ func Open(path string, cacheID string) (*CacheFile, error) {
if err != nil {
return nil, err
}
err = filemanager.Chown(ctx, path)
if err != nil {
return nil, E.Cause(err, "platform chown")
}
var cacheIDBytes []byte
if cacheID != "" {
cacheIDBytes = append([]byte{0}, []byte(cacheID)...)

View File

@@ -148,7 +148,7 @@ func NewServer(ctx context.Context, router adapter.Router, logFactory log.Observ
func (s *Server) PreStart() error {
if s.cacheFilePath != "" {
cacheFile, err := cachefile.Open(s.cacheFilePath, s.cacheID)
cacheFile, err := cachefile.Open(s.ctx, s.cacheFilePath, s.cacheID)
if err != nil {
return E.Cause(err, "open cache file")
}

View File

@@ -60,11 +60,11 @@ func (s *CommandServer) handleLogConn(conn net.Conn) error {
for element := s.savedLines.Front(); element != nil; element = element.Next() {
savedLines = append(savedLines, element.Value)
}
s.access.Unlock()
subscription, done, err := s.observer.Subscribe()
if err != nil {
return err
}
s.access.Unlock()
defer s.observer.UnSubscribe(subscription)
for _, line := range savedLines {
err = writeLog(conn, []byte(line))

View File

@@ -19,6 +19,7 @@ type PlatformInterface interface {
UsePlatformInterfaceGetter() bool
GetInterfaces() (NetworkInterfaceIterator, error)
UnderNetworkExtension() bool
ReadWIFIState() *WIFIState
ClearDNSCache()
}
@@ -38,6 +39,15 @@ type NetworkInterface struct {
Addresses StringIterator
}
type WIFIState struct {
SSID string
BSSID string
}
func NewWIFIState(wifiSSID string, wifiBSSID string) *WIFIState {
return &WIFIState{wifiSSID, wifiBSSID}
}
type NetworkInterfaceIterator interface {
Next() *NetworkInterface
HasNext() bool

View File

@@ -23,6 +23,7 @@ type Interface interface {
Interfaces() ([]NetworkInterface, error)
UnderNetworkExtension() bool
ClearDNSCache()
ReadWIFIState() adapter.WIFIState
process.Searcher
}

View File

@@ -210,6 +210,14 @@ func (w *platformInterfaceWrapper) ClearDNSCache() {
w.iif.ClearDNSCache()
}
func (w *platformInterfaceWrapper) ReadWIFIState() adapter.WIFIState {
wifiState := w.iif.ReadWIFIState()
if wifiState == nil {
return adapter.WIFIState{}
}
return (adapter.WIFIState)(*wifiState)
}
func (w *platformInterfaceWrapper) DisableColors() bool {
return runtime.GOOS != "android"
}