module_faucet.go 8.8 KB

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