enrcmd.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. "bytes"
  19. "encoding/base64"
  20. "encoding/hex"
  21. "fmt"
  22. "io/ioutil"
  23. "net"
  24. "os"
  25. "strconv"
  26. "strings"
  27. "github.com/ethereum/go-ethereum/p2p/enode"
  28. "github.com/ethereum/go-ethereum/p2p/enr"
  29. "github.com/ethereum/go-ethereum/rlp"
  30. "gopkg.in/urfave/cli.v1"
  31. )
  32. var enrdumpCommand = cli.Command{
  33. Name: "enrdump",
  34. Usage: "Pretty-prints node records",
  35. Action: enrdump,
  36. Flags: []cli.Flag{
  37. cli.StringFlag{Name: "file"},
  38. },
  39. }
  40. func enrdump(ctx *cli.Context) error {
  41. var source string
  42. if file := ctx.String("file"); file != "" {
  43. if ctx.NArg() != 0 {
  44. return fmt.Errorf("can't dump record from command-line argument in -file mode")
  45. }
  46. var b []byte
  47. var err error
  48. if file == "-" {
  49. b, err = ioutil.ReadAll(os.Stdin)
  50. } else {
  51. b, err = ioutil.ReadFile(file)
  52. }
  53. if err != nil {
  54. return err
  55. }
  56. source = string(b)
  57. } else if ctx.NArg() == 1 {
  58. source = ctx.Args()[0]
  59. } else {
  60. return fmt.Errorf("need record as argument")
  61. }
  62. r, err := parseRecord(source)
  63. if err != nil {
  64. return fmt.Errorf("INVALID: %v", err)
  65. }
  66. fmt.Print(dumpRecord(r))
  67. return nil
  68. }
  69. // dumpRecord creates a human-readable description of the given node record.
  70. func dumpRecord(r *enr.Record) string {
  71. out := new(bytes.Buffer)
  72. if n, err := enode.New(enode.ValidSchemes, r); err != nil {
  73. fmt.Fprintf(out, "INVALID: %v\n", err)
  74. } else {
  75. fmt.Fprintf(out, "Node ID: %v\n", n.ID())
  76. }
  77. kv := r.AppendElements(nil)[1:]
  78. fmt.Fprintf(out, "Record has sequence number %d and %d key/value pairs.\n", r.Seq(), len(kv)/2)
  79. fmt.Fprint(out, dumpRecordKV(kv, 2))
  80. return out.String()
  81. }
  82. func dumpRecordKV(kv []interface{}, indent int) string {
  83. // Determine the longest key name for alignment.
  84. var out string
  85. var longestKey = 0
  86. for i := 0; i < len(kv); i += 2 {
  87. key := kv[i].(string)
  88. if len(key) > longestKey {
  89. longestKey = len(key)
  90. }
  91. }
  92. // Print the keys, invoking formatters for known keys.
  93. for i := 0; i < len(kv); i += 2 {
  94. key := kv[i].(string)
  95. val := kv[i+1].(rlp.RawValue)
  96. pad := longestKey - len(key)
  97. out += strings.Repeat(" ", indent) + strconv.Quote(key) + strings.Repeat(" ", pad+1)
  98. formatter := attrFormatters[key]
  99. if formatter == nil {
  100. formatter = formatAttrRaw
  101. }
  102. fmtval, ok := formatter(val)
  103. if ok {
  104. out += fmtval + "\n"
  105. } else {
  106. out += hex.EncodeToString(val) + " (!)\n"
  107. }
  108. }
  109. return out
  110. }
  111. // parseNode parses a node record and verifies its signature.
  112. func parseNode(source string) (*enode.Node, error) {
  113. if strings.HasPrefix(source, "enode://") {
  114. return enode.ParseV4(source)
  115. }
  116. r, err := parseRecord(source)
  117. if err != nil {
  118. return nil, err
  119. }
  120. return enode.New(enode.ValidSchemes, r)
  121. }
  122. // parseRecord parses a node record from hex, base64, or raw binary input.
  123. func parseRecord(source string) (*enr.Record, error) {
  124. bin := []byte(source)
  125. if d, ok := decodeRecordHex(bytes.TrimSpace(bin)); ok {
  126. bin = d
  127. } else if d, ok := decodeRecordBase64(bytes.TrimSpace(bin)); ok {
  128. bin = d
  129. }
  130. var r enr.Record
  131. err := rlp.DecodeBytes(bin, &r)
  132. return &r, err
  133. }
  134. func decodeRecordHex(b []byte) ([]byte, bool) {
  135. if bytes.HasPrefix(b, []byte("0x")) {
  136. b = b[2:]
  137. }
  138. dec := make([]byte, hex.DecodedLen(len(b)))
  139. _, err := hex.Decode(dec, b)
  140. return dec, err == nil
  141. }
  142. func decodeRecordBase64(b []byte) ([]byte, bool) {
  143. if bytes.HasPrefix(b, []byte("enr:")) {
  144. b = b[4:]
  145. }
  146. dec := make([]byte, base64.RawURLEncoding.DecodedLen(len(b)))
  147. n, err := base64.RawURLEncoding.Decode(dec, b)
  148. return dec[:n], err == nil
  149. }
  150. // attrFormatters contains formatting functions for well-known ENR keys.
  151. var attrFormatters = map[string]func(rlp.RawValue) (string, bool){
  152. "id": formatAttrString,
  153. "ip": formatAttrIP,
  154. "ip6": formatAttrIP,
  155. "tcp": formatAttrUint,
  156. "tcp6": formatAttrUint,
  157. "udp": formatAttrUint,
  158. "udp6": formatAttrUint,
  159. }
  160. func formatAttrRaw(v rlp.RawValue) (string, bool) {
  161. s := hex.EncodeToString(v)
  162. return s, true
  163. }
  164. func formatAttrString(v rlp.RawValue) (string, bool) {
  165. content, _, err := rlp.SplitString(v)
  166. return strconv.Quote(string(content)), err == nil
  167. }
  168. func formatAttrIP(v rlp.RawValue) (string, bool) {
  169. content, _, err := rlp.SplitString(v)
  170. if err != nil || len(content) != 4 && len(content) != 6 {
  171. return "", false
  172. }
  173. return net.IP(content).String(), true
  174. }
  175. func formatAttrUint(v rlp.RawValue) (string, bool) {
  176. var x uint64
  177. if err := rlp.DecodeBytes(v, &x); err != nil {
  178. return "", false
  179. }
  180. return strconv.FormatUint(x, 10), true
  181. }