github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/libbox/platform.go (about)

     1  package libbox
     2  
     3  import (
     4  	"github.com/sagernet/sing-box/option"
     5  )
     6  
     7  type PlatformInterface interface {
     8  	UsePlatformAutoDetectInterfaceControl() bool
     9  	AutoDetectInterfaceControl(fd int32) error
    10  	OpenTun(options TunOptions) (int32, error)
    11  	WriteLog(message string)
    12  	UseProcFS() bool
    13  	FindConnectionOwner(ipProtocol int32, sourceAddress string, sourcePort int32, destinationAddress string, destinationPort int32) (int32, error)
    14  	PackageNameByUid(uid int32) (string, error)
    15  	UIDByPackageName(packageName string) (int32, error)
    16  	UsePlatformDefaultInterfaceMonitor() bool
    17  	StartDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
    18  	CloseDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
    19  	UsePlatformInterfaceGetter() bool
    20  	GetInterfaces() (NetworkInterfaceIterator, error)
    21  	UnderNetworkExtension() bool
    22  	IncludeAllNetworks() bool
    23  	ReadWIFIState() *WIFIState
    24  	ClearDNSCache()
    25  }
    26  
    27  type TunInterface interface {
    28  	FileDescriptor() int32
    29  	Close() error
    30  }
    31  
    32  type InterfaceUpdateListener interface {
    33  	UpdateDefaultInterface(interfaceName string, interfaceIndex int32)
    34  }
    35  
    36  type NetworkInterface struct {
    37  	Index     int32
    38  	MTU       int32
    39  	Name      string
    40  	Addresses StringIterator
    41  }
    42  
    43  type WIFIState struct {
    44  	SSID  string
    45  	BSSID string
    46  }
    47  
    48  func NewWIFIState(wifiSSID string, wifiBSSID string) *WIFIState {
    49  	return &WIFIState{wifiSSID, wifiBSSID}
    50  }
    51  
    52  type NetworkInterfaceIterator interface {
    53  	Next() *NetworkInterface
    54  	HasNext() bool
    55  }
    56  
    57  type OnDemandRule interface {
    58  	Target() int32
    59  	DNSSearchDomainMatch() StringIterator
    60  	DNSServerAddressMatch() StringIterator
    61  	InterfaceTypeMatch() int32
    62  	SSIDMatch() StringIterator
    63  	ProbeURL() string
    64  }
    65  
    66  type OnDemandRuleIterator interface {
    67  	Next() OnDemandRule
    68  	HasNext() bool
    69  }
    70  
    71  type onDemandRule struct {
    72  	option.OnDemandRule
    73  }
    74  
    75  func (r *onDemandRule) Target() int32 {
    76  	if r.OnDemandRule.Action == nil {
    77  		return -1
    78  	}
    79  	return int32(*r.OnDemandRule.Action)
    80  }
    81  
    82  func (r *onDemandRule) DNSSearchDomainMatch() StringIterator {
    83  	return newIterator(r.OnDemandRule.DNSSearchDomainMatch)
    84  }
    85  
    86  func (r *onDemandRule) DNSServerAddressMatch() StringIterator {
    87  	return newIterator(r.OnDemandRule.DNSServerAddressMatch)
    88  }
    89  
    90  func (r *onDemandRule) InterfaceTypeMatch() int32 {
    91  	if r.OnDemandRule.InterfaceTypeMatch == nil {
    92  		return -1
    93  	}
    94  	return int32(*r.OnDemandRule.InterfaceTypeMatch)
    95  }
    96  
    97  func (r *onDemandRule) SSIDMatch() StringIterator {
    98  	return newIterator(r.OnDemandRule.SSIDMatch)
    99  }
   100  
   101  func (r *onDemandRule) ProbeURL() string {
   102  	return r.OnDemandRule.ProbeURL
   103  }