enrcmd.go 5.2 KB

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