interface.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package provider
  2. import (
  3. "fmt"
  4. "github.com/metacubex/mihomo/common/utils"
  5. "github.com/metacubex/mihomo/constant"
  6. )
  7. // Vehicle Type
  8. const (
  9. File VehicleType = iota
  10. HTTP
  11. Compatible
  12. )
  13. // VehicleType defined
  14. type VehicleType int
  15. func (v VehicleType) String() string {
  16. switch v {
  17. case File:
  18. return "File"
  19. case HTTP:
  20. return "HTTP"
  21. case Compatible:
  22. return "Compatible"
  23. default:
  24. return "Unknown"
  25. }
  26. }
  27. type Vehicle interface {
  28. Read() ([]byte, error)
  29. Path() string
  30. Proxy() string
  31. Type() VehicleType
  32. }
  33. // Provider Type
  34. const (
  35. Proxy ProviderType = iota
  36. Rule
  37. )
  38. // ProviderType defined
  39. type ProviderType int
  40. func (pt ProviderType) String() string {
  41. switch pt {
  42. case Proxy:
  43. return "Proxy"
  44. case Rule:
  45. return "Rule"
  46. default:
  47. return "Unknown"
  48. }
  49. }
  50. // Provider interface
  51. type Provider interface {
  52. Name() string
  53. VehicleType() VehicleType
  54. Type() ProviderType
  55. Initial() error
  56. Update() error
  57. }
  58. // ProxyProvider interface
  59. type ProxyProvider interface {
  60. Provider
  61. Proxies() []constant.Proxy
  62. // Touch is used to inform the provider that the proxy is actually being used while getting the list of proxies.
  63. // Commonly used in DialContext and DialPacketConn
  64. Touch()
  65. HealthCheck()
  66. Version() uint32
  67. RegisterHealthCheckTask(url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint)
  68. HealthCheckURL() string
  69. }
  70. // RuleProvider interface
  71. type RuleProvider interface {
  72. Provider
  73. Behavior() RuleBehavior
  74. Match(*constant.Metadata) bool
  75. ShouldResolveIP() bool
  76. ShouldFindProcess() bool
  77. Strategy() any
  78. }
  79. // Rule Behavior
  80. const (
  81. Domain RuleBehavior = iota
  82. IPCIDR
  83. Classical
  84. )
  85. // RuleBehavior defined
  86. type RuleBehavior int
  87. func (rt RuleBehavior) String() string {
  88. switch rt {
  89. case Domain:
  90. return "Domain"
  91. case IPCIDR:
  92. return "IPCIDR"
  93. case Classical:
  94. return "Classical"
  95. default:
  96. return "Unknown"
  97. }
  98. }
  99. func (rt RuleBehavior) Byte() byte {
  100. switch rt {
  101. case Domain:
  102. return 0
  103. case IPCIDR:
  104. return 1
  105. case Classical:
  106. return 2
  107. default:
  108. return 255
  109. }
  110. }
  111. func ParseBehavior(s string) (behavior RuleBehavior, err error) {
  112. switch s {
  113. case "domain":
  114. behavior = Domain
  115. case "ipcidr":
  116. behavior = IPCIDR
  117. case "classical":
  118. behavior = Classical
  119. default:
  120. err = fmt.Errorf("unsupported behavior type: %s", s)
  121. }
  122. return
  123. }
  124. const (
  125. YamlRule RuleFormat = iota
  126. TextRule
  127. MrsRule
  128. )
  129. type RuleFormat int
  130. func (rf RuleFormat) String() string {
  131. switch rf {
  132. case YamlRule:
  133. return "YamlRule"
  134. case TextRule:
  135. return "TextRule"
  136. case MrsRule:
  137. return "MrsRule"
  138. default:
  139. return "Unknown"
  140. }
  141. }
  142. func ParseRuleFormat(s string) (format RuleFormat, err error) {
  143. switch s {
  144. case "", "yaml":
  145. format = YamlRule
  146. case "text":
  147. format = TextRule
  148. case "mrs":
  149. format = MrsRule
  150. default:
  151. err = fmt.Errorf("unsupported format type: %s", s)
  152. }
  153. return
  154. }
  155. type Tunnel interface {
  156. Providers() map[string]ProxyProvider
  157. RuleProviders() map[string]RuleProvider
  158. RuleUpdateCallback() *utils.Callback[RuleProvider]
  159. }