platform.go 3.5 KB

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