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.

84 lines
1.9 KiB

  1. // nolint: gosec
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "math/rand"
  9. "os"
  10. "path/filepath"
  11. "github.com/tendermint/tendermint/crypto/ed25519"
  12. "github.com/tendermint/tendermint/internal/p2p"
  13. "github.com/tendermint/tendermint/internal/p2p/pex"
  14. tmp2p "github.com/tendermint/tendermint/proto/tendermint/p2p"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. func main() {
  18. baseDir := flag.String("base", ".", `where the "corpus" directory will live`)
  19. flag.Parse()
  20. initCorpus(*baseDir)
  21. }
  22. func initCorpus(rootDir string) {
  23. log.SetFlags(0)
  24. corpusDir := filepath.Join(rootDir, "corpus")
  25. if err := os.MkdirAll(corpusDir, 0755); err != nil {
  26. log.Fatalf("Creating %q err: %v", corpusDir, err)
  27. }
  28. sizes := []int{0, 1, 2, 17, 5, 31}
  29. // Make the PRNG predictable
  30. rand.Seed(10)
  31. for _, n := range sizes {
  32. var addrs []*p2p.NetAddress
  33. // IPv4 addresses
  34. for i := 0; i < n; i++ {
  35. privKey := ed25519.GenPrivKey()
  36. addr := fmt.Sprintf(
  37. "%s@%v.%v.%v.%v:26656",
  38. types.NodeIDFromPubKey(privKey.PubKey()),
  39. rand.Int()%256,
  40. rand.Int()%256,
  41. rand.Int()%256,
  42. rand.Int()%256,
  43. )
  44. netAddr, _ := types.NewNetAddressString(addr)
  45. addrs = append(addrs, netAddr)
  46. }
  47. // IPv6 addresses
  48. privKey := ed25519.GenPrivKey()
  49. ipv6a, err := types.NewNetAddressString(
  50. fmt.Sprintf("%s@[ff02::1:114]:26656", types.NodeIDFromPubKey(privKey.PubKey())))
  51. if err != nil {
  52. log.Fatalf("can't create a new netaddress: %v", err)
  53. }
  54. addrs = append(addrs, ipv6a)
  55. msg := tmp2p.PexMessage{
  56. Sum: &tmp2p.PexMessage_PexResponse{
  57. PexResponse: &tmp2p.PexResponse{Addresses: pex.NetAddressesToProto(addrs)},
  58. },
  59. }
  60. bz, err := msg.Marshal()
  61. if err != nil {
  62. log.Fatalf("unable to marshal: %v", err)
  63. }
  64. filename := filepath.Join(rootDir, "corpus", fmt.Sprintf("%d", n))
  65. if err := ioutil.WriteFile(filename, bz, 0644); err != nil {
  66. log.Fatalf("can't write %X to %q: %v", bz, filename, err)
  67. }
  68. log.Printf("wrote %q", filename)
  69. }
  70. }