platform.go 2.7 KB

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