nodesetcmd.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. "-snap": {0, snapFilter},
  87. }
  88. func parseFilters(args []string) ([]nodeFilter, error) {
  89. var filters []nodeFilter
  90. for len(args) > 0 {
  91. fc, ok := filterFlags[args[0]]
  92. if !ok {
  93. return nil, fmt.Errorf("invalid filter %q", args[0])
  94. }
  95. if len(args)-1 < fc.narg {
  96. return nil, fmt.Errorf("filter %q wants %d arguments, have %d", args[0], fc.narg, len(args)-1)
  97. }
  98. filter, err := fc.fn(args[1 : 1+fc.narg])
  99. if err != nil {
  100. return nil, fmt.Errorf("%s: %v", args[0], err)
  101. }
  102. filters = append(filters, filter)
  103. args = args[1+fc.narg:]
  104. }
  105. return filters, nil
  106. }
  107. func andFilter(args []string) (nodeFilter, error) {
  108. checks, err := parseFilters(args)
  109. if err != nil {
  110. return nil, err
  111. }
  112. f := func(n nodeJSON) bool {
  113. for _, filter := range checks {
  114. if !filter(n) {
  115. return false
  116. }
  117. }
  118. return true
  119. }
  120. return f, nil
  121. }
  122. func ipFilter(args []string) (nodeFilter, error) {
  123. _, cidr, err := net.ParseCIDR(args[0])
  124. if err != nil {
  125. return nil, err
  126. }
  127. f := func(n nodeJSON) bool { return cidr.Contains(n.N.IP()) }
  128. return f, nil
  129. }
  130. func minAgeFilter(args []string) (nodeFilter, error) {
  131. minage, err := time.ParseDuration(args[0])
  132. if err != nil {
  133. return nil, err
  134. }
  135. f := func(n nodeJSON) bool {
  136. age := n.LastResponse.Sub(n.FirstResponse)
  137. return age >= minage
  138. }
  139. return f, nil
  140. }
  141. func ethFilter(args []string) (nodeFilter, error) {
  142. var filter forkid.Filter
  143. switch args[0] {
  144. case "mainnet":
  145. filter = forkid.NewStaticFilter(params.MainnetChainConfig, params.MainnetGenesisHash)
  146. case "rinkeby":
  147. filter = forkid.NewStaticFilter(params.RinkebyChainConfig, params.RinkebyGenesisHash)
  148. case "goerli":
  149. filter = forkid.NewStaticFilter(params.GoerliChainConfig, params.GoerliGenesisHash)
  150. case "ropsten":
  151. filter = forkid.NewStaticFilter(params.RopstenChainConfig, params.RopstenGenesisHash)
  152. default:
  153. return nil, fmt.Errorf("unknown network %q", args[0])
  154. }
  155. f := func(n nodeJSON) bool {
  156. var eth struct {
  157. ForkID forkid.ID
  158. Tail []rlp.RawValue `rlp:"tail"`
  159. }
  160. if n.N.Load(enr.WithEntry("eth", &eth)) != nil {
  161. return false
  162. }
  163. return filter(eth.ForkID) == nil
  164. }
  165. return f, nil
  166. }
  167. func lesFilter(args []string) (nodeFilter, error) {
  168. f := func(n nodeJSON) bool {
  169. var les struct {
  170. Tail []rlp.RawValue `rlp:"tail"`
  171. }
  172. return n.N.Load(enr.WithEntry("les", &les)) == nil
  173. }
  174. return f, nil
  175. }
  176. func snapFilter(args []string) (nodeFilter, error) {
  177. f := func(n nodeJSON) bool {
  178. var snap struct {
  179. Tail []rlp.RawValue `rlp:"tail"`
  180. }
  181. return n.N.Load(enr.WithEntry("snap", &snap)) == nil
  182. }
  183. return f, nil
  184. }