platform.go 2.9 KB

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