nodesetcmd.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "net"
  20. "time"
  21. "github.com/ethereum/go-ethereum/core/forkid"
  22. "github.com/ethereum/go-ethereum/p2p/enr"
  23. "github.com/ethereum/go-ethereum/params"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. "gopkg.in/urfave/cli.v1"
  26. )
  27. var (
  28. nodesetCommand = cli.Command{
  29. Name: "nodeset",
  30. Usage: "Node set tools",
  31. Subcommands: []cli.Command{
  32. nodesetInfoCommand,
  33. nodesetFilterCommand,
  34. },
  35. }
  36. nodesetInfoCommand = cli.Command{
  37. Name: "info",
  38. Usage: "Shows statistics about a node set",
  39. Action: nodesetInfo,
  40. ArgsUsage: "<nodes.json>",
  41. }
  42. nodesetFilterCommand = cli.Command{
  43. Name: "filter",
  44. Usage: "Filters a node set",
  45. Action: nodesetFilter,
  46. ArgsUsage: "<nodes.json> filters..",
  47. SkipFlagParsing: true,
  48. }
  49. )
  50. func nodesetInfo(ctx *cli.Context) error {
  51. if ctx.NArg() < 1 {
  52. return fmt.Errorf("need nodes file as argument")
  53. }
  54. ns := loadNodesJSON(ctx.Args().First())
  55. fmt.Printf("Set contains %d nodes.\n", len(ns))
  56. return nil
  57. }
  58. func nodesetFilter(ctx *cli.Context) error {
  59. if ctx.NArg() < 1 {
  60. return fmt.Errorf("need nodes file as argument")
  61. }
  62. ns := loadNodesJSON(ctx.Args().First())
  63. filter, err := andFilter(ctx.Args().Tail())
  64. if err != nil {
  65. return err
  66. }
  67. result := make(nodeSet)
  68. for id, n := range ns {
  69. if filter(n) {
  70. result[id] = n
  71. }
  72. }
  73. writeNodesJSON("-", result)
  74. return nil
  75. }
  76. type nodeFilter func(nodeJSON) bool
  77. type nodeFilterC struct {
  78. narg int
  79. fn func([]string) (nodeFilter, error)
  80. }
  81. var filterFlags = map[string]nodeFilterC{
  82. "-ip": {1, ipFilter},
  83. "-min-age": {1, minAgeFilter},
  84. "-eth-network": {1, ethFilter},
  85. "-les-server": {0, lesFilter},
  86. }
  87. func parseFilters(args []string) ([]nodeFilter, error) {
  88. var filters []nodeFilter
  89. for len(args) > 0 {
  90. fc, ok := filterFlags[args[0]]
  91. if !ok {
  92. return nil, fmt.Errorf("invalid filter %q", args[0])
  93. }
  94. if len(args) < fc.narg {
  95. return nil, fmt.Errorf("filter %q wants %d arguments, have %d", args[0], fc.narg, len(args))
  96. }
  97. filter, err := fc.fn(args[1:])
  98. if err != nil {
  99. return nil, fmt.Errorf("%s: %v", args[0], err)
  100. }
  101. filters = append(filters, filter)
  102. args = args[fc.narg+1:]
  103. }
  104. return filters, nil
  105. }
  106. func andFilter(args []string) (nodeFilter, error) {
  107. checks, err := parseFilters(args)
  108. if err != nil {
  109. return nil, err
  110. }
  111. f := func(n nodeJSON) bool {
  112. for _, filter := range checks {
  113. if !filter(n) {
  114. return false
  115. }
  116. }
  117. return true
  118. }
  119. return f, nil
  120. }
  121. func ipFilter(args []string) (nodeFilter, error) {
  122. _, cidr, err := net.ParseCIDR(args[0])
  123. if err != nil {
  124. return nil, err
  125. }
  126. f := func(n nodeJSON) bool { return cidr.Contains(n.N.IP()) }
  127. return f, nil
  128. }
  129. func minAgeFilter(args []string) (nodeFilter, error) {
  130. minage, err := time.ParseDuration(args[0])
  131. if err != nil {
  132. return nil, err
  133. }
  134. f := func(n nodeJSON) bool {
  135. age := n.LastResponse.Sub(n.FirstResponse)
  136. return age >= minage
  137. }
  138. return f, nil
  139. }
  140. func ethFilter(args []string) (nodeFilter, error) {
  141. var filter func(forkid.ID) error
  142. switch args[0] {
  143. case "mainnet":
  144. filter = forkid.NewStaticFilter(params.MainnetChainConfig, params.MainnetGenesisHash)
  145. case "rinkeby":
  146. filter = forkid.NewStaticFilter(params.RinkebyChainConfig, params.RinkebyGenesisHash)
  147. case "goerli":
  148. filter = forkid.NewStaticFilter(params.GoerliChainConfig, params.GoerliGenesisHash)
  149. case "ropsten":
  150. filter = forkid.NewStaticFilter(params.TestnetChainConfig, params.TestnetGenesisHash)
  151. default:
  152. return nil, fmt.Errorf("unknown network %q", args[0])
  153. }
  154. f := func(n nodeJSON) bool {
  155. var eth struct {
  156. ForkID forkid.ID
  157. _ []rlp.RawValue `rlp:"tail"`
  158. }
  159. if n.N.Load(enr.WithEntry("eth", &eth)) != nil {
  160. return false
  161. }
  162. return filter(eth.ForkID) == nil
  163. }
  164. return f, nil
  165. }
  166. func lesFilter(args []string) (nodeFilter, error) {
  167. f := func(n nodeJSON) bool {
  168. var les struct {
  169. _ []rlp.RawValue `rlp:"tail"`
  170. }
  171. return n.N.Load(enr.WithEntry("les", &les)) == nil
  172. }
  173. return f, nil
  174. }