module_faucet.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2017 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/json"
  20. "fmt"
  21. "html/template"
  22. "math/rand"
  23. "path/filepath"
  24. "strconv"
  25. "strings"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/log"
  28. )
  29. // faucetDockerfile is the Dockerfile required to build an faucet container to
  30. // grant crypto tokens based on GitHub authentications.
  31. var faucetDockerfile = `
  32. FROM ethereum/client-go:alltools-latest
  33. ADD genesis.json /genesis.json
  34. ADD account.json /account.json
  35. ADD account.pass /account.pass
  36. ENTRYPOINT [ \
  37. "faucet", "--genesis", "/genesis.json", "--network", "{{.NetworkID}}", "--bootnodes", "{{.Bootnodes}}", "--ethstats", "{{.Ethstats}}", "--ethport", "{{.EthPort}}", \
  38. "--faucet.name", "{{.FaucetName}}", "--faucet.amount", "{{.FaucetAmount}}", "--faucet.minutes", "{{.FaucetMinutes}}", "--faucet.tiers", "{{.FaucetTiers}}", \
  39. "--account.json", "/account.json", "--account.pass", "/account.pass" \
  40. {{if .CaptchaToken}}, "--captcha.token", "{{.CaptchaToken}}", "--captcha.secret", "{{.CaptchaSecret}}"{{end}}{{if .NoAuth}}, "--noauth"{{end}} \
  41. ]`
  42. // faucetComposefile is the docker-compose.yml file required to deploy and maintain
  43. // a crypto faucet.
  44. var faucetComposefile = `
  45. version: '2'
  46. services:
  47. faucet:
  48. build: .
  49. image: {{.Network}}/faucet
  50. ports:
  51. - "{{.EthPort}}:{{.EthPort}}"{{if not .VHost}}
  52. - "{{.ApiPort}}:8080"{{end}}
  53. volumes:
  54. - {{.Datadir}}:/root/.faucet
  55. environment:
  56. - ETH_PORT={{.EthPort}}
  57. - ETH_NAME={{.EthName}}
  58. - FAUCET_AMOUNT={{.FaucetAmount}}
  59. - FAUCET_MINUTES={{.FaucetMinutes}}
  60. - FAUCET_TIERS={{.FaucetTiers}}
  61. - CAPTCHA_TOKEN={{.CaptchaToken}}
  62. - CAPTCHA_SECRET={{.CaptchaSecret}}
  63. - NO_AUTH={{.NoAuth}}{{if .VHost}}
  64. - VIRTUAL_HOST={{.VHost}}
  65. - VIRTUAL_PORT=8080{{end}}
  66. logging:
  67. driver: "json-file"
  68. options:
  69. max-size: "1m"
  70. max-file: "10"
  71. restart: always
  72. `
  73. // deployFaucet deploys a new faucet container to a remote machine via SSH,
  74. // docker and docker-compose. If an instance with the specified network name
  75. // already exists there, it will be overwritten!
  76. func deployFaucet(client *sshClient, network string, bootnodes []string, config *faucetInfos, nocache bool) ([]byte, error) {
  77. // Generate the content to upload to the server
  78. workdir := fmt.Sprintf("%d", rand.Int63())
  79. files := make(map[string][]byte)
  80. dockerfile := new(bytes.Buffer)
  81. template.Must(template.New("").Parse(faucetDockerfile)).Execute(dockerfile, map[string]interface{}{
  82. "NetworkID": config.node.network,
  83. "Bootnodes": strings.Join(bootnodes, ","),
  84. "Ethstats": config.node.ethstats,
  85. "EthPort": config.node.portFull,
  86. "CaptchaToken": config.captchaToken,
  87. "CaptchaSecret": config.captchaSecret,
  88. "FaucetName": strings.Title(network),
  89. "FaucetAmount": config.amount,
  90. "FaucetMinutes": config.minutes,
  91. "FaucetTiers": config.tiers,
  92. "NoAuth": config.noauth,
  93. })
  94. files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes()
  95. composefile := new(bytes.Buffer)
  96. template.Must(template.New("").Parse(faucetComposefile)).Execute(composefile, map[string]interface{}{
  97. "Network": network,
  98. "Datadir": config.node.datadir,
  99. "VHost": config.host,
  100. "ApiPort": config.port,
  101. "EthPort": config.node.portFull,
  102. "EthName": config.node.ethstats[:strings.Index(config.node.ethstats, ":")],
  103. "CaptchaToken": config.captchaToken,
  104. "CaptchaSecret": config.captchaSecret,
  105. "FaucetAmount": config.amount,
  106. "FaucetMinutes": config.minutes,
  107. "FaucetTiers": config.tiers,
  108. "NoAuth": config.noauth,
  109. })
  110. files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
  111. files[filepath.Join(workdir, "genesis.json")] = config.node.genesis
  112. files[filepath.Join(workdir, "account.json")] = []byte(config.node.keyJSON)
  113. files[filepath.Join(workdir, "account.pass")] = []byte(config.node.keyPass)
  114. // Upload the deployment files to the remote server (and clean up afterwards)
  115. if out, err := client.Upload(files); err != nil {
  116. return out, err
  117. }
  118. defer client.Run("rm -rf " + workdir)
  119. // Build and deploy the faucet service
  120. if nocache {
  121. return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s build --pull --no-cache && docker-compose -p %s up -d --force-recreate", workdir, network, network))
  122. }
  123. return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build --force-recreate", workdir, network))
  124. }
  125. // faucetInfos is returned from an faucet status check to allow reporting various
  126. // configuration parameters.
  127. type faucetInfos struct {
  128. node *nodeInfos
  129. host string
  130. port int
  131. amount int
  132. minutes int
  133. tiers int
  134. noauth bool
  135. captchaToken string
  136. captchaSecret string
  137. }
  138. // Report converts the typed struct into a plain string->string map, containing
  139. // most - but not all - fields for reporting to the user.
  140. func (info *faucetInfos) Report() map[string]string {
  141. report := map[string]string{
  142. "Website address": info.host,
  143. "Website listener port": strconv.Itoa(info.port),
  144. "Ethereum listener port": strconv.Itoa(info.node.portFull),
  145. "Funding amount (base tier)": fmt.Sprintf("%d Ethers", info.amount),
  146. "Funding cooldown (base tier)": fmt.Sprintf("%d mins", info.minutes),
  147. "Funding tiers": strconv.Itoa(info.tiers),
  148. "Captha protection": fmt.Sprintf("%v", info.captchaToken != ""),
  149. "Ethstats username": info.node.ethstats,
  150. }
  151. if info.noauth {
  152. report["Debug mode (no auth)"] = "enabled"
  153. }
  154. if info.node.keyJSON != "" {
  155. var key struct {
  156. Address string `json:"address"`
  157. }
  158. if err := json.Unmarshal([]byte(info.node.keyJSON), &key); err == nil {
  159. report["Funding account"] = common.HexToAddress(key.Address).Hex()
  160. } else {
  161. log.Error("Failed to retrieve signer address", "err", err)
  162. }
  163. }
  164. return report
  165. }
  166. // checkFaucet does a health-check against an faucet server to verify whether
  167. // it's running, and if yes, gathering a collection of useful infos about it.
  168. func checkFaucet(client *sshClient, network string) (*faucetInfos, error) {
  169. // Inspect a possible faucet container on the host
  170. infos, err := inspectContainer(client, fmt.Sprintf("%s_faucet_1", network))
  171. if err != nil {
  172. return nil, err
  173. }
  174. if !infos.running {
  175. return nil, ErrServiceOffline
  176. }
  177. // Resolve the port from the host, or the reverse proxy
  178. port := infos.portmap["8080/tcp"]
  179. if port == 0 {
  180. if proxy, _ := checkNginx(client, network); proxy != nil {
  181. port = proxy.port
  182. }
  183. }
  184. if port == 0 {
  185. return nil, ErrNotExposed
  186. }
  187. // Resolve the host from the reverse-proxy and the config values
  188. host := infos.envvars["VIRTUAL_HOST"]
  189. if host == "" {
  190. host = client.server
  191. }
  192. amount, _ := strconv.Atoi(infos.envvars["FAUCET_AMOUNT"])
  193. minutes, _ := strconv.Atoi(infos.envvars["FAUCET_MINUTES"])
  194. tiers, _ := strconv.Atoi(infos.envvars["FAUCET_TIERS"])
  195. // Retrieve the funding account informations
  196. var out []byte
  197. keyJSON, keyPass := "", ""
  198. if out, err = client.Run(fmt.Sprintf("docker exec %s_faucet_1 cat /account.json", network)); err == nil {
  199. keyJSON = string(bytes.TrimSpace(out))
  200. }
  201. if out, err = client.Run(fmt.Sprintf("docker exec %s_faucet_1 cat /account.pass", network)); err == nil {
  202. keyPass = string(bytes.TrimSpace(out))
  203. }
  204. // Run a sanity check to see if the port is reachable
  205. if err = checkPort(host, port); err != nil {
  206. log.Warn("Faucet service seems unreachable", "server", host, "port", port, "err", err)
  207. }
  208. // Container available, assemble and return the useful infos
  209. return &faucetInfos{
  210. node: &nodeInfos{
  211. datadir: infos.volumes["/root/.faucet"],
  212. portFull: infos.portmap[infos.envvars["ETH_PORT"]+"/tcp"],
  213. ethstats: infos.envvars["ETH_NAME"],
  214. keyJSON: keyJSON,
  215. keyPass: keyPass,
  216. },
  217. host: host,
  218. port: port,
  219. amount: amount,
  220. minutes: minutes,
  221. tiers: tiers,
  222. captchaToken: infos.envvars["CAPTCHA_TOKEN"],
  223. captchaSecret: infos.envvars["CAPTCHA_SECRET"],
  224. noauth: infos.envvars["NO_AUTH"] == "true",
  225. }, nil
  226. }