dnscmd.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Copyright 2018 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. "crypto/ecdsa"
  19. "encoding/json"
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "path/filepath"
  24. "time"
  25. "github.com/ethereum/go-ethereum/accounts/keystore"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/console"
  28. "github.com/ethereum/go-ethereum/p2p/dnsdisc"
  29. "github.com/ethereum/go-ethereum/p2p/enode"
  30. cli "gopkg.in/urfave/cli.v1"
  31. )
  32. var (
  33. dnsCommand = cli.Command{
  34. Name: "dns",
  35. Usage: "DNS Discovery Commands",
  36. Subcommands: []cli.Command{
  37. dnsSyncCommand,
  38. dnsSignCommand,
  39. dnsTXTCommand,
  40. dnsCloudflareCommand,
  41. dnsRoute53Command,
  42. },
  43. }
  44. dnsSyncCommand = cli.Command{
  45. Name: "sync",
  46. Usage: "Download a DNS discovery tree",
  47. ArgsUsage: "<url> [ <directory> ]",
  48. Action: dnsSync,
  49. Flags: []cli.Flag{dnsTimeoutFlag},
  50. }
  51. dnsSignCommand = cli.Command{
  52. Name: "sign",
  53. Usage: "Sign a DNS discovery tree",
  54. ArgsUsage: "<tree-directory> <key-file>",
  55. Action: dnsSign,
  56. Flags: []cli.Flag{dnsDomainFlag, dnsSeqFlag},
  57. }
  58. dnsTXTCommand = cli.Command{
  59. Name: "to-txt",
  60. Usage: "Create a DNS TXT records for a discovery tree",
  61. ArgsUsage: "<tree-directory> <output-file>",
  62. Action: dnsToTXT,
  63. }
  64. dnsCloudflareCommand = cli.Command{
  65. Name: "to-cloudflare",
  66. Usage: "Deploy DNS TXT records to CloudFlare",
  67. ArgsUsage: "<tree-directory>",
  68. Action: dnsToCloudflare,
  69. Flags: []cli.Flag{cloudflareTokenFlag, cloudflareZoneIDFlag},
  70. }
  71. dnsRoute53Command = cli.Command{
  72. Name: "to-route53",
  73. Usage: "Deploy DNS TXT records to Amazon Route53",
  74. ArgsUsage: "<tree-directory>",
  75. Action: dnsToRoute53,
  76. Flags: []cli.Flag{route53AccessKeyFlag, route53AccessSecretFlag, route53ZoneIDFlag},
  77. }
  78. )
  79. var (
  80. dnsTimeoutFlag = cli.DurationFlag{
  81. Name: "timeout",
  82. Usage: "Timeout for DNS lookups",
  83. }
  84. dnsDomainFlag = cli.StringFlag{
  85. Name: "domain",
  86. Usage: "Domain name of the tree",
  87. }
  88. dnsSeqFlag = cli.UintFlag{
  89. Name: "seq",
  90. Usage: "New sequence number of the tree",
  91. }
  92. )
  93. // dnsSync performs dnsSyncCommand.
  94. func dnsSync(ctx *cli.Context) error {
  95. var (
  96. c = dnsClient(ctx)
  97. url = ctx.Args().Get(0)
  98. outdir = ctx.Args().Get(1)
  99. )
  100. domain, _, err := dnsdisc.ParseURL(url)
  101. if err != nil {
  102. return err
  103. }
  104. if outdir == "" {
  105. outdir = domain
  106. }
  107. t, err := c.SyncTree(url)
  108. if err != nil {
  109. return err
  110. }
  111. def := treeToDefinition(url, t)
  112. def.Meta.LastModified = time.Now()
  113. writeTreeMetadata(outdir, def)
  114. writeTreeNodes(outdir, def)
  115. return nil
  116. }
  117. func dnsSign(ctx *cli.Context) error {
  118. if ctx.NArg() < 2 {
  119. return fmt.Errorf("need tree definition directory and key file as arguments")
  120. }
  121. var (
  122. defdir = ctx.Args().Get(0)
  123. keyfile = ctx.Args().Get(1)
  124. def = loadTreeDefinition(defdir)
  125. domain = directoryName(defdir)
  126. )
  127. if def.Meta.URL != "" {
  128. d, _, err := dnsdisc.ParseURL(def.Meta.URL)
  129. if err != nil {
  130. return fmt.Errorf("invalid 'url' field: %v", err)
  131. }
  132. domain = d
  133. }
  134. if ctx.IsSet(dnsDomainFlag.Name) {
  135. domain = ctx.String(dnsDomainFlag.Name)
  136. }
  137. if ctx.IsSet(dnsSeqFlag.Name) {
  138. def.Meta.Seq = ctx.Uint(dnsSeqFlag.Name)
  139. } else {
  140. def.Meta.Seq++ // Auto-bump sequence number if not supplied via flag.
  141. }
  142. t, err := dnsdisc.MakeTree(def.Meta.Seq, def.Nodes, def.Meta.Links)
  143. if err != nil {
  144. return err
  145. }
  146. key := loadSigningKey(keyfile)
  147. url, err := t.Sign(key, domain)
  148. if err != nil {
  149. return fmt.Errorf("can't sign: %v", err)
  150. }
  151. def = treeToDefinition(url, t)
  152. def.Meta.LastModified = time.Now()
  153. writeTreeMetadata(defdir, def)
  154. return nil
  155. }
  156. func directoryName(dir string) string {
  157. abs, err := filepath.Abs(dir)
  158. if err != nil {
  159. exit(err)
  160. }
  161. return filepath.Base(abs)
  162. }
  163. // dnsToTXT peforms dnsTXTCommand.
  164. func dnsToTXT(ctx *cli.Context) error {
  165. if ctx.NArg() < 1 {
  166. return fmt.Errorf("need tree definition directory as argument")
  167. }
  168. output := ctx.Args().Get(1)
  169. if output == "" {
  170. output = "-" // default to stdout
  171. }
  172. domain, t, err := loadTreeDefinitionForExport(ctx.Args().Get(0))
  173. if err != nil {
  174. return err
  175. }
  176. writeTXTJSON(output, t.ToTXT(domain))
  177. return nil
  178. }
  179. // dnsToCloudflare peforms dnsCloudflareCommand.
  180. func dnsToCloudflare(ctx *cli.Context) error {
  181. if ctx.NArg() < 1 {
  182. return fmt.Errorf("need tree definition directory as argument")
  183. }
  184. domain, t, err := loadTreeDefinitionForExport(ctx.Args().Get(0))
  185. if err != nil {
  186. return err
  187. }
  188. client := newCloudflareClient(ctx)
  189. return client.deploy(domain, t)
  190. }
  191. // dnsToRoute53 peforms dnsRoute53Command.
  192. func dnsToRoute53(ctx *cli.Context) error {
  193. if ctx.NArg() < 1 {
  194. return fmt.Errorf("need tree definition directory as argument")
  195. }
  196. domain, t, err := loadTreeDefinitionForExport(ctx.Args().Get(0))
  197. if err != nil {
  198. return err
  199. }
  200. client := newRoute53Client(ctx)
  201. return client.deploy(domain, t)
  202. }
  203. // loadSigningKey loads a private key in Ethereum keystore format.
  204. func loadSigningKey(keyfile string) *ecdsa.PrivateKey {
  205. keyjson, err := ioutil.ReadFile(keyfile)
  206. if err != nil {
  207. exit(fmt.Errorf("failed to read the keyfile at '%s': %v", keyfile, err))
  208. }
  209. password, _ := console.Stdin.PromptPassword("Please enter the password for '" + keyfile + "': ")
  210. key, err := keystore.DecryptKey(keyjson, password)
  211. if err != nil {
  212. exit(fmt.Errorf("error decrypting key: %v", err))
  213. }
  214. return key.PrivateKey
  215. }
  216. // dnsClient configures the DNS discovery client from command line flags.
  217. func dnsClient(ctx *cli.Context) *dnsdisc.Client {
  218. var cfg dnsdisc.Config
  219. if commandHasFlag(ctx, dnsTimeoutFlag) {
  220. cfg.Timeout = ctx.Duration(dnsTimeoutFlag.Name)
  221. }
  222. return dnsdisc.NewClient(cfg)
  223. }
  224. // There are two file formats for DNS node trees on disk:
  225. //
  226. // The 'TXT' format is a single JSON file containing DNS TXT records
  227. // as a JSON object where the keys are names and the values are objects
  228. // containing the value of the record.
  229. //
  230. // The 'definition' format is a directory containing two files:
  231. //
  232. // enrtree-info.json -- contains sequence number & links to other trees
  233. // nodes.json -- contains the nodes as a JSON array.
  234. //
  235. // This format exists because it's convenient to edit. nodes.json can be generated
  236. // in multiple ways: it may be written by a DHT crawler or compiled by a human.
  237. type dnsDefinition struct {
  238. Meta dnsMetaJSON
  239. Nodes []*enode.Node
  240. }
  241. type dnsMetaJSON struct {
  242. URL string `json:"url,omitempty"`
  243. Seq uint `json:"seq"`
  244. Sig string `json:"signature,omitempty"`
  245. Links []string `json:"links"`
  246. LastModified time.Time `json:"lastModified"`
  247. }
  248. func treeToDefinition(url string, t *dnsdisc.Tree) *dnsDefinition {
  249. meta := dnsMetaJSON{
  250. URL: url,
  251. Seq: t.Seq(),
  252. Sig: t.Signature(),
  253. Links: t.Links(),
  254. }
  255. if meta.Links == nil {
  256. meta.Links = []string{}
  257. }
  258. return &dnsDefinition{Meta: meta, Nodes: t.Nodes()}
  259. }
  260. // loadTreeDefinition loads a directory in 'definition' format.
  261. func loadTreeDefinition(directory string) *dnsDefinition {
  262. metaFile, nodesFile := treeDefinitionFiles(directory)
  263. var def dnsDefinition
  264. err := common.LoadJSON(metaFile, &def.Meta)
  265. if err != nil && !os.IsNotExist(err) {
  266. exit(err)
  267. }
  268. if def.Meta.Links == nil {
  269. def.Meta.Links = []string{}
  270. }
  271. // Check link syntax.
  272. for _, link := range def.Meta.Links {
  273. if _, _, err := dnsdisc.ParseURL(link); err != nil {
  274. exit(fmt.Errorf("invalid link %q: %v", link, err))
  275. }
  276. }
  277. // Check/convert nodes.
  278. nodes := loadNodesJSON(nodesFile)
  279. if err := nodes.verify(); err != nil {
  280. exit(err)
  281. }
  282. def.Nodes = nodes.nodes()
  283. return &def
  284. }
  285. // loadTreeDefinitionForExport loads a DNS tree and ensures it is signed.
  286. func loadTreeDefinitionForExport(dir string) (domain string, t *dnsdisc.Tree, err error) {
  287. metaFile, _ := treeDefinitionFiles(dir)
  288. def := loadTreeDefinition(dir)
  289. if def.Meta.URL == "" {
  290. return "", nil, fmt.Errorf("missing 'url' field in %v", metaFile)
  291. }
  292. domain, pubkey, err := dnsdisc.ParseURL(def.Meta.URL)
  293. if err != nil {
  294. return "", nil, fmt.Errorf("invalid 'url' field in %v: %v", metaFile, err)
  295. }
  296. if t, err = dnsdisc.MakeTree(def.Meta.Seq, def.Nodes, def.Meta.Links); err != nil {
  297. return "", nil, err
  298. }
  299. if err := ensureValidTreeSignature(t, pubkey, def.Meta.Sig); err != nil {
  300. return "", nil, err
  301. }
  302. return domain, t, nil
  303. }
  304. // ensureValidTreeSignature checks that sig is valid for tree and assigns it as the
  305. // tree's signature if valid.
  306. func ensureValidTreeSignature(t *dnsdisc.Tree, pubkey *ecdsa.PublicKey, sig string) error {
  307. if sig == "" {
  308. return fmt.Errorf("missing signature, run 'devp2p dns sign' first")
  309. }
  310. if err := t.SetSignature(pubkey, sig); err != nil {
  311. return fmt.Errorf("invalid signature on tree, run 'devp2p dns sign' to update it")
  312. }
  313. return nil
  314. }
  315. // writeTreeMetadata writes a DNS node tree metadata file to the given directory.
  316. func writeTreeMetadata(directory string, def *dnsDefinition) {
  317. metaJSON, err := json.MarshalIndent(&def.Meta, "", jsonIndent)
  318. if err != nil {
  319. exit(err)
  320. }
  321. if err := os.Mkdir(directory, 0744); err != nil && !os.IsExist(err) {
  322. exit(err)
  323. }
  324. metaFile, _ := treeDefinitionFiles(directory)
  325. if err := ioutil.WriteFile(metaFile, metaJSON, 0644); err != nil {
  326. exit(err)
  327. }
  328. }
  329. func writeTreeNodes(directory string, def *dnsDefinition) {
  330. ns := make(nodeSet, len(def.Nodes))
  331. ns.add(def.Nodes...)
  332. _, nodesFile := treeDefinitionFiles(directory)
  333. writeNodesJSON(nodesFile, ns)
  334. }
  335. func treeDefinitionFiles(directory string) (string, string) {
  336. meta := filepath.Join(directory, "enrtree-info.json")
  337. nodes := filepath.Join(directory, "nodes.json")
  338. return meta, nodes
  339. }
  340. // writeTXTJSON writes TXT records in JSON format.
  341. func writeTXTJSON(file string, txt map[string]string) {
  342. txtJSON, err := json.MarshalIndent(txt, "", jsonIndent)
  343. if err != nil {
  344. exit(err)
  345. }
  346. if file == "-" {
  347. os.Stdout.Write(txtJSON)
  348. fmt.Println()
  349. return
  350. }
  351. if err := ioutil.WriteFile(file, txtJSON, 0644); err != nil {
  352. exit(err)
  353. }
  354. }