Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

firehol.go 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package ipblocklist
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  6. "net"
  7. "regexp"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/9seconds/mtg/v2/ipblocklist/files"
  12. "github.com/9seconds/mtg/v2/mtglib"
  13. "github.com/kentik/patricia"
  14. "github.com/kentik/patricia/bool_tree"
  15. "github.com/panjf2000/ants/v2"
  16. )
  17. const (
  18. fireholIPv4DefaultCIDR = 32
  19. fireholIPv6DefaultCIDR = 128
  20. )
  21. var fireholRegexpComment = regexp.MustCompile(`\s*#.*?$`)
  22. // Firehol is IPBlocklist which uses lists from FireHOL:
  23. // https://iplists.firehol.org/
  24. //
  25. // It can use both local files and remote URLs. This is not necessary
  26. // that blocklists should be taken from this website, we expect only
  27. // compatible formats here.
  28. //
  29. // Example of the format:
  30. //
  31. // # this is a comment
  32. // # to ignore
  33. // 127.0.0.1 # you can specify an IP
  34. // 10.0.0.0/8 # or cidr
  35. type Firehol struct {
  36. ctx context.Context
  37. ctxCancel context.CancelFunc
  38. logger mtglib.Logger
  39. updateMutex sync.RWMutex
  40. blocklists []files.File
  41. workerPool *ants.Pool
  42. treeV4 *bool_tree.TreeV4
  43. treeV6 *bool_tree.TreeV6
  44. }
  45. // Shutdown stop a background update process.
  46. func (f *Firehol) Shutdown() {
  47. f.ctxCancel()
  48. }
  49. // Contains is given IP list can be found in FireHOL blocklists.
  50. func (f *Firehol) Contains(ip net.IP) bool {
  51. if ip == nil {
  52. return true
  53. }
  54. f.updateMutex.RLock()
  55. defer f.updateMutex.RUnlock()
  56. if ip4 := ip.To4(); ip4 != nil {
  57. return f.containsIPv4(ip4)
  58. }
  59. return f.containsIPv6(ip.To16())
  60. }
  61. // Run starts a background update process.
  62. //
  63. // This is a blocking method so you probably want to run it in a
  64. // goroutine.
  65. func (f *Firehol) Run(updateEach time.Duration) {
  66. if updateEach == 0 {
  67. updateEach = DefaultFireholUpdateEach
  68. }
  69. ticker := time.NewTicker(updateEach)
  70. defer func() {
  71. ticker.Stop()
  72. select {
  73. case <-ticker.C:
  74. default:
  75. }
  76. }()
  77. f.update()
  78. for {
  79. select {
  80. case <-f.ctx.Done():
  81. return
  82. case <-ticker.C:
  83. f.update()
  84. }
  85. }
  86. }
  87. func (f *Firehol) containsIPv4(addr net.IP) bool {
  88. ip := patricia.NewIPv4AddressFromBytes(addr, 32) // nolint: gomnd
  89. if ok, _ := f.treeV4.FindDeepestTag(ip); ok {
  90. return true
  91. }
  92. return false
  93. }
  94. func (f *Firehol) containsIPv6(addr net.IP) bool {
  95. ip := patricia.NewIPv6Address(addr, 128) // nolint: gomnd
  96. if ok, _ := f.treeV6.FindDeepestTag(ip); ok {
  97. return true
  98. }
  99. return false
  100. }
  101. func (f *Firehol) update() {
  102. ctx, cancel := context.WithCancel(f.ctx)
  103. defer cancel()
  104. wg := &sync.WaitGroup{}
  105. wg.Add(len(f.blocklists))
  106. treeMutex := &sync.Mutex{}
  107. v4tree := bool_tree.NewTreeV4()
  108. v6tree := bool_tree.NewTreeV6()
  109. for _, v := range f.blocklists {
  110. go func(file files.File) {
  111. defer wg.Done()
  112. logger := f.logger.BindStr("filename", file.String())
  113. fileContent, err := file.Open(ctx)
  114. if err != nil {
  115. logger.WarningError("update has failed", err)
  116. return
  117. }
  118. defer fileContent.Close()
  119. if err := f.updateFromFile(treeMutex, v4tree, v6tree, bufio.NewScanner(fileContent)); err != nil {
  120. logger.WarningError("update has failed", err)
  121. }
  122. }(v)
  123. }
  124. wg.Wait()
  125. f.updateMutex.Lock()
  126. defer f.updateMutex.Unlock()
  127. f.treeV4 = v4tree
  128. f.treeV6 = v6tree
  129. f.logger.Info("blocklist was updated")
  130. }
  131. func (f *Firehol) updateFromFile(mutex sync.Locker,
  132. v4tree *bool_tree.TreeV4,
  133. v6tree *bool_tree.TreeV6,
  134. scanner *bufio.Scanner) error {
  135. for scanner.Scan() {
  136. text := scanner.Text()
  137. text = fireholRegexpComment.ReplaceAllLiteralString(text, "")
  138. text = strings.TrimSpace(text)
  139. if text == "" {
  140. continue
  141. }
  142. ip, cidr, err := f.updateParseLine(text)
  143. if err != nil {
  144. return fmt.Errorf("cannot parse a line: %w", err)
  145. }
  146. f.updateAddToTrees(ip, cidr, mutex, v4tree, v6tree)
  147. }
  148. if scanner.Err() != nil {
  149. return fmt.Errorf("cannot parse a file: %w", scanner.Err())
  150. }
  151. return nil
  152. }
  153. func (f *Firehol) updateParseLine(text string) (net.IP, uint, error) {
  154. _, ipnet, err := net.ParseCIDR(text)
  155. if err != nil {
  156. ipaddr := net.ParseIP(text)
  157. if ipaddr == nil {
  158. return nil, 0, fmt.Errorf("incorrect ip address %s", text)
  159. }
  160. ip4 := ipaddr.To4()
  161. if ip4 != nil {
  162. return ip4, fireholIPv4DefaultCIDR, nil
  163. }
  164. return ipaddr.To16(), fireholIPv6DefaultCIDR, nil
  165. }
  166. ones, _ := ipnet.Mask.Size()
  167. return ipnet.IP, uint(ones), nil
  168. }
  169. func (f *Firehol) updateAddToTrees(ip net.IP, cidr uint,
  170. mutex sync.Locker,
  171. v4tree *bool_tree.TreeV4, v6tree *bool_tree.TreeV6) {
  172. mutex.Lock()
  173. defer mutex.Unlock()
  174. if ip.To4() != nil {
  175. v4tree.Set(patricia.NewIPv4AddressFromBytes(ip, cidr), true)
  176. } else {
  177. v6tree.Set(patricia.NewIPv6Address(ip, cidr), true)
  178. }
  179. }
  180. // NewFirehol creates a new instance of FireHOL IP blocklist.
  181. //
  182. // This method does not start an update process so please execute Run
  183. // when it is necessary.
  184. func NewFirehol(logger mtglib.Logger, network mtglib.Network,
  185. downloadConcurrency uint,
  186. urls []string,
  187. localFiles []string) (*Firehol, error) {
  188. blocklists := []files.File{}
  189. for _, v := range localFiles {
  190. file, err := files.NewLocal(v)
  191. if err != nil {
  192. return nil, fmt.Errorf("cannot create a local file %s: %w", v, err)
  193. }
  194. blocklists = append(blocklists, file)
  195. }
  196. httpClient := network.MakeHTTPClient(nil)
  197. for _, v := range urls {
  198. file, err := files.NewHTTP(httpClient, v)
  199. if err != nil {
  200. return nil, fmt.Errorf("cannot create a HTTP file %s: %w", v, err)
  201. }
  202. blocklists = append(blocklists, file)
  203. }
  204. return NewFireholFromFiles(logger, downloadConcurrency, blocklists)
  205. }
  206. func NewFireholFromFiles(logger mtglib.Logger,
  207. downloadConcurrency uint,
  208. blocklists []files.File) (*Firehol, error) {
  209. if downloadConcurrency == 0 {
  210. downloadConcurrency = DefaultFireholDownloadConcurrency
  211. }
  212. workerPool, _ := ants.NewPool(int(downloadConcurrency))
  213. ctx, cancel := context.WithCancel(context.Background())
  214. return &Firehol{
  215. ctx: ctx,
  216. ctxCancel: cancel,
  217. logger: logger.Named("firehol"),
  218. treeV4: bool_tree.NewTreeV4(),
  219. treeV6: bool_tree.NewTreeV6(),
  220. workerPool: workerPool,
  221. blocklists: blocklists,
  222. }, nil
  223. }