platform.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. 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. StartDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
  17. CloseDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
  18. GetInterfaces() (NetworkInterfaceIterator, error)
  19. UnderNetworkExtension() bool
  20. IncludeAllNetworks() bool
  21. ReadWIFIState() *WIFIState
  22. SystemCertificates() StringIterator
  23. ClearDNSCache()
  24. SendNotification(notification *Notification) error
  25. }
  26. type TunInterface interface {
  27. FileDescriptor() int32
  28. Close() error
  29. }
  30. type InterfaceUpdateListener interface {
  31. UpdateDefaultInterface(interfaceName string, interfaceIndex int32, isExpensive bool, isConstrained bool)
  32. }
  33. const (
  34. InterfaceTypeWIFI = int32(C.InterfaceTypeWIFI)
  35. InterfaceTypeCellular = int32(C.InterfaceTypeCellular)
  36. InterfaceTypeEthernet = int32(C.InterfaceTypeEthernet)
  37. InterfaceTypeOther = int32(C.InterfaceTypeOther)
  38. )
  39. type NetworkInterface struct {
  40. Index int32
  41. MTU int32
  42. Name string
  43. Addresses StringIterator
  44. Flags int32
  45. Type int32
  46. DNSServer StringIterator
  47. Metered bool
  48. }
  49. type WIFIState struct {
  50. SSID string
  51. BSSID string
  52. }
  53. func NewWIFIState(wifiSSID string, wifiBSSID string) *WIFIState {
  54. return &WIFIState{wifiSSID, wifiBSSID}
  55. }
  56. type NetworkInterfaceIterator interface {
  57. Next() *NetworkInterface
  58. HasNext() bool
  59. }
  60. type Notification struct {
  61. Identifier string
  62. TypeName string
  63. TypeID int32
  64. Title string
  65. Subtitle string
  66. Body string
  67. OpenURL string
  68. }
  69. type OnDemandRule interface {
  70. Target() int32
  71. DNSSearchDomainMatch() StringIterator
  72. DNSServerAddressMatch() StringIterator
  73. InterfaceTypeMatch() int32
  74. SSIDMatch() StringIterator
  75. ProbeURL() string
  76. }
  77. type OnDemandRuleIterator interface {
  78. Next() OnDemandRule
  79. HasNext() bool
  80. }
  81. type onDemandRule struct {
  82. option.OnDemandRule
  83. }
  84. func (r *onDemandRule) Target() int32 {
  85. if r.OnDemandRule.Action == nil {
  86. return -1
  87. }
  88. return int32(*r.OnDemandRule.Action)
  89. }
  90. func (r *onDemandRule) DNSSearchDomainMatch() StringIterator {
  91. return newIterator(r.OnDemandRule.DNSSearchDomainMatch)
  92. }
  93. func (r *onDemandRule) DNSServerAddressMatch() StringIterator {
  94. return newIterator(r.OnDemandRule.DNSServerAddressMatch)
  95. }
  96. func (r *onDemandRule) InterfaceTypeMatch() int32 {
  97. if r.OnDemandRule.InterfaceTypeMatch == nil {
  98. return -1
  99. }
  100. return int32(*r.OnDemandRule.InterfaceTypeMatch)
  101. }
  102. func (r *onDemandRule) SSIDMatch() StringIterator {
  103. return newIterator(r.OnDemandRule.SSIDMatch)
  104. }
  105. func (r *onDemandRule) ProbeURL() string {
  106. return r.OnDemandRule.ProbeURL
  107. }