platform.go 3.0 KB

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